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 scale(Bitmap bitmap, float scaling) {
    Matrix matrix = new Matrix();
    matrix.postScale(scaling, scaling);/*from w  ww .j  av  a  2 s . c o  m*/
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

/**
 * A copy of the Android internals StoreThumbnail method, it used with the insertImage to
 * populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
 * meta data. The StoreThumbnail method is private so it must be duplicated here.
 * @see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
 *//*ww  w.j a  va 2  s.  co  m*/
private static final Bitmap storeThumbnail(ContentResolver cr, Bitmap source, long id, float width,
        float height, int kind) {

    // create the matrix to scale it
    Matrix matrix = new Matrix();

    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();

    matrix.setScale(scaleX, scaleY);

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    ContentValues values = new ContentValues(4);
    values.put(MediaStore.Images.Thumbnails.KIND, kind);
    values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int) id);
    values.put(MediaStore.Images.Thumbnails.HEIGHT, thumb.getHeight());
    values.put(MediaStore.Images.Thumbnails.WIDTH, thumb.getWidth());

    Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);

    try {
        OutputStream thumbOut = cr.openOutputStream(url);
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
        thumbOut.close();
        return thumb;
    } catch (FileNotFoundException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
}

From source file:Main.java

public static Bitmap rotateWithCanvas(Bitmap bitmap, int degrees) {

    int destWidth, destHeight;

    float centerX = bitmap.getWidth() / 2;
    float centerY = bitmap.getHeight() / 2;

    // We want to do the rotation at origin, but since the bounding
    // rectangle will be changed after rotation, so the delta values
    // are based on old & new width/height respectively.
    Matrix matrix = new Matrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postRotate(degrees);/*from   w  ww . j  ava2  s  .c om*/
    if (degrees / 90 % 2 == 0) {
        destWidth = bitmap.getWidth();
        destHeight = bitmap.getHeight();
        matrix.postTranslate(centerX, centerY);
    } else {
        destWidth = bitmap.getHeight();
        destHeight = bitmap.getWidth();
        matrix.postTranslate(centerY, centerX);
    }
    Bitmap cropped = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, matrix, null);
    return cropped;
}

From source file:Main.java

public static Bitmap createBitmapFromByteArray(byte[] data, Size previewSize) {
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos);
    byte[] jdata = baos.toByteArray();
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;//from  ww w.  jav a 2s.c  o  m
    Bitmap bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length, opt);

    Matrix matrix = new Matrix();
    matrix.postRotate(-90);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

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);/* w w w . j  a v a2s  .  c  om*/
    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);
    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 rotateBitmap(Bitmap source, int rotation, boolean recycle) {
    if (rotation == 0)
        return source;
    int w = source.getWidth();
    int h = source.getHeight();
    Matrix m = new Matrix();
    m.postRotate(rotation);/*from w  w w  .  j  a va  2s  .  c  om*/
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, w, h, m, true);
    if (recycle)
        source.recycle();
    return bitmap;
}

From source file:Main.java

/**
 * get a screen shot with size : width X height.
 *///from   ww  w. j  a  v a 2s.c o  m
public static Bitmap getScreenShot(Activity activity, int width, int height) {
    if (activity == null || width < 1 || height < 1) {
        return null;
    }
    Window window = activity.getWindow();
    if (window == null) {
        return null;
    }
    View decorView = window.getDecorView();
    if (decorView == null) {
        return null;
    }
    decorView.setDrawingCacheEnabled(true);
    Bitmap screenShot = decorView.getDrawingCache(true);
    if (screenShot == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale((float) width / screenShot.getWidth(), (float) height / screenShot.getHeight());
    Bitmap drawingCache = Bitmap.createBitmap(screenShot, 0, 0, screenShot.getWidth(), screenShot.getHeight(),
            matrix, true);
    decorView.destroyDrawingCache();
    screenShot.recycle();
    return drawingCache;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
    if (bitmap == null)
        return bitmap;
    int bitmapWidth = bitmap.getWidth();
    int bitmapHeight = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) width / bitmapWidth);
    float scaleHeight;
    if (height == 0)
        scaleHeight = scaleWidth;/*from  w w w .  j  a v  a  2 s  .  c o  m*/
    else
        scaleHeight = ((float) height / bitmapHeight);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
    return newbmp;
}

From source file:Main.java

public static Bitmap rotateImage(@NonNull Bitmap in, int angle) {
    Matrix mat = new Matrix();
    mat.postRotate(angle);//from  w w w. j  a v a2s.c  om
    return Bitmap.createBitmap(in, 0, 0, in.getWidth(), in.getHeight(), mat, true);
}

From source file:Main.java

/**
 * This is an expensive operation that copies the image in place with the pixels rotated. If
 * possible rather use getOrientationMatrix, and put that as the imageMatrix on an ImageView.
 *
 * @param imageToOrient   Image Bitmap to orient.
 * @param degreesToRotate number of degrees to rotate the image by. If zero the original image is
 *                        returned unmodified.
 * @return The oriented bitmap. May be the imageToOrient without modification, or a new Bitmap.
 *///  ww  w .ja v a  2s.c  om
public static Bitmap rotateImage(@NonNull Bitmap imageToOrient, int degreesToRotate) {
    Bitmap result = imageToOrient;
    try {
        if (degreesToRotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(degreesToRotate);
            result = Bitmap.createBitmap(imageToOrient, 0, 0, imageToOrient.getWidth(),
                    imageToOrient.getHeight(), matrix, true /*filter*/);
        }
    } catch (Exception e) {
        if (Log.isLoggable(TAG, Log.ERROR)) {
            Log.e(TAG, "Exception when trying to orient image", e);
        }
    }
    return result;
}