import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Environment; import android.util.Log; public class MainActivity extends Activity { final String PATH = Environment.getExternalStorageDirectory() + "/Bst/a.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { try { getThumbUploadPath(PATH,480); } catch (Exception e) { Log.e("eeee", e.toString()); } } private String getThumbUploadPath(String oldPath,int bitmapMaxWidth) throws Exception { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(oldPath, options); int height = options.outHeight; int width = options.outWidth; int reqHeight = 0; int reqWidth = bitmapMaxWidth; reqHeight = (reqWidth * height)/width; // 在内存中创建bitmap对象,这个对象按照缩放大小创建的 options.inSampleSize = calculateInSampleSize(options, bitmapMaxWidth, reqHeight); // System.out.println("calculateInSampleSize(options, 480, 800);===" // + calculateInSampleSize(options, 480, 800)); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(oldPath, options); //Log.e("asdasdas", "reqWidth->"+reqWidth+"---reqHeight->"+reqHeight); Bitmap bbb = compressImage(Bitmap.createScaledBitmap(bitmap, bitmapMaxWidth, reqHeight, false)); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") .format(new Date()); return BitmapUtils.saveImg(bbb, MD5Utils.md5(timeStamp)); } private 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) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize; } private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 options -= 10;// 每次都减少10 baos.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片 return bitmap; } }
BitmapUtils:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.graphics.Bitmap; import android.os.Environment; /** * 将bitmap保存在SD卡 * @author xinruzhou * */ public class BitmapUtils { /** * * @param b Bitmap * <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> 图片存储的位置 * @throws FileNotFoundException */ public static String saveImg(Bitmap b,String name) throws Exception{ String path = Environment.getExternalStorageDirectory().getPath()+File.separator+"test/headImg/"; File mediaFile = new File(path + File.separator + name + ".jpg"); if(mediaFile.exists()){ mediaFile.delete(); } if(!new File(path).exists()){ new File(path).mkdirs(); } mediaFile.createNewFile(); FileOutputStream fos = new FileOutputStream(mediaFile); b.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); b.recycle(); b = null; System.gc(); return mediaFile.getPath(); } }
MD5Utils:
import java.security.MessageDigest; public class MD5Utils { /** * MD5加密 * @param str 要加密的字符串 * @return 加密后的字符串 */ public static String md5(String str) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { e.printStackTrace(); return ""; } char[] charArray = str.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) { byteArray<i> = (byte) charArray<i>; } byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes<i>) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } } </i></i></i>
google的算法:
用法:o2.inSampleSize = computeSampleSize(o, -1, nWidth*nHeight);
public 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; } private 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) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }