List of usage examples for android.graphics Matrix Matrix
public Matrix()
From source file:Main.java
@SuppressWarnings("deprecation") public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y, int newHeight) { float scale = (float) newHeight / (float) image.getIntrinsicHeight(); float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale); Bitmap bmp = ((BitmapDrawable) image).getBitmap(); int width = bmp.getWidth(); int height = bmp.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap matrix.postRotate((float) Angle); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight()); bmd.draw(canvas);/*from w w w . j a v a 2 s . c o m*/ return bmd.getIntrinsicWidth(); }
From source file:Main.java
public static Bitmap getResizedBitmap(Bitmap src, int targetWidth, int targetHeight, float degrees) { int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); float scale = getFitScale(targetWidth, targetHeight, srcWidth, srcHeight); Matrix matrix = new Matrix(); matrix.postScale(scale, scale);// w w w . jav a 2 s . co m matrix.postRotate(degrees); return Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix, true); }
From source file:Main.java
public static Bitmap rotateBitmap(Bitmap source, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle);/*from w ww.j a v a 2 s. c o m*/ return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); }
From source file:Main.java
/** * Resize a bitmap object to fit the passed width and height * // w w w. ja va 2s .co m * @param input * The bitmap to be resized * @param destWidth * Desired maximum width of the result bitmap * @param destHeight * Desired maximum height of the result bitmap * @return A new resized bitmap * @throws OutOfMemoryError * if the operation exceeds the available vm memory */ public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation) throws OutOfMemoryError, Exception { int dstWidth = destWidth; int dstHeight = destHeight; final int srcWidth = input.getWidth(); final int srcHeight = input.getHeight(); if (rotation == 90 || rotation == 270) { dstWidth = destHeight; dstHeight = destWidth; } boolean needsResize = false; float p; if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) { needsResize = true; if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) { p = (float) dstWidth / (float) srcWidth; dstHeight = (int) (srcHeight * p); } else { p = (float) dstHeight / (float) srcHeight; dstWidth = (int) (srcWidth * p); } } else { dstWidth = srcWidth; dstHeight = srcHeight; } if (needsResize || rotation != 0) { Bitmap output; if (rotation == 0) { output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true); } else { Matrix matrix = new Matrix(); matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight); matrix.postRotate(rotation); output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true); } return output; } else return input; }
From source file:Main.java
public static Bitmap resizeBitmap(Bitmap input, int destWidth, int destHeight, int rotation) throws OutOfMemoryError { int dstWidth = destWidth; int dstHeight = destHeight; int srcWidth = input.getWidth(); int srcHeight = input.getHeight(); if ((rotation == 90) || (rotation == 270)) { dstWidth = destHeight;/* w w w. j a v a 2s.c o m*/ dstHeight = destWidth; } boolean needsResize = false; if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) { needsResize = true; float ratio1 = (float) srcWidth / dstWidth; float ratio2 = (float) srcHeight / dstHeight; Log.v("dsd", "ratio1:" + ratio1 + " ratio2:" + ratio2); if (ratio1 > ratio2) { float p = (float) dstWidth / srcWidth; dstHeight = (int) (srcHeight * p); } else { float p = (float) dstHeight / srcHeight; dstWidth = (int) (srcWidth * p); } } else { dstWidth = srcWidth; dstHeight = srcHeight; } Log.v("dsd", "dstWidth:" + dstWidth + " dstHeight:" + dstHeight + " srcWidth:" + srcWidth + " srcHeight:" + srcHeight); if ((needsResize) || (rotation != 0)) { Bitmap output = null; if (rotation == 0) { output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true); } else { Matrix matrix = new Matrix(); matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight); matrix.postRotate(rotation); output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true); } return output; } return input; }
From source file:Main.java
private static Bitmap createScaledBitmap(File f, int rotation, int scale) throws FileNotFoundException { BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale;//from w w w . java 2 s . co m Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); if (0 < rotation) { Matrix matrix = new Matrix(); matrix.postRotate(rotation); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (rotatedBitmap != bitmap) { bitmap.recycle(); bitmap = null; } return rotatedBitmap; } return bitmap; }
From source file:Main.java
public static Bitmap rotateBitmap(String path, int orientation, int screenWidth, int screenHeight) { Bitmap bitmap = null;// www .ja v a 2 s .c o m final int maxWidth = screenWidth / 2; final int maxHeight = screenHeight / 2; try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int sourceWidth, sourceHeight; if (orientation == 90 || orientation == 270) { sourceWidth = options.outHeight; sourceHeight = options.outWidth; } else { sourceWidth = options.outWidth; sourceHeight = options.outHeight; } boolean compress = false; if (sourceWidth > maxWidth || sourceHeight > maxHeight) { float widthRatio = (float) sourceWidth / (float) maxWidth; float heightRatio = (float) sourceHeight / (float) maxHeight; options.inJustDecodeBounds = false; if (new File(path).length() > 512000) { float maxRatio = Math.max(widthRatio, heightRatio); options.inSampleSize = (int) maxRatio; compress = true; } bitmap = BitmapFactory.decodeFile(path, options); } else { bitmap = BitmapFactory.decodeFile(path); } if (orientation > 0) { Matrix matrix = new Matrix(); //matrix.postScale(sourceWidth, sourceHeight); matrix.postRotate(orientation); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } sourceWidth = bitmap.getWidth(); sourceHeight = bitmap.getHeight(); if ((sourceWidth > maxWidth || sourceHeight > maxHeight) && compress) { float widthRatio = (float) sourceWidth / (float) maxWidth; float heightRatio = (float) sourceHeight / (float) maxHeight; float maxRatio = Math.max(widthRatio, heightRatio); sourceWidth = (int) ((float) sourceWidth / maxRatio); sourceHeight = (int) ((float) sourceHeight / maxRatio); Bitmap bm = Bitmap.createScaledBitmap(bitmap, sourceWidth, sourceHeight, true); bitmap.recycle(); return bm; } } catch (Exception e) { } return bitmap; }
From source file:Main.java
private static Bitmap scaleBitmap(Bitmap bitmap, float ratio) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(1f / ratio, 1f / ratio); Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return result; }
From source file:Main.java
/** * Resize a bitmap object to fit the passed width and height * * @param input//from ww w. ja v a 2s . com * The bitmap to be resized * @param destWidth * Desired maximum width of the result bitmap * @param destHeight * Desired maximum height of the result bitmap * @return A new resized bitmap * @throws OutOfMemoryError * if the operation exceeds the available vm memory */ public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation) throws OutOfMemoryError { int dstWidth = destWidth; int dstHeight = destHeight; final int srcWidth = input.getWidth(); final int srcHeight = input.getHeight(); if (rotation == 90 || rotation == 270) { dstWidth = destHeight; dstHeight = destWidth; } boolean needsResize = false; float p; if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) { needsResize = true; if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) { p = (float) dstWidth / (float) srcWidth; dstHeight = (int) (srcHeight * p); } else { p = (float) dstHeight / (float) srcHeight; dstWidth = (int) (srcWidth * p); } } else { dstWidth = srcWidth; dstHeight = srcHeight; } if (needsResize || rotation != 0) { Bitmap output; if (rotation == 0) { output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true); } else { Matrix matrix = new Matrix(); matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight); matrix.postRotate(rotation); output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true); } return output; } else return input; }
From source file:Main.java
public static Bitmap imageWithFixedRotation(Bitmap bm, int degrees) { if (bm == null || bm.isRecycled()) return null; if (degrees == 0) return bm; final Matrix matrix = new Matrix(); matrix.postRotate(degrees);/*from w w w. jav a 2 s . c o m*/ Bitmap result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); if (result != bm) bm.recycle(); return result; }