Example usage for android.graphics Matrix postScale

List of usage examples for android.graphics Matrix postScale

Introduction

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

Prototype

public boolean postScale(float sx, float sy) 

Source Link

Document

Postconcats the matrix with the specified scale.

Usage

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
 * Shrinks the passed image file spec into the specificed dimensions, and returns the bitmap. If the view 
 * is non-null, the image is also placed in the view.
 * //from   www  .  ja  v  a 2 s  . c o  m
 * @param destView
 * @param filename
 * @param maxWidth
 * @param maxHeight
 * @param exact
 * 
 * @return
 */
private static Bitmap shrinkFileIntoImageView(ImageView destView, String filename, int maxWidth, int maxHeight,
        boolean exact) {
    Bitmap bm = null;

    // Read the file to get file size
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, opt);

    // If no size info, or a single pixel, assume file bad and set the 'alert' icon
    if (opt.outHeight <= 0 || opt.outWidth <= 0 || (opt.outHeight == 1 && opt.outWidth == 1)) {
        if (destView != null)
            destView.setImageResource(android.R.drawable.ic_dialog_alert);
        return null;
    }

    // Next time we don't just want the bounds, we want the file
    opt.inJustDecodeBounds = false;

    // Work out how to scale the file to fit in required bbox
    float widthRatio = (float) maxWidth / opt.outWidth;
    float heightRatio = (float) maxHeight / opt.outHeight;

    // Work out scale so that it fit exactly
    float ratio = widthRatio < heightRatio ? widthRatio : heightRatio;

    // Note that inSampleSize seems to ALWAYS be forced to a power of 2, no matter what we
    // specify, so we just work with powers of 2.
    int idealSampleSize = (int) android.util.FloatMath.ceil(1 / ratio); // This is the sample size we want to use
    // Get the nearest *bigger* power of 2.
    int samplePow2 = (int) Math.pow(2, Math.ceil(Math.log(idealSampleSize) / Math.log(2)));

    try {
        if (exact) {
            // Create one bigger than needed and scale it; this is an attempt to improve quality.
            opt.inSampleSize = samplePow2 / 2;
            if (opt.inSampleSize < 1)
                opt.inSampleSize = 1;
            Bitmap tmpBm = BitmapFactory.decodeFile(filename, opt);
            if (tmpBm == null) {
                // We ran out of memory, most likely
                // TODO: Need a way to try loading images after GC(), or something. Otherwise, covers in cover browser wil stay blank.
                Logger.logError(
                        new RuntimeException("Unexpectedly failed to decode bitmap; memory exhausted?"));
                return null;
            }
            android.graphics.Matrix matrix = new android.graphics.Matrix();
            // Fixup ratio based on new sample size and scale it.
            ratio = ratio / (1.0f / opt.inSampleSize);
            matrix.postScale(ratio, ratio);
            bm = Bitmap.createBitmap(tmpBm, 0, 0, opt.outWidth, opt.outHeight, matrix, true);
            // Recycle if original was not returned
            if (bm != tmpBm) {
                tmpBm.recycle();
                tmpBm = null;
            }
        } else {
            // Use a scale that will make image *no larger than* the desired size
            if (ratio < 1.0f)
                opt.inSampleSize = samplePow2;
            bm = BitmapFactory.decodeFile(filename, opt);
        }
    } catch (OutOfMemoryError e) {
        return null;
    }

    // Set ImageView and return bitmap
    if (destView != null)
        destView.setImageBitmap(bm);

    return bm;
}

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

private void addChildScale(View v, Matrix m) {
    final float f = getScaleFactor(getChildsCenter(v));
    m.postScale(f, f);
}

From source file:cl.monsoon.s1next.widget.PhotoView.java

/**
 * Gets a bitmap of the cropped region. If cropping is not enabled, returns {@code null}.
 *///from ww w  .  j  a va2s  .co  m
public Bitmap getCroppedPhoto() {
    if (!mAllowCrop) {
        return null;
    }

    final Bitmap croppedBitmap = Bitmap.createBitmap((int) CROPPED_SIZE, (int) CROPPED_SIZE,
            Bitmap.Config.ARGB_8888);
    final Canvas croppedCanvas = new Canvas(croppedBitmap);

    // scale for the final dimensions
    final int cropWidth = mCropRect.right - mCropRect.left;
    final float scaleWidth = CROPPED_SIZE / cropWidth;
    final float scaleHeight = CROPPED_SIZE / cropWidth;

    // translate to the origin & scale
    final Matrix matrix = new Matrix(mDrawMatrix);
    matrix.postTranslate(-mCropRect.left, -mCropRect.top);
    matrix.postScale(scaleWidth, scaleHeight);

    // draw the photo
    if (mDrawable != null) {
        croppedCanvas.concat(matrix);
        mDrawable.draw(croppedCanvas);
    }

    return croppedBitmap;
}

From source file:com.android.volley.ui.PhotoView.java

/**
 * Gets a bitmap of the cropped region. If cropping is not enabled, returns {@code null}.
 *///from  www.j  av  a2 s .  co  m
public Bitmap getCroppedPhoto() {
    if (!mAllowCrop) {
        return null;
    }

    final Bitmap croppedBitmap = Bitmap.createBitmap((int) CROPPED_SIZE, (int) CROPPED_SIZE,
            Bitmap.Config.ARGB_8888);
    final Canvas croppedCanvas = new Canvas(croppedBitmap);

    // scale for the final dimensions
    final int cropWidth = mCropRect.right - mCropRect.left;
    final float scaleWidth = CROPPED_SIZE / cropWidth;
    final float scaleHeight = CROPPED_SIZE / cropWidth;

    // translate to the origin & scale
    final Matrix matrix = new Matrix(mDrawMatrix);
    matrix.postTranslate(-mCropRect.left, -mCropRect.top);
    matrix.postScale(scaleWidth, scaleHeight);

    // draw the photo
    if (mDrawable != null) {
        croppedCanvas.concat(matrix);
        mDrawable.draw(croppedCanvas);
    }
    return croppedBitmap;
}

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

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();//w w  w  .ja  va 2  s  .c  om

    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.jafme.mobile.activity.CropImageActivity.java

private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
    // Release memory now
    clearImageView();/*  w  w  w  .j a  v  a 2 s  .  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));

            // 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:org.de.jmg.learn._MainActivity.java

private Bitmap resizeBM(Bitmap b) {
    if (b != null) {
        int width = Math.max(b.getWidth(), b.getHeight());
        int widthScreen = this.width;
        widthScreen -= lib.dpToPx(40);/*from   w w w .ja  v  a2 s.c om*/
        float fact = (float) widthScreen / (float) width;
        Matrix matrix = new Matrix();
        matrix.postScale(fact, fact);
        return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
    }
    return null;
}

From source file:com.tct.mail.browse.ConversationItemView.java

private Bitmap resizeBitMap(Bitmap bitmap, float width_scale, float height_scal) {
    Matrix matrix = new Matrix();
    matrix.postScale(width_scale, height_scal);
    Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
            true);//from  w  w w . j a va  2 s  .  c o m
    return resizeBitmap;
}

From source file:de.vanita5.twittnuker.util.Utils.java

/**
 * Resizes specific a Bitmap with keeping ratio.
 *//*from  ww w .  j a v  a 2s .  c o m*/
public static Bitmap resizeBitmap(Bitmap orig, final int desireWidth, final int desireHeight) {
    final int width = orig.getWidth();
    final int height = orig.getHeight();

    if (0 < width && 0 < height && desireWidth < width || desireHeight < height) {
        // Calculate scale
        float scale;
        if (width < height) {
            scale = (float) desireHeight / (float) height;
            if (desireWidth < width * scale) {
                scale = (float) desireWidth / (float) width;
            }
        } else {
            scale = (float) desireWidth / (float) width;
        }

        // Draw resized image
        final Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        final Bitmap bitmap = Bitmap.createBitmap(orig, 0, 0, width, height, matrix, true);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(bitmap, 0, 0, null);

        orig = bitmap;
    }

    return orig;
}