Example usage for android.graphics Matrix setScale

List of usage examples for android.graphics Matrix setScale

Introduction

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

Prototype

public void setScale(float sx, float sy) 

Source Link

Document

Set the matrix to scale by sx and sy.

Usage

From source file:com.daiv.android.twitter.services.SendTweet.java

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Log.v("Test_composing_image", "rotation: " + orientation);

    try {//from   ww  w.ja va 2 s .co  m
        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap scaleTo(Matrix scaler, Bitmap source, int targetWidth, int targetHeight,
        boolean fitInScreen) {
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    float scale = 0;
    float scaleH = targetHeight / bitmapHeightF;
    float scaleW = targetWidth / bitmapWidthF;

    if (fitInScreen && scaleH < scaleW) {
        scale = scaleH;/*from   ww w.java2 s  .c o  m*/
    } else if (fitInScreen && scaleH >= scaleW) {
        scale = scaleW;
    } else if (bitmapAspect > viewAspect) {
        scale = scaleH;
    } else {
        scale = scaleW;
    }

    if (scale != 0 && (scale < .9F || scale > 1F)) {
        scaler.setScale(scale, scale);
    } else {
        scaler = null;
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
        source.recycle();
    } else {
        b1 = source;
    }
    return b1;
}

From source file:biz.varkon.shelvesom.util.ImageUtilities.java

private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset,
        boolean clipShadow, Paint paint) {
    Matrix m;
    synchronized (Bitmap.class) {
        m = sScaleMatrix;//from   w  w  w  .  j  a  v a 2  s .  com
        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;
    }

    src.recycle();
    return b;
}

From source file:com.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java

private static Matrix getRotateMatrix(int ori) {
    Matrix matrix = new Matrix();
    switch (ori) {
    case 2:/*from w w  w.j  ava2 s .com*/
        matrix.setScale(-1, 1);
        break;
    case 3:
        matrix.setRotate(180);
        break;
    case 4:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case 5:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case 6:
        matrix.setRotate(90);
        break;
    case 7:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case 8:
        matrix.setRotate(-90);
        break;
    }
    return matrix;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    if (src != null) {
        try {//from w ww  .  ja  va2  s. c  om
            // 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();
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.postRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.postRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.postRotate(270);
                break;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                matrix.postTranslate(src.getWidth(), 0);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setScale(1, -1);
                matrix.postTranslate(0, src.getHeight());
                break;
            }
            // Rotate the bitmap
            return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        } catch (IOException e) {
            // Ignore
        }
    }
    return src;
}

From source file:com.appbase.androidquery.callback.BitmapAjaxCallback.java

private static Matrix getRotateMatrix(int ori) {

    Matrix matrix = new Matrix();
    switch (ori) {
    case 2:/*from   w w  w . j a  v  a2  s  .co  m*/
        matrix.setScale(-1, 1);
        break;
    case 3:
        matrix.setRotate(180);
        break;
    case 4:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case 5:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case 6:
        matrix.setRotate(90);
        break;
    case 7:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case 8:
        matrix.setRotate(-90);
        break;

    }

    return matrix;

}

From source file:com.fastaccess.tfl.ui.widget.drag.DragView.java

public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, int left, int top,
        int width, int height) {
    super(context);
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Matrix scale = new Matrix();
    float scaleFactor = width;
    scaleFactor = mScale = (scaleFactor + DRAG_SCALE) / scaleFactor;
    scale.setScale(scaleFactor, scaleFactor);
    mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
    mRegistrationX = registrationX + (DRAG_SCALE / 2);
    mRegistrationY = registrationY + (DRAG_SCALE / 2);
}

From source file:com.nextgis.maplibui.formcontrol.Sign.java

public void save(int width, int height, boolean transparentBackground, File sigFile) throws IOException {
    if (mNotInitialized)
        return;//from   w  ww .j a  v  a  2 s.c  o m

    float scale = Math.min((float) width / getWidth(), (float) height / getHeight());
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    canvas.setMatrix(matrix);

    int color = transparentBackground ? Color.TRANSPARENT : 0xFFFFFF - mPaint.getColor();
    drawSign(canvas, color, mPaint);
    if (sigFile.exists() || sigFile.createNewFile()) {
        FileOutputStream out = new FileOutputStream(sigFile);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
    }
}

From source file:com.stanleyidesis.quotograph.api.controller.LWQNotificationControllerImpl.java

private Bitmap chopToCenterSquare(Bitmap original) {
    int squareDim = Math.min(original.getWidth(), original.getHeight());
    int squareYCoord = (int) (original.getHeight() / 2.0) - (int) (squareDim / 2.0);
    int squareXCoord = (int) (original.getWidth() / 2.0) - (int) (squareDim / 2.0);

    final Point size = UIUtils.getRealScreenSize();
    int screenWidth = Math.min(size.x, size.y);
    int maxBitmapDim = (int) (screenWidth * .3f);
    // Calculate center
    float scale = ((float) maxBitmapDim) / ((float) squareDim);
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    return Bitmap.createBitmap(original, squareXCoord, squareYCoord, squareDim, squareDim, matrix, true);
}

From source file:com.example.linhdq.test.documents.creation.crop.CropImageActivity.java

@OnClick(R.id.item_save)
void onSaveClicked() {
    if (!mCropData.isPresent() || mSaving || (mCrop == null)) {
        return;/*from   www  .j a v a  2  s .c  o  m*/
    }
    mSaving = true;

    Util.startBackgroundJob(this, null, getText(R.string.cropping_image).toString(), new Runnable() {
        public void run() {
            try {
                float scale = 1f / mCropData.get().getScaleResult().getScaleFactor();
                Matrix scaleMatrix = new Matrix();
                scaleMatrix.setScale(scale, scale);

                final float[] trapezoid = mCrop.getTrapezoid();
                final RectF perspectiveCorrectedBoundingRect = new RectF(
                        mCrop.getPerspectiveCorrectedBoundingRect());
                scaleMatrix.mapRect(perspectiveCorrectedBoundingRect);
                Box bb = new Box((int) perspectiveCorrectedBoundingRect.left,
                        (int) perspectiveCorrectedBoundingRect.top,
                        (int) perspectiveCorrectedBoundingRect.width(),
                        (int) perspectiveCorrectedBoundingRect.height());

                Pix pix8 = Convert.convertTo8(mPix);
                mPix.recycle();

                Pix croppedPix = Clip.clipRectangle2(pix8, bb);
                if (croppedPix == null) {
                    throw new IllegalStateException();
                }
                pix8.recycle();

                scaleMatrix.postTranslate(-bb.getX(), -bb.getY());
                scaleMatrix.mapPoints(trapezoid);

                final float[] dest = new float[] { 0, 0, bb.getWidth(), 0, bb.getWidth(), bb.getHeight(), 0,
                        bb.getHeight() };
                Pix bilinear = Projective.projectiveTransform(croppedPix, dest, trapezoid);
                if (bilinear == null) {
                    bilinear = croppedPix;
                } else {
                    croppedPix.recycle();
                }

                if (mRotation != 0 && mRotation != 4) {
                    Pix rotatedPix = Rotate.rotateOrth(bilinear, mRotation);
                    bilinear.recycle();
                    bilinear = rotatedPix;
                }
                if (bilinear == null) {
                    throw new IllegalStateException();
                }
                Intent result = new Intent();
                OCR.savePixToCacheDir(CropImageActivity.this, bilinear.copy());
                result.putExtra(DocumentGridActivity.EXTRA_NATIVE_PIX, bilinear.getNativePix());
                setResult(RESULT_OK, result);
            } catch (IllegalStateException e) {
                setResult(RESULT_CANCELED);
            } finally {
                finish();
            }
        }
    }, mHandler);

}