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 rotateBitmap(Bitmap source, int angle) {
    Matrix matrix = new Matrix();
    if (angle > 0)
        matrix.setRotate(angle);/*ww  w  .  j a va 2 s .com*/
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    if (bitmap != source && !source.isRecycled())
        source.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap formatBitmap(Bitmap bitmap, int size) {

    float width = bitmap.getWidth();
    float height = bitmap.getHeight();
    float rota = size / height;

    int widthnew = (int) ((size * width) / height);
    // int wh = Math.max(bitmap.getWidth(), bitmap.getHeight());
    Bitmap mBitmap = Bitmap.createBitmap(widthnew, size, Bitmap.Config.ARGB_8888);
    Matrix matrix = new Matrix();
    matrix.postScale(rota, rota);/*from   ww w . jav  a 2  s . c  o m*/
    Canvas canvas = new Canvas(mBitmap);
    canvas.drawBitmap(bitmap, matrix, null);

    // bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
    return mBitmap;
}

From source file:com.BeeFramework.Utils.Utils.java

public static Bitmap scaleBitmap(Bitmap bm, int pixel) {
    int srcHeight = bm.getHeight();
    int srcWidth = bm.getWidth();

    if (srcHeight > pixel || srcWidth > pixel) {
        float scale_y = 0;
        float scale_x = 0;
        if (srcHeight > srcWidth) {
            scale_y = ((float) pixel) / srcHeight;
            scale_x = scale_y;//from   w w  w  .  j a  va  2s.c  o m
        } else {
            scale_x = ((float) pixel) / srcWidth;
            scale_y = scale_x;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(scale_x, scale_y);
        Bitmap dstbmp = Bitmap.createBitmap(bm, 0, 0, srcWidth, srcHeight, matrix, true);
        return dstbmp;
    } else {
        return Bitmap.createBitmap(bm);
    }
}

From source file:Main.java

private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
    // If the rotate angle is 0, then return the original image, else return the rotated image
    if (angle != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);/*from  w w  w.  j  a va 2  s  . com*/
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } else {
        return bitmap;
    }
}

From source file:Main.java

private static Bitmap scale(Bitmap in, int size) {
    int width = in.getWidth();
    int height = in.getHeight();
    float scalew;
    float scaleh;
    Matrix matrix = new Matrix();
    //scale smaller axis to size (if necessary)
    if (width > height) {
        scaleh = (float) size / height;
        scalew = scaleh;//from w w  w . j av a 2  s  .co m
        if ((width * scalew) % 2 != 0) {
            scalew = ((width * scalew) + 1) / (float) width;
        }

    } else {
        scalew = (float) size / width;
        scaleh = scalew;
        if ((height * scaleh) % 2 != 0) {
            scaleh = ((height * scaleh) + 1) / (float) height;
        }
    }
    Log.i("RESIZING", "old width: " + width + "old height: " + height + "new size: " + size);
    matrix.postScale(scalew, scaleh);
    Bitmap scaledPic = Bitmap.createBitmap(in, 0, 0, width, height, matrix, true);
    return scaledPic;
}

From source file:Main.java

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    try {/* w ww.j  a v a  2  s  .co  m*/
        // Try to load the bitmap as a bitmap file
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                || orientation == ExifInterface.ORIENTATION_NORMAL) {
            return src;
        }
        Matrix matrix = new Matrix();
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.setScale(-1, 1);
            matrix.postTranslate(src.getWidth(), 0);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.setScale(1, -1);
            matrix.postTranslate(0, src.getHeight());
        }
        // Rotate the bitmap
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    } catch (IOException e) {
        // Ignore
    }
    return src;
}

From source file:Main.java

/**
 * // w ww .j  ava 2s. com
 * @param filePath
 * @param targetWidth
 * @param targetHeight
 * @param recycle
 * @return
 */
public static Bitmap resizeAndCropCenter(String filePath, int targetWidth, int targetHeight, boolean recycle) {
    //get sampleBitmap
    Options opts = new Options();
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, opts);

    int scaleWidth = opts.outWidth / targetWidth;
    int scaleHeight = opts.outHeight / targetHeight;
    int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
    if (scale < 1) {
        scale = 1;
    }
    opts.inJustDecodeBounds = false;
    opts.inSampleSize = scale;
    Bitmap sampleBitmap = BitmapFactory.decodeFile(filePath, opts);

    if (opts.outWidth == -1 || opts.outHeight == -1) {
        throw new IllegalArgumentException();
    }
    //get scalebitmap
    float fScaleWidth = targetWidth / ((float) opts.outWidth);
    float fScaleHeight = targetHeight / ((float) opts.outHeight);
    float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight;
    if (fScale > 1)
        fScale = 1;
    Matrix matrix = new Matrix();
    matrix.postScale(fScale, fScale);
    Bitmap scaleBitmap = Bitmap.createBitmap(sampleBitmap, 0, 0, opts.outWidth, opts.outHeight, matrix, true);

    //get targetBitmap
    int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2;
    bitmapX = bitmapX > 0 ? bitmapX : 0;
    int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2;
    bitmapY = bitmapY > 0 ? bitmapY : 0;
    targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth());
    targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight());
    Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight);

    if (recycle)
        sampleBitmap.recycle();
    return targetBitmap;
}

From source file:Main.java

private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset,
        boolean clipShadow, Paint paint) {

    Matrix m;//w  w  w .  jav a  2  s. c  o m
    synchronized (Bitmap.class) {
        m = sScaleMatrix;
        sScaleMatrix = null;
    }

    if (m == null) {
        m = new Matrix();
    }

    final int width = src.getWidth();
    final int height = src.getHeight();
    final float sx = dstWidth / (float) width;
    final float sy = dstHeight / (float) height;
    m.setScale(sx, sy);

    Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);

    synchronized (Bitmap.class) {
        sScaleMatrix = m;
    }

    return b;
}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {/*  w  ww .j a  v a  2 s .  c om*/
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}

From source file:com.BeeFramework.Utils.Utils.java

public static Bitmap scaleBitmap(Bitmap bm, int dstHeight, int dstWidth) {
    if (bm == null)
        return null;//java.lang.NullPointerException
    int srcHeight = bm.getHeight();
    int srcWidth = bm.getWidth();
    if (srcHeight > dstHeight || srcWidth > dstWidth) {
        float scale_y = ((float) dstHeight) / srcHeight;
        float scale_x = ((float) dstWidth) / srcWidth;
        float scale = scale_y < scale_x ? scale_y : scale_x;
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);/*from   w  ww  .ja v a2 s  .c om*/
        Bitmap dstbmp = Bitmap.createBitmap(bm, 0, 0, srcWidth, srcHeight, matrix, true);
        return dstbmp;
    } else {
        return Bitmap.createBitmap(bm);
    }
}