标签:
今天在网上找了一下参考,得出把图片压缩至KB
其他不想多说、直接上代码
拍完照后调用下面代码
BitmapUtils.compressBitmap(photoPath, photoPath, 640); //压缩
这是个工具类。
package com.continuouscamera; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore.MediaColumns; /** * Tools for handler picture */ public final class BitmapUtils { private static final String TAG = BitmapUtils.class.getSimpleName(); public static void compressBitmap(String sourcePath, String targetPath, float maxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(sourcePath, options); final float originalWidth = options.outWidth; final float originalHeight = options.outHeight; float convertedWidth; float convertedHeight; if (originalWidth > originalHeight) { convertedWidth = maxSize; convertedHeight = maxSize / originalWidth * originalHeight; } else { convertedHeight = maxSize; convertedWidth = maxSize / originalHeight * originalWidth; } final float ratio = originalWidth / convertedWidth; options.inSampleSize = (int) ratio; options.inJustDecodeBounds = false; Bitmap convertedBitmap = BitmapFactory.decodeFile(sourcePath, options); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); convertedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); FileOutputStream fileOutputStream; try { fileOutputStream = new FileOutputStream(new File(targetPath)); fileOutputStream.write(byteArrayOutputStream.toByteArray()); fileOutputStream.flush(); fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 根据URI获取图片物理路径 */ public static String getAbsoluteImagePath(Uri uri, Activity activity) { String[] proj = { MediaColumns.DATA }; Cursor cursor = activity.managedQuery(uri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } /** * * @param path * @param maxSize * @return */ public static Bitmap decodeBitmap(String path, int maxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int originalWidth = options.outWidth; final int originalHeight = options.outHeight; int convertedWidth; int convertedHeight; if (originalWidth > originalHeight) { convertedWidth = maxSize; convertedHeight = maxSize / originalWidth * originalHeight; } else { convertedHeight = maxSize; convertedWidth = maxSize / originalHeight * originalWidth; } options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inSampleSize = computeSampleSize(options, maxSize, convertedWidth * convertedHeight); Bitmap convertedBitmap = BitmapFactory.decodeFile(path, options); if (convertedBitmap != null) { final int realWidth = convertedBitmap.getWidth(); final int realHeight = convertedBitmap.getHeight(); } return convertedBitmap; } /** * * @param path * @param maxWidth * @param maxHeight * @return */ public static Bitmap decodeBitmap(String path, int maxWidth, int maxHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int originalWidth = options.outWidth; final int originalHeight = options.outHeight; options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inSampleSize = computeSampleSize(options, maxWidth, maxWidth * maxHeight); Bitmap convertedBitmap = BitmapFactory.decodeFile(path, options); if (convertedBitmap != null) { final int realWidth = convertedBitmap.getWidth(); final int realHeight = convertedBitmap.getHeight(); } return convertedBitmap; } private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? minSideLength : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } } private static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } /** * 生成8位16进制的缓存因子:规则的8位哈希码,不足前面补零 * @param string * @return */ public static String toRegularHashCode(String string) { final String hexHashCode = Integer.toHexString(string.hashCode()); final StringBuilder stringBuilder = new StringBuilder(hexHashCode); while(stringBuilder.length() < 8){ stringBuilder.insert(0, ‘0‘); } return stringBuilder.toString(); } }
标签:
原文地址:http://www.cnblogs.com/kevin-chen/p/4722431.html