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:fi.tuukka.weather.utils.Utils.java

public static void showImage(Activity activity, View view, Bitmap bmp) {
    final Dialog imageDialog = new Dialog(activity);
    imageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    imageDialog.setContentView(R.layout.showimage);
    imageDialog.setCancelable(true);//from  w  w  w  . j  a v  a 2s .  c om

    ImageView imageView = (ImageView) imageDialog.findViewById(R.id.imageView);
    // Getting width & height of the given image.
    DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
    int wn = displayMetrics.widthPixels;
    int hn = displayMetrics.heightPixels;
    int wo = bmp.getWidth();
    int ho = bmp.getHeight();
    Matrix mtx = new Matrix();
    // Setting rotate to 90
    mtx.preRotate(90);
    // Setting resize
    mtx.postScale(((float) 1.3 * wn) / ho, ((float) 1.3 * hn) / wo);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, wo, ho, mtx, true);
    BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

    imageView.setImageDrawable(bmd);

    imageView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View button) {
            imageDialog.dismiss();
        }
    });

    imageDialog.show();
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

/**
 * This function calculates a to Screen Image Transformation matrix
 *
 * @param reflectRotation set true if you want the rotation encoded
 * @return Screen to Image transformation matrix
 *///from  ww  w. j  a v a2s.c om
protected Matrix getScreenToImageMatrix(boolean reflectRotation) {
    Matrix m = getImageToScreenMatrix(reflectRotation);
    Matrix invert = new Matrix();
    m.invert(invert);
    return invert;
}

From source file:chrisrenke.drawerarrowdrawable.DrawerArrowDrawable.java

/**
 * Scales the paths to the given screen density. If the density matches the
 * {@link DrawerArrowDrawable#PATH_GEN_DENSITY}, no scaling needs to be done.
 *//*from   w  w w. j av  a2  s .c o m*/
private static void scalePath(Path path, float density) {
    if (density == PATH_GEN_DENSITY)
        return;
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(density / PATH_GEN_DENSITY, density / PATH_GEN_DENSITY, 0, 0);
    path.transform(scaleMatrix);
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * /*from   www  . j  ava2 s  . c  om*/
 *
 * @param angle  angle
 * @param bitmap bitmap
 * @return Bitmap
 */
private static Bitmap rotateImageView(int angle, Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

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

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    if (src != null) {
        try {/*from  w ww. j  a  v a 2 s  .  c  o  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();
            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.scigames.registration.Registration4PhotoActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        or.setVisibility(View.VISIBLE);
        retakeButton.setVisibility(View.VISIBLE);
        saveButton.setVisibility(View.VISIBLE);
        takePhotoButton.setVisibility(View.INVISIBLE);

        if (data.hasExtra("data")) {
            //change to new view
            photo = (Bitmap) data.getExtras().get("data");
            //photoUri = data.getExtras().get("data");
            Log.d(TAG, "photoDensity: ");
            Log.d(TAG, String.valueOf(photo.getDensity()));
            Log.d(TAG, "photo getHeight:");
            Log.d(TAG, String.valueOf(photo.getHeight()));
            Log.d(TAG, "photo getWidth");
            Log.d(TAG, String.valueOf(photo.getWidth()));
            Log.d(TAG, "photo config:");
            if (photo.getHeight() < photo.getWidth()) {
                Log.d(TAG, "height < width");

                Log.d(TAG, photo.getConfig().toString());
                photoToSend = Bitmap.createBitmap(120, 160, photo.getConfig());
                //photoToSend = Bitmap.createBitmap(photo);
                Canvas canvas = new Canvas(photoToSend);
                Log.d(TAG, "photo getScaledHeight:");
                Log.d(TAG, String.valueOf(photo.getScaledHeight(canvas)));
                Log.d(TAG, "photo getScaledWidth");
                Log.d(TAG, String.valueOf(photo.getScaledWidth(canvas)));
                Matrix matrix = new Matrix();
                //matrix.preScale(-1.0f, 1.0f);
                //Bitmap mirroredBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight());
                matrix.setRotate(270, photo.getWidth() / 2, photo.getHeight() / 2);

                //matrix.postTranslate(photo.getWidth(),photo.getHeight());
                Log.d(TAG, matrix.toString());
                canvas.drawBitmap(photo, matrix, new Paint());
            } else {
                Log.d(TAG, photo.getConfig().toString());
                photoToSend = Bitmap.createBitmap(120, 160, photo.getConfig());
                //photoToSend = Bitmap.createBitmap(photo); 
                Canvas canvas = new Canvas(photoToSend);
                Log.d(TAG, "photo getScaledHeight:");
                Log.d(TAG, String.valueOf(photo.getScaledHeight(canvas)));
                Log.d(TAG, "photo getScaledWidth");
                Log.d(TAG, String.valueOf(photo.getScaledWidth(canvas)));
                Matrix matrix = new Matrix();
                //matrix.preScale(-1.0f, 1.0f);
                //Bitmap mirroredBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight());
                //matrix.setRotate(0, photo.getWidth()/2, photo.getHeight()/2);
                //matrix.setScale(0.9f, 0.9f);
                Log.d(TAG, matrix.toString());
                //matrix.postTranslate(photo.getWidth(),photo.getHeight());

                canvas.drawBitmap(photo, matrix, new Paint());
                avatarPhoto.setScaleX(0.95f);
                avatarPhoto.setScaleY(0.95f);
                avatarPhoto.setX(180f);/* www . j av a 2s  .  co m*/
                avatarPhoto.setY(440f);
            }
            avatarPhoto.setImageBitmap(photoToSend);

            Log.d(TAG, "photoToSendDensity: ");
            Log.d(TAG, String.valueOf(photoToSend.getDensity()));
            Log.d(TAG, "photoToSend getScaledHeight:");
            Log.d(TAG, String.valueOf(photoToSend.getHeight()));
            Log.d(TAG, "photoToSend getScaledWidth");
            Log.d(TAG, String.valueOf(photoToSend.getWidth()));
        } else {
            or.setVisibility(View.INVISIBLE);
            saveButton.setVisibility(View.INVISIBLE);
            retakeButton.setVisibility(View.INVISIBLE);
            takePhotoButton.setVisibility(View.VISIBLE);
            //instruction.setText("");
        }
    }
}

From source file:mobisocial.musubi.util.UriImage.java

/**
 * Returns the bytes for this UriImage. If the uri for the image is remote,
 * then this code must not be run on the main thread.
 *///from ww w . j  av a2 s . c  o  m
public byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit, boolean square)
        throws IOException {
    if (!mDecodedBounds) {
        decodeBoundsInfo();
        mDecodedBounds = true;
    }
    InputStream input = null;
    try {
        int inDensity = 0;
        int targetDensity = 0;
        BitmapFactory.Options read_options = new BitmapFactory.Options();
        read_options.inJustDecodeBounds = true;
        input = openInputStream(mUri);
        BitmapFactory.decodeStream(input, null, read_options);
        if (read_options.outWidth > widthLimit || read_options.outHeight > heightLimit) {
            //we need to scale
            if (read_options.outWidth / widthLimit > read_options.outHeight / heightLimit) {
                //width is the large edge
                if (read_options.outWidth * heightLimit > widthLimit * read_options.outHeight) {
                    //incoming image is wider than target
                    inDensity = read_options.outWidth;
                    targetDensity = widthLimit;
                } else {
                    //incoming image is taller than target
                    inDensity = read_options.outHeight;
                    targetDensity = heightLimit;

                }
            } else {
                //height is the long edge, swap the limits
                if (read_options.outWidth * widthLimit > heightLimit * read_options.outHeight) {
                    //incoming image is wider than target
                    inDensity = read_options.outWidth;
                    targetDensity = heightLimit;
                } else {
                    //incoming image is taller than target
                    inDensity = read_options.outHeight;
                    targetDensity = widthLimit;

                }
            }
        } else {
            //no scale
            if (read_options.outWidth > read_options.outHeight) {
                inDensity = targetDensity = read_options.outWidth;
            } else {
                inDensity = targetDensity = read_options.outHeight;
            }
        }

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG,
                    "getResizedImageData: wlimit=" + widthLimit + ", hlimit=" + heightLimit + ", sizeLimit="
                            + byteLimit + ", mWidth=" + mWidth + ", mHeight=" + mHeight + ", initialRatio="
                            + targetDensity + "/" + inDensity);
        }

        ByteArrayOutputStream os = null;
        int attempts = 1;

        int lowMemoryReduce = 1;
        do {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDensity = inDensity;
            options.inSampleSize = lowMemoryReduce;
            options.inScaled = lowMemoryReduce == 1;
            options.inTargetDensity = targetDensity;
            //no purgeable because we are only trying to resave this
            if (input != null)
                input.close();
            input = openInputStream(mUri);
            int quality = IMAGE_COMPRESSION_QUALITY;
            try {
                Bitmap b = BitmapFactory.decodeStream(input, null, options);
                if (b == null) {
                    return null;
                }
                if (options.outWidth > widthLimit + 1 || options.outHeight > heightLimit + 1) {
                    // The decoder does not support the inSampleSize option.
                    // Scale the bitmap using Bitmap library.
                    int scaledWidth;
                    int scaledHeight;
                    scaledWidth = options.outWidth * targetDensity / inDensity;
                    scaledHeight = options.outHeight * targetDensity / inDensity;

                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "getResizedImageData: retry scaling using " + "Bitmap.createScaledBitmap: w="
                                + scaledWidth + ", h=" + scaledHeight);
                    }

                    if (square) {
                        int w = b.getWidth();
                        int h = b.getHeight();
                        int dim = Math.min(w, h);
                        b = Bitmap.createBitmap(b, (w - dim) / 2, (h - dim) / 2, dim, dim);
                        scaledWidth = dim;
                        scaledHeight = dim;
                    }
                    Bitmap b2 = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false);
                    b.recycle();
                    b = b2;
                    if (b == null) {
                        return null;
                    }
                }

                Matrix matrix = new Matrix();
                if (mRotation != 0f) {
                    matrix.preRotate(mRotation);
                }

                Bitmap old = b;
                b = Bitmap.createBitmap(old, 0, 0, old.getWidth(), old.getHeight(), matrix, true);

                // Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY.
                // In case that the image byte size is still too large reduce the quality in
                // proportion to the desired byte size. Should the quality fall below
                // MINIMUM_IMAGE_COMPRESSION_QUALITY skip a compression attempt and we will enter
                // the next round with a smaller image to start with.
                os = new ByteArrayOutputStream();
                b.compress(CompressFormat.JPEG, quality, os);
                int jpgFileSize = os.size();
                if (jpgFileSize > byteLimit) {
                    int reducedQuality = quality * byteLimit / jpgFileSize;
                    //always try to squish it before computing the new size
                    if (reducedQuality < MINIMUM_IMAGE_COMPRESSION_QUALITY) {
                        reducedQuality = MINIMUM_IMAGE_COMPRESSION_QUALITY;
                    }
                    quality = reducedQuality;

                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "getResizedImageData: compress(2) w/ quality=" + quality);
                    }

                    os = new ByteArrayOutputStream();
                    b.compress(CompressFormat.JPEG, quality, os);
                }
                b.recycle(); // done with the bitmap, release the memory
            } catch (java.lang.OutOfMemoryError e) {
                Log.w(TAG, "getResizedImageData - image too big (OutOfMemoryError), will try "
                        + " with smaller scale factor, cur scale factor", e);
                lowMemoryReduce *= 2;
                // fall through and keep trying with a smaller scale factor.
            }
            if (true || Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG,
                        "attempt=" + attempts + " size=" + (os == null ? 0 : os.size()) + " width="
                                + options.outWidth + " height=" + options.outHeight + " Ratio=" + targetDensity
                                + "/" + inDensity + " quality=" + quality);
            }
            //move halfway to the target
            targetDensity = (os == null) ? (int) (targetDensity * .8)
                    : (targetDensity * byteLimit / os.size() + targetDensity) / 2;
            attempts++;
        } while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS);

        return os == null ? null : os.toByteArray();
    } catch (Throwable t) {
        Log.e(TAG, t.getMessage(), t);
        return null;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
    }
}

From source file:com.p2c.thelife.SettingsFragment.java

private void rotateImage(float angle) {
    ImageView imageView = (ImageView) getActivity().findViewById(R.id.settings_image);

    if (m_updatedBitmap == null) {
        m_updatedBitmap = Utilities.getBitmapFromDrawable(imageView.getDrawable());
    }/*w w w.  j  a  v  a 2  s  .c  o m*/

    // rotate image in memory
    Matrix matrix = new Matrix();
    matrix.setRotate(angle);
    m_updatedBitmap = Bitmap.createBitmap(m_updatedBitmap, 0, 0, m_updatedBitmap.getWidth(),
            m_updatedBitmap.getHeight(), matrix, true);

    // save new image bitmap
    imageView.setImageBitmap(m_updatedBitmap);
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

private void drawHighresImage(Canvas canvas, Bitmap fullHighres) {
    Matrix originalToScreen = MasterImage.getImage().originalImageToScreen();
    if (fullHighres != null && originalToScreen != null) {
        Matrix screenToOriginal = new Matrix();
        originalToScreen.invert(screenToOriginal);
        Rect rBounds = new Rect();
        rBounds.set(MasterImage.getImage().getPartialBounds());
        if (fullHighres != null) {
            originalToScreen.preTranslate(rBounds.left, rBounds.top);
            canvas.clipRect(mImageBounds);
            canvas.drawBitmap(fullHighres, originalToScreen, mPaint);
        }/*from   w  ww .  ja  va2s .  co  m*/
    }
}

From source file:de.wikilab.android.friendica01.Max.java

public static void resizeImage(String pathOfInputImage, String pathOfOutputImage, int dstWidth, int dstHeight) {
    try {//from www  . j a  va 2s  . co  m
        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(pathOfOutputImage);
            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);
    }
}