Example usage for android.graphics Matrix Matrix

List of usage examples for android.graphics Matrix Matrix

Introduction

In this page you can find the example usage for android.graphics Matrix Matrix.

Prototype

public Matrix() 

Source Link

Document

Create an identity matrix

Usage

From source file:Main.java

public static Bitmap createReflectImages(Bitmap bImap) {
    if (null == bImap) {
        return null;
    }// w w  w.  j a va  2  s . com

    final int reflectionGap = 4;
    int width = bImap.getWidth();
    int height = bImap.getHeight();

    Bitmap bitmapWithReflection = null;
    try {
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionImage = Bitmap.createBitmap(bImap, 0, height / 2, width, height / 2, matrix, false);
        bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bImap, 0, 0, null);
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bImap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    } catch (OutOfMemoryError e) {
    }

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap yuv2bitmap(byte[] data, int width, int height) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    yuvImage.compressToJpeg(new android.graphics.Rect(0, 0, width, height), 100, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

    // rotate// w  ww. j a  va 2  s . c o m
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap dst = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    return dst;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from   w w w  . j  a  va 2  s  .c o m

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
                Bitmap.Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
                Shader.TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static boolean rotateBitmap(File inFile, File outFile, int angle)
        throws FileNotFoundException, IOException {
    // Declare//ww w.  j a v a 2  s. co  m
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    // Create options
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Create transform matrix
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    // Increment inSampleSize progressively to reduce image resolution and size. If
    // the program is properly managing memory, and you don't have other large images
    // loaded in memory, this loop will generally not need to go through more than 3
    // iterations. To be safe though, we stop looping after a certain amount of tries
    // to avoid infinite loops
    for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
        try {
            // Load the bitmap from file
            inStream = new FileInputStream(inFile);
            Bitmap originalBitmap = BitmapFactory.decodeStream(inStream, null, options);
            // Rotate the bitmap
            Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            // Save the rotated bitmap
            outStream = new FileOutputStream(outFile);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
            // Recycle the bitmaps to immediately free memory
            originalBitmap.recycle();
            originalBitmap = null;
            rotatedBitmap.recycle();
            rotatedBitmap = null;
            // Return
            return true;
        } catch (OutOfMemoryError e) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
        } finally {
            // Clean-up if we failed on save
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Failed
    return false;
}

From source file:Main.java

public static Bitmap rotateBitmapInNeeded(String path, Bitmap srcBitmap) {
    if (TextUtils.isEmpty(path) || srcBitmap == null) {
        return null;
    }//from  ww  w. j a v a  2s.c  o  m

    ExifInterface localExifInterface;
    try {
        localExifInterface = new ExifInterface(path);
        int rotateInt = localExifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        float rotate = getImageRotate(rotateInt);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(),
                    matrix, false);
            if (dstBitmap == null) {
                return srcBitmap;
            } else {
                if (srcBitmap != null && !srcBitmap.isRecycled()) {
                    srcBitmap.recycle();
                }
                return dstBitmap;
            }
        } else {
            return srcBitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return srcBitmap;
    }
}

From source file:Main.java

static public Bitmap getOrientedBitmapFromBitmapAndPath(Bitmap bitmap, String filePath) {
    Log.d(TAG, "[AirImagePickerUtils] Entering getOrientedBitmapFromBitmapAndPath");
    try {/* ww  w.  j a v a  2  s.c  om*/
        // Get orientation from EXIF
        ExifInterface exif = new ExifInterface(filePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        // Compute rotation matrix
        Matrix rotation = new Matrix();
        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotation.preRotate(90);
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotation.preRotate(180);
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotation.preRotate(270);
            break;
        }

        // Return new bitmap
        Log.d(TAG, "[AirImagePickerUtils] Exiting getOrientedBitmapFromBitmapAndPath");
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotation, true);
    } catch (Exception exception) {
        Log.d(TAG, "Couldn't fix bitmap orientation: " + exception.getMessage());
        Log.d(TAG, "[AirImagePickerUtils] Exiting getOrientedBitmapFromBitmapAndPath");
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap scale(Bitmap b, int reqWidth, int reqHeight) {
    Matrix m = new Matrix();
    if (b.getWidth() > b.getHeight()) {
        reqWidth = (int) (reqHeight * (1.0 * b.getWidth() / b.getHeight()));
    } else {/*from ww w . j a  v a 2 s  .  com*/
        reqHeight = (int) (reqWidth * (1.0 * b.getHeight() / b.getWidth()));
    }
    m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight),
            Matrix.ScaleToFit.CENTER);
    return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
}

From source file:Main.java

public static Bitmap roateBitmap(Bitmap bitmap, int roatation) {
    Matrix matrix = new Matrix();
    matrix.setRotate(roatation);/*from  w  w w.j  a  v  a 2 s  .co  m*/
    if (bitmap == null) {
        return null;
    }
    try {
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap resize(Bitmap bitmap, int targetWidth, int targetHeight) {
    if (bitmap == null || targetWidth < 0 || targetHeight < 0) {
        return null;
    }/*w  w  w  . j  a v  a  2s.c  o m*/
    int pictureWidth = bitmap.getWidth();
    int pictureHeight = bitmap.getHeight();
    float scale = Math.min((float) targetWidth / pictureWidth, (float) targetHeight / pictureHeight); // (1)

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    return Bitmap.createBitmap(bitmap, 0, 0, pictureWidth, pictureHeight, matrix, true);
}

From source file:Main.java

/** simply resizes a given drawable resource to the given width and height */
public static Drawable resizeImage(Context ctx, int resId, int iconWidth, int iconHeight) {

    // load the origial Bitmap
    Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(), resId);

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = iconWidth;
    int newHeight = iconHeight;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);

    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}