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 boolean storeImage(Context context, Bitmap bmp, boolean isRotate) {

    // use the current data&time for image file name
    String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date());

    // saved bitmap: full path
    String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS;
    File f = new File(path);

    if (f != null && !f.getParentFile().exists()) {
        f.getParentFile().mkdirs();/* w ww.  ja v a2 s  .co  m*/
    }

    if (isRotate) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postRotate(90);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    try {
        FileOutputStream out = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS);
}

From source file:Main.java

public static Bitmap createCircularClip(Bitmap input, int width, int height) {
    if (input == null)
        return null;

    final int inWidth = input.getWidth();
    final int inHeight = input.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    paint.setAntiAlias(true);/*  w  w  w .  j a v a  2s.co m*/
    final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
    final RectF dstRect = new RectF(0, 0, width, height);
    final Matrix m = new Matrix();
    m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
    canvas.setMatrix(m);
    canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
    return output;
}

From source file:Main.java

/**
 * A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions. This operation
 * is significantly less expensive in terms of memory if a mutable Bitmap with the given dimensions is passed in
 * as well.//from w  ww .  j a va2 s . c o  m
 *
 * @param recycled A mutable Bitmap with dimensions width and height that we can load the cropped portion of toCrop
 *                 into
 * @param toCrop The Bitmap to resize
 * @param width The width of the final Bitmap
 * @param height The height of the final Bitmap
 * @return The resized Bitmap (will be recycled if recycled is not null)
 */
public static Bitmap centerCrop(Bitmap recycled, Bitmap toCrop, int width, int height) {
    if (toCrop == null) {
        return null;
    } else if (toCrop.getWidth() == width && toCrop.getHeight() == height) {
        return toCrop;
    }
    //from ImageView/Bitmap.createScaledBitmap
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ImageView.java
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/Bitmap.java
    final float scale;
    float dx = 0, dy = 0;
    Matrix m = new Matrix();
    if (toCrop.getWidth() * height > width * toCrop.getHeight()) {
        scale = (float) height / (float) toCrop.getHeight();
        dx = (width - toCrop.getWidth() * scale) * 0.5f;
    } else {
        scale = (float) width / (float) toCrop.getWidth();
        dy = (height - toCrop.getHeight() * scale) * 0.5f;
    }

    m.setScale(scale, scale);
    m.postTranslate((int) dx + 0.5f, (int) dy + 0.5f);
    final Bitmap result;
    if (recycled != null) {
        result = recycled;
    } else {
        result = Bitmap.createBitmap(width, height,
                toCrop.getConfig() == null ? Bitmap.Config.ARGB_8888 : toCrop.getConfig());
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint(PAINT_FLAGS);
    canvas.drawBitmap(toCrop, m, paint);
    return result;
}

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 scaleWidht = ((float) w) / width;
    float scaleHeight = ((float) h) / height;
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}

From source file:Main.java

public static int loadTexture(Resources resources, int resource, int internalFormat, boolean flip) {
    int[] textures = s_LOAD_TEXTURE_ID.get();
    if (textures == null) {
        textures = new int[1];
        s_LOAD_TEXTURE_ID.set(textures);
    }//w w w  .  j  a va2  s.  c o  m

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, textures, 0);

    int texture = textures[0];
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    Bitmap bitmap = BitmapFactory.decodeResource(resources, resource);

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    if (flip) {
        Matrix matrix = new Matrix();
        matrix.setScale(1, -1);
        matrix.postTranslate(0, height);
        Bitmap flipBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

        bitmap.recycle();
        bitmap = flipBitmap;
    }

    GLUtils.texImage2D(GL_TEXTURE_2D, 0, internalFormat, bitmap, GL_UNSIGNED_BYTE, 0);

    bitmap.recycle();

    glBindTexture(GL_TEXTURE_2D, 0);

    return texture;
}

From source file:Main.java

public static Bitmap getDesirableBitmap(String imgPath, int desiredSize) {
    try {//  w  w  w  . jav  a 2  s  .c  o  m
        int scale = getDesirableBitmapSampleSize(imgPath, desiredSize);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        Bitmap originalBitmap = BitmapFactory.decodeFile(imgPath, options);

        int rotation = getExifOrientation(imgPath);
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap exifBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            originalBitmap.recycle();
            return exifBitmap;
        }
        return originalBitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        //TODO: Handle
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        //Hmmm...what to do...
    }
    return null;
}

From source file:Main.java

/***
 * Downsize the bitmap to at most the indicated ratio of the max specified size
 * @param bm Bitmap to resize/*from   w w w. ja  v a2 s . c  o  m*/
 * @param maxX pixels of max width
 * @param maxY pixels of max height
 * @param maxRatio The max ratio wrt the display size
 * @return the new bitmap, OR input bitmap if no change needed
 */
public static Bitmap getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio) {

    // we have an starting bitmap object to work from
    if (null == bm) {
        return bm;
    }

    // Get current size and h:w ratio
    int height = bm.getHeight();
    int width = bm.getWidth();

    // ensure bitmap size is valid
    if (0 == height || 0 == width) {
        return bm;
    }

    // What is the height to width ratio - will always be > 0 at this point
    double ratio = height / width;

    // Figure out new max size
    int newHeight = (int) (Math.min(maxX, maxY) * maxRatio);
    int newWidth = (int) (newHeight / ratio);

    // If we don't need to downsize, then return with the original
    if (newHeight >= height && newWidth >= width) {
        return bm;
    }

    // Calculate the scaling factors in both the x and y direction
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap, allowing for failure
    try {
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    } catch (Exception e) {
        return bm;
    }
}

From source file:Main.java

/**
 * create scaled bitmap with required width and height
 *
 * @param srcBitmap/*w  w w.j a  v  a 2 s . co  m*/
 * @param reqWidth
 * @param reqHeight
 * @param recycleOrig
 * @param scaleType
 * @return
 */
public synchronized static Bitmap createBitmap(Bitmap srcBitmap, int reqWidth, int reqHeight,
        boolean recycleOrig, int scaleType) {
    int bitmapWidth = srcBitmap.getWidth();
    int bitmapHeight = srcBitmap.getHeight();
    if (reqWidth == 0)
        reqWidth = bitmapWidth;
    if (reqHeight == 0)
        reqHeight = bitmapHeight;

    //        final Rect srcRect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    //        final Rect dstRect = new Rect(0, 0, reqWidth, reqHeight);
    float scaleWidth = 1;
    float scaleHeight = 1;
    if (scaleType == SCALE_TYPE_FIT_START) {
        scaleWidth = (reqWidth / bitmapWidth < reqHeight / bitmapHeight)
                ? (float) reqWidth / (float) bitmapWidth
                : (float) reqHeight / (float) bitmapHeight;
        scaleHeight = scaleWidth;
    } else if (scaleType == SCALE_TYPE_FIT_XY) {
        scaleWidth = (float) reqWidth / (float) bitmapWidth;
        scaleHeight = (float) reqHeight / (float) bitmapHeight;
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

    if (recycleOrig) {
        srcBitmap.recycle();
    }

    return resizedBitmap;
}

From source file:Main.java

public static Bitmap createCorrectlyRotatedBitmapIfNeeded(Bitmap bitmap, String jpegPath, float scale) {
    // Rotate the image if necessary; all images are shot in LANDSCAPE mode
    try {//from  www.  j a va  2  s . c  o m
        ExifInterface exif = new ExifInterface(jpegPath);

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.d("CashLensUtils", "image has orientation " + Integer.toString(orientation) + ", width "
                + Integer.toString(bitmap.getWidth()) + ", height " + Integer.toString(bitmap.getHeight()));

        Matrix matrix = new Matrix();

        if (scale != 1.0f)
            matrix.preScale(scale, scale);

        // From http://sylvana.net/jpegcrop/exif_orientation.html
        // For convenience, here is what the letter F would look like if it were tagged correctly 
        // and displayed by a program that ignores the orientation tag (thus showing the stored image):
        //   (1)       2      (3)      4         5          (6)          7         (8)
        //
        //   888888  888888      88  88      8888888888  88                  88  8888888888
        //   88          88      88  88      88  88      88  88          88  88      88  88
        //   8888      8888    8888  8888    88          8888888888  8888888888          88
        //   88          88      88  88
        //   88          88  888888  888888

        if (orientation == 3)
            matrix.postRotate(180);
        else if (orientation == 6)
            matrix.postRotate(90);
        else if (orientation == 8)
            matrix.postRotate(-90);

        if (orientation != 1 || scale != 1.0f) {
            // Create a new image with the correct (maybe rotated) width/height
            Bitmap newImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);

            Log.d("CashLensUtils", "created a new image with width " + Integer.toString(newImage.getWidth())
                    + ", height " + Integer.toString(newImage.getHeight()));

            // Replace original image
            return newImage;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//www  .ja  v a  2 s  . c  om
    BitmapFactory.decodeFile(filePath, options);

    String orientString;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        e.printStackTrace();
        return bitmap;
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotationAngle = 90;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotationAngle = 180;
        outHeight = bitmap.getHeight();
        outWidth = bitmap.getWidth();
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotationAngle = 270;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}