Example usage for android.graphics Matrix getValues

List of usage examples for android.graphics Matrix getValues

Introduction

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

Prototype

public void getValues(float[] values) 

Source Link

Document

Copy 9 values from the matrix into the array.

Usage

From source file:Main.java

public static Drawable rotateBitmap(Bitmap inputBitmap) {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, (float) inputBitmap.getWidth() / 2, (float) inputBitmap.getHeight() / 2);

    float outputX = inputBitmap.getHeight();
    float outputY = 0;

    final float[] values = new float[9];
    matrix.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    matrix.postTranslate(outputX - x1, outputY - y1);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getHeight(), inputBitmap.getWidth(),
            Bitmap.Config.ARGB_8888);//from ww  w.j  a  v  a2s  .c  o  m
    Paint paint = new Paint();
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawBitmap(inputBitmap, matrix, paint);
    return new BitmapDrawable(null, outputBitmap);
}

From source file:Main.java

public static Bitmap resizeBitmapToImageView(String path) {
    Bitmap resizedBitmap = null;// w w w .  jav  a  2  s. com
    try {
        int inWidth;
        int inHeight;
        int dstWidth = 1000;
        int dstHeight = 1000;

        InputStream in = new FileInputStream(path);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(path);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)

        options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]),
                (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        //            try {
        //                FileOutputStream out = new FileOutputStream(path);
        //                resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        //            } catch (Exception e) {
        ////                Log.e("Image", e.getMessage(), e);
        //            }
    } catch (IOException e) {
        //            Log.e("Image", e.getMessage(), e);
    }
    return resizedBitmap;
}

From source file:Main.java

private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) {
    try {/*w  w w . j av  a  2 s . c  o m*/
        int inWidth = 0;
        int inHeight = 0;

        InputStream in = new FileInputStream(pathOfInputImage);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(pathOfInputImage);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)
        options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        try {
            FileOutputStream out = new FileOutputStream(pathOfInputImage);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (Exception e) {
            Log.e("Image", e.getMessage(), e);
        }
    } catch (IOException e) {
        Log.e("Image", e.getMessage(), e);
    } catch (Exception e) {
        Log.e("Image", e.getMessage());
    }
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/*from   w  w w  .ja v a 2 s .c om*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }

    final float[] values = new float[9];
    m.getValues(values);

    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];

    m.postTranslate(targetX - x1, targetY - y1);

    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);

    return bm1;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();//from w w  w .j a v  a  2  s .c  om
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }

    final float[] values = new float[9];
    m.getValues(values);

    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];

    m.postTranslate(targetX - x1, targetY - y1);

    Bitmap temp = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    Canvas canvas = new Canvas(temp);
    canvas.drawBitmap(bm, m, paint);

    return temp;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/*  w  w w.  ja  v  a  2 s  .  c o m*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }
    final float[] values = new float[9];
    m.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    m.postTranslate(targetX - x1, targetY - y1);
    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);
    bm.recycle();
    bm = null;
    return bm1;
}

From source file:it.mb.whatshare.Utils.java

/**
 * Encodes the argument <tt>matrix</tt> into a JSON array.
 * /* w  w w .  j a v a2  s .c om*/
 * <p>
 * Values are cast to <tt>double</tt> because of JSON lack for a primitive
 * <tt>float</tt> value.
 * 
 * @param matrix
 *            the matrix to be encoded
 * @return the matrix encoded into a JSON array, or <code>null</code> if
 *         <tt>matrix</tt> is <code>null</code> or inconsistent (i.e. it
 *         doesn't contain <tt>float</tt>'s)
 */
public static JSONArray matrixToJson(Matrix matrix) {
    if (matrix == null)
        return null;
    JSONArray array = new JSONArray();
    float[] values = new float[9];
    matrix.getValues(values);
    for (float value : values) {
        try {
            array.put(value);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
    return array;
}

From source file:de.wikilab.android.friendica01.Max.java

public static void resizeImage(String pathOfInputImage, String pathOfOutputImage, int dstWidth, int dstHeight) {
    try {/*from  ww w. j  a v a2 s .  c om*/
        int inWidth = 0;
        int inHeight = 0;

        InputStream in = new FileInputStream(pathOfInputImage);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(pathOfInputImage);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)
        options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        try {
            FileOutputStream out = new FileOutputStream(pathOfOutputImage);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (Exception e) {
            Log.e("Image", e.getMessage(), e);
        }
    } catch (IOException e) {
        Log.e("Image", e.getMessage(), e);
    }
}

From source file:com.mylikes.likes.etchasketch.Slate.java

public static float getScale(Matrix m) {
    m.getValues(mvals);
    return mvals[0];
}

From source file:com.fractview.ImageViewFragment.java

private void applyTouch(Matrix bitmapMatrix, Matrix prefsMatrix) {
    // Did we only move?
    final float[] bm = new float[9];
    bitmapMatrix.getValues(bm);

    if (bm[0] == 1f && bm[1] == 0f && bm[3] == 0f && bm[4] == 1f) {
        Log.d(TAG, "We only moved!");
        taskFragment.modifyImage(new UnsafeImageEditor() {
            @Override/*from  ww w .j  a  va2 s  .  co  m*/
            public void edit(AbstractImgCache cache) {
                ScaleableCache scaleable = (ScaleableCache) cache;
                scaleable.move((int) Math.round(bm[2]), (int) Math.round(bm[5]));
            }
        }, true);
    } else {
        // Update zoom
        Log.d(TAG, "We need to scale");
        Matrix m = new Matrix();
        prefsMatrix.invert(m);

        final float[] matrix = new float[9];
        m.getValues(matrix);

        taskFragment.modifyImage(new UnsafeImageEditor() {
            @Override
            public void edit(AbstractImgCache cache) {
                ScaleableCache scaleable = (ScaleableCache) cache;
                scaleable.newRelativeScale(matrix);
            }
        }, true);
    }
}