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 rotate(Bitmap b, int degrees, Matrix m) {
    if (degrees != 0 && b != null) {
        if (m == null) {
            m = new Matrix();
        }//ww  w  .j ava 2s  .c o m
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
            Log.e(TAG, "Got oom exception ", ex);
        }
    }
    return b;
}

From source file:Main.java

public static Bitmap getImage(Context context, Bitmap source, int row, int col, int rowTotal, int colTotal,
        float multiple, boolean isRecycle) {
    Bitmap temp = getBitmap(source, (col - 1) * source.getWidth() / colTotal,
            (row - 1) * source.getHeight() / rowTotal, source.getWidth() / colTotal,
            source.getHeight() / rowTotal);

    if (isRecycle) {
        recycleBitmap(source);// w  ww. ja  v a  2 s.  com
    }
    if (multiple != 1.0) {
        Matrix matrix = new Matrix();
        matrix.postScale(multiple, multiple);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    }
    return temp;
}

From source file:Main.java

public static Bitmap imageScale(Bitmap bitmap, int dst_w, int dst_h) {
    int src_w = bitmap.getWidth();
    int src_h = bitmap.getHeight();
    float scale_w = ((float) dst_w) / src_w;
    float scale_h = ((float) dst_h) / src_h;
    Matrix matrix = new Matrix();
    matrix.postScale(scale_w, scale_h);/*from ww w. j a  v a 2 s .c  om*/
    Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix, true);
    return dstbmp;
}

From source file:Main.java

public static void scaleImage(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();
    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);/*w w  w  . j a  v  a  2s .  c  o m*/
    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();
    // Apply the scaled bitmap
    view.setImageDrawable(result);
    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

From source file:Main.java

/**
 * Shrinks and rotates (if necessary) a passed Bitmap.
 *
 * @param bm/* w  w  w.  j  a v a2s . c  o m*/
 * @param maxLengthOfEdge
 * @param rotateXDegree
 * @return Bitmap
 */
public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge, int rotateXDegree) {
    if (maxLengthOfEdge > bm.getWidth() && maxLengthOfEdge > bm.getHeight()) {
        return bm;
    } else {
        // shrink image
        float scale = (float) 1.0;
        if (bm.getHeight() > bm.getWidth()) {
            scale = ((float) maxLengthOfEdge) / bm.getHeight();
        } else {
            scale = ((float) maxLengthOfEdge) / bm.getWidth();
        }
        // CREATE CommonAsync MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scale, scale);
        matrix.postRotate(rotateXDegree);

        // RECREATE THE NEW BITMAP
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);

        matrix = null;
        System.gc();

        return bm;
    }
}

From source file:Main.java

public static Bitmap rotateImage(Bitmap bitmap, String storagePath) {
    Bitmap resultBitmap = bitmap;//from w w w. j a  v  a2s  .c  o m
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        // 1: nothing to do

        // 2
        if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.postScale(-1.0f, 1.0f);
        }
        // 3
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        }
        // 4
        else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.postScale(1.0f, -1.0f);
        }
        // 5
        else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 6
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        }
        // 7
        else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 8
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (resultBitmap != bitmap) {
            bitmap.recycle();
        }
    } catch (Exception exception) {
        Log.e("BitmapUtil", "Could not rotate the image: " + storagePath);
    }
    return resultBitmap;
}

From source file:Main.java

public static void scaleImageWithOriRatio(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);//from   w ww .ja va 2  s .c o  m

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

From source file:Main.java

public static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
    Matrix m = sMatrix.get();/*www .j ava2  s .c o  m*/
    if (m == null) {
        m = new Matrix();
        sMatrix.set(m);
    } else {
        m.reset();
    }

    offsetDescendantMatrix(group, child, m);

    RectF rectF = sRectF.get();
    if (rectF == null) {
        rectF = new RectF();
        sRectF.set(rectF);
    }
    rectF.set(rect);
    m.mapRect(rectF);
    rect.set((int) (rectF.left + 0.5f), (int) (rectF.top + 0.5f), (int) (rectF.right + 0.5f),
            (int) (rectF.bottom + 0.5f));
}

From source file:Main.java

public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
    boolean saveComplete = true;
    try {/*from w ww  .  java2  s. c  o m*/
        f.createNewFile();
        FileOutputStream fOut = null;
        fOut = new FileOutputStream(f);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        int finalWidth = 800;
        int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
        double x = width * finalHeight;
        double y = height * finalWidth;

        if (x > y) {
            finalHeight = (int) (y / (double) width);
        } else if (x < y) {
            finalWidth = (int) (x / (double) height);
        }

        if (finalWidth > width && finalHeight > height) {
            finalWidth = width;
            finalHeight = height;
        }
        Matrix matrix = new Matrix();
        matrix.reset();
        float scaleWidth = ((float) finalWidth) / (float) width;
        float scaleHeight = ((float) finalHeight) / (float) height;
        matrix.postScale(scaleWidth, scaleHeight);
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();
        mBitmap.recycle();
        System.gc();
    } catch (FileNotFoundException e) {
        saveComplete = false;
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        saveComplete = false;
    }
    return saveComplete;
}

From source file:Main.java

public static Bitmap scaleBitmapForDevice(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*  www . j  a  v  a2  s  . co m*/

    float density = Resources.getSystem().getDisplayMetrics().density;
    int newWidth = (int) (bitmap.getWidth() * density);
    int newHeight = (int) (bitmap.getHeight() * density);
    /*
    Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
     */
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));
    bitmap.recycle();

    return scaledBitmap;
}