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 getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    final int w = bm.getWidth();
    final int h = bm.getHeight();
    final float sw = ((float) newWidth) / w;
    final float sh = ((float) newHeight) / h;

    Matrix matrix = new Matrix();
    matrix.postScale(sw, sh);/* w  w  w . j  a v  a  2  s .c  o m*/
    return Bitmap.createBitmap(bm, 0, 0, w, h, matrix, false);
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
    int newWidth = width;
    int newHeight = height;
    // if(newWidth < bitmap.getWidth()){
    // return bitmap;
    // }//w w w. j  a v a  2  s .  c  om
    if (newHeight == 0) {
        newHeight = (int) (newWidth / (float) bitmap.getWidth() * bitmap.getHeight());
    }
    Bitmap result = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Matrix matrix = new Matrix();
    float scaleX = 1;
    float scaleY = 1;
    // if(newWidth>bitmap.getWidth()){
    scaleX = newWidth / (float) bitmap.getWidth();
    if (height != 0) {
        scaleY = newHeight / (float) bitmap.getHeight();
    } else {
        scaleY = scaleX;
    }
    // }
    matrix.postScale(scaleX, scaleY);
    canvas.drawBitmap(bitmap, matrix, null);
    return result;
}

From source file:Main.java

/**
 * Adjust the photo orientation//from   w  ww.j a va 2  s.  c o  m
 * @param pathToFile
 * @return
 */
public static Bitmap adjustPhotoOrientation(String pathToFile) {
    try {
        Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
        ExifInterface exif = new ExifInterface(pathToFile);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }
        if (rotate != 0) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap correctOrientation(Bitmap bitmap, int orientation) {
    Matrix matrix = new Matrix();
    matrix.postRotate(orientation);/*from   w w  w  . j  av  a 2 s. co  m*/
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

/**
 * This method resets bitmap orientation to 0 if not.
 * // ww w . j  av  a2s  .c om
 * @param path
 * @param bitmap
 * @return bitmap with 0 degree orientation
 */
public static Bitmap resetBitmapOrientation(String path, Bitmap bitmap) {
    // TODO Auto-generated method stub
    int rotate = 0;
    try {
        ExifInterface ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        default:
            // do nothing...
            break;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (rotate == 0) {
        return bitmap;
    } else {
        Matrix m = new Matrix();
        m.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    }
}

From source file:Main.java

public static Bitmap rotate(Bitmap source, float degrees) {
    if (source == null || source.getWidth() == 0 || source.getHeight() == 0)
        return null;
    int width = source.getWidth();
    int height = source.getHeight();
    Matrix m = new Matrix();
    float w = width;
    float h = height;
    float px = w / 2;
    float py = h / 2;
    m.setRotate(degrees, px, py);//w  ww .java 2 s. c  o  m
    return Bitmap.createBitmap(source, 0, 0, width, height, m, true);
}

From source file:Main.java

public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();/*from  ww w  .j a  v a2  s .c o  m*/
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*w  w w .ja v a  2 s.  co 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), 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, 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(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 Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;
}

From source file:Main.java

/**
 * To avoid problems with rotated videos retrieved from camera
 * @param bitmap/*from  w  w  w .j a  va2  s. c  o  m*/
 * @param filePath
 * @return
 */
public static Bitmap rotateImage(Bitmap bitmap, String filePath) {
    Bitmap resultBitmap = bitmap;

    try {
        ExifInterface exifInterface = new ExifInterface(filePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (Exception exception) {
        Log.d("AndroidTouchGallery", "Could not rotate the image");
    }
    return resultBitmap;
}