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 void createScaledImage(String sourceFile, String destinationFile, int desiredWidth,
        int desiredHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*  w w  w. java 2 s.c om*/
    BitmapFactory.decodeFile(sourceFile, options);

    int srcWidth = options.outWidth;
    int srcHeight = options.outHeight;

    if (desiredWidth > srcWidth) {
        desiredWidth = srcWidth;
    }

    int inSampleSize = 1;
    while (srcWidth / 2 > desiredWidth) {
        srcWidth /= 2;
        srcHeight /= 2;
        inSampleSize *= 2;
    }

    float desiredScale = (float) desiredWidth / srcWidth;

    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = inSampleSize;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(sourceFile, options);

    Matrix matrix = new Matrix();
    matrix.postScale(desiredScale, desiredScale);
    Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(),
            sampledSrcBitmap.getHeight(), matrix, true);
    sampledSrcBitmap = null;

    try {
        FileOutputStream out = new FileOutputStream(destinationFile);
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
        scaledBitmap = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Bitmap getDrawable(String path, int zoom, int mItemwidth, int mItemHerght) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w w  w  .ja v  a  2s  .co m*/
    BitmapFactory.decodeFile(path, options);
    int mWidth = options.outWidth;
    int mHeight = options.outHeight;
    int s = 1;
    while ((mWidth / s > mItemwidth * 2 * zoom) || (mHeight / s > mItemHerght * 2 * zoom)) {
        s *= 2;
    }

    options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.ARGB_8888;
    options.inSampleSize = s;
    Bitmap bm = BitmapFactory.decodeFile(path, options);

    if (bm != null) {
        int h = bm.getHeight();
        int w = bm.getWidth();

        float ft = (float) ((float) w / (float) h);
        float fs = (float) ((float) mItemwidth / (float) mItemHerght);

        int neww = ft >= fs ? mItemwidth * zoom : (int) (mItemHerght * zoom * ft);
        int newh = ft >= fs ? (int) (mItemwidth * zoom / ft) : mItemHerght * zoom;

        float scaleWidth = ((float) neww) / w;
        float scaleHeight = ((float) newh) / h;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
        return bm;
    }
    return null;
}

From source file:Main.java

public static Bitmap resizeBitmapToImageView(String path) {
    Bitmap resizedBitmap = null;//from  w w  w.  ja v a 2 s. c  om
    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

public static Bitmap createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) {
    if (null == srcBitmap) {
        return null;
    }//  www.  j a  va 2 s  .  com

    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    int reflectionWidth = srcBitmap.getWidth();
    int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight);

    if (0 == srcWidth || srcHeight == 0) {
        return null;
    }

    // The matrix
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    try {
        // The reflection bitmap, width is same with original's
        Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight,
                reflectionWidth, reflectionHeight, matrix, false);

        if (null == reflectionBitmap) {
            return null;
        }

        Canvas canvas = new Canvas(reflectionBitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);

        LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmap.getHeight(), 0x70FFFFFF,
                0x00FFFFFF, TileMode.MIRROR);
        paint.setShader(shader);

        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));

        // Draw the linear shader.
        canvas.drawRect(0, 0, reflectionBitmap.getWidth(), reflectionBitmap.getHeight(), paint);

        return reflectionBitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = (float) (w / width);
    float scaleHeight = (float) (h / height);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}

From source file:Main.java

private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) {
    try {/*from   www  .  j a v  a 2s.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(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

@SuppressWarnings("deprecation")
public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) {

    if (scale == 0.0)
        return 0;

    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);
    float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*  ww  w .  j a  va 2s.  co  m*/

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

/**
 * Creates a centered bitmap of the desired size.
 *
 * @param source original bitmap source//from   ww w .j a v  a 2  s.c  o  m
 * @param width targeted width
 * @param height targeted height
 * @param options options used during thumbnail extraction
 */
public static Bitmap crop(Bitmap source, int width, int height, int options) {
    if (source == null) {
        return null;
    }

    float scale;
    if (source.getWidth() < source.getHeight()) {
        scale = width / (float) source.getWidth();
    } else {
        scale = height / (float) source.getHeight();
    }
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    Bitmap thumbnail = transform(matrix, source, width, height, OPTIONS_SCALE_UP | options);
    return thumbnail;
}

From source file:Main.java

public static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) {
    if (source == null)
        return null;

    float scale;/*  ww w.ja va 2  s .com*/
    if (source.getWidth() < source.getHeight())
        scale = width / (float) source.getWidth();
    else
        scale = height / (float) source.getHeight();
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    Bitmap thumbnail = transform(matrix, source, width, height, OPTIONS_SCALE_UP | options);
    return thumbnail;
}

From source file:Main.java

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    if (scaleWidth < scaleHeight) {
        scaleHeight = scaleWidth;//from w  w w  .j  a v  a 2s .c om
    } else {
        scaleWidth = scaleHeight;
    }

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // "recreate" the new bitmap
    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}