Example usage for android.graphics Matrix mapRect

List of usage examples for android.graphics Matrix mapRect

Introduction

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

Prototype

public boolean mapRect(RectF dst, RectF src) 

Source Link

Document

Apply this matrix to the src rectangle, and write the transformed rectangle into dst.

Usage

From source file:Main.java

public static boolean setImageToScreenMatrix(Matrix dst, RectF image, RectF screen, int rotation) {
    RectF rotatedImage = new RectF();
    dst.setRotate(rotation, image.centerX(), image.centerY());
    if (!dst.mapRect(rotatedImage, image)) {
        return false; // fails for rotations that are not multiples of 90
                      // degrees
    }/*w  ww  . j a v a  2s  .  co  m*/
    boolean rToR = dst.setRectToRect(rotatedImage, screen, Matrix.ScaleToFit.CENTER);
    boolean rot = dst.preRotate(rotation, image.centerX(), image.centerY());
    return rToR && rot;
}

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;/*from www  .ja va 2  s .  c o  m*/

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:com.jafme.mobile.activity.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();/*w  w w  .j  a  va  2  s  .  c  o m*/

    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();

        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);

            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));

            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right,
                    (int) adjusted.bottom);
        }

        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());

            //  ?  

            if (exifRotation != 0) {

                final Matrix matrix = new Matrix();
                matrix.setRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);

                exifRotation = 0;
            }

            int croppedWidth = croppedImage.getWidth();
            int croppedHeight = croppedImage.getHeight();
            if (croppedWidth > outWidth || croppedHeight > outHeight) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / croppedWidth, (float) outHeight / croppedHeight);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedWidth, croppedHeight, matrix,
                        true);
            }

        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + ","
                    + height + "," + exifRotation + ")", e);
        }

    } catch (IOException e) {
        Log.e(LOG_TAG, "Error cropping image: " + e.getMessage(), e);
        finish();
    } catch (OutOfMemoryError e) {
        Log.e(LOG_TAG, "OOM cropping image: " + e.getMessage(), e);
        setResultException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}

From source file:com.futurologeek.smartcrossing.crop.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();/*from  w w  w . j  ava2s  . com*/

    InputStream is = null;
    Bitmap croppedImage = null;
    try {
        is = getContentResolver().openInputStream(sourceUri);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
        final int width = decoder.getWidth();
        final int height = decoder.getHeight();

        if (exifRotation != 0) {
            // Adjust crop area to account for image rotation
            Matrix matrix = new Matrix();
            matrix.setRotate(-exifRotation);

            RectF adjusted = new RectF();
            matrix.mapRect(adjusted, new RectF(rect));

            //if the cutting box are rectangle( outWidth != outHeight ),and the exifRotation is 90 or 270,
            //the outWidth and outHeight showld be interchanged
            if (exifRotation == 90 || exifRotation == 270) {
                int temp = outWidth;
                outWidth = outHeight;
                outHeight = temp;
            }

            // Adjust to account for origin at 0,0
            adjusted.offset(adjusted.left < 0 ? width : 0, adjusted.top < 0 ? height : 0);
            rect = new Rect((int) adjusted.left, (int) adjusted.top, (int) adjusted.right,
                    (int) adjusted.bottom);
        }

        try {
            croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
            if (rect.width() > outWidth || rect.height() > outHeight) {
                Matrix matrix = new Matrix();
                matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());

                //if the picture's exifRotation !=0 ,they should be rotate to 0 degrees
                matrix.postRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);
            } else {
                //if the picture need not to be scale, they also neet to be rotate to 0 degrees
                Matrix matrix = new Matrix();
                matrix.postRotate(exifRotation);
                croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(),
                        croppedImage.getHeight(), matrix, true);
            }
        } catch (IllegalArgumentException e) {
            // Rethrow with some extra information
            throw new IllegalArgumentException("Rectangle " + rect + " is outside of the image (" + width + ","
                    + height + "," + exifRotation + ")", e);
        }

    } catch (IOException e) {
        Log.e("Error cropping image: " + e.getMessage(), e);
        finish();
    } catch (OutOfMemoryError e) {
        Log.e("OOM cropping image: " + e.getMessage(), e);
        setResultException(e);
    } finally {
        CropUtil.closeSilently(is);
    }
    return croppedImage;
}

From source file:com.goka.flickableview.ImageViewTouchBase.java

protected RectF getBitmapRect(Matrix supportMatrix) {
    Matrix m = getImageViewMatrix(supportMatrix);
    m.mapRect(mBitmapRectTmp, mBitmapRect);
    return mBitmapRectTmp;
}