android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
复制代码 代码如下:
final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); final BitmapFactory.Options options = new BitmapFactory.Options(); // Calculate inSampleSize // Decode bitmap with inSampleSize set 2.2 处理图片旋转 [java] 复制代码 代码如下:
int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ; int degree = readPictureDegree(filePath); private static int readPictureDegree(String path) { [java] 复制代码 代码如下:
view plaincopyprint?private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){ if(bitmap == null) return null ; int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting post rotate to 90 Matrix mtx = new Matrix(); mtx.postRotate(rotate); return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); } private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){ // Setting post rotate to 90 2.3压缩图片 [java] 复制代码 代码如下:
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0 bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0 完整的方法代码: [java] 复制代码 代码如下:
public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); if(bm == null){ return null; } int degree = readPictureDegree(filePath); bm = rotateBitmap(bm,degree) ; ByteArrayOutputStream baos = null ; try{ baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 30, baos); }finally{ try { if(baos != null) baos.close() ; } catch (IOException e) { e.printStackTrace(); } } return bm ; } public static Bitmap getSmallBitmap(String filePath) { // Calculate inSampleSize // Decode bitmap with inSampleSize set } [java] 复制代码 代码如下:
view plaincopyprint?private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio; } return inSampleSize; } private static int calculateInSampleSize(BitmapFactory.Options options, if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // Choose the smallest ratio as inSampleSize value, this will return inSampleSize; |
|
来自: androidHung > 《android》