package com.inspection.util; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Matrix; public class Base64Encoder { private static final char last2byte = (char) Integer.parseInt("00000011", 2); private static final char last4byte = (char) Integer.parseInt("00001111", 2); private static final char last6byte = (char) Integer.parseInt("00111111", 2); private static final char lead6byte = (char) Integer.parseInt("11111100", 2); private static final char lead4byte = (char) Integer.parseInt("11110000", 2); private static final char lead2byte = (char) Integer.parseInt("11000000", 2); private static final char[] encodeTable = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; /** * Base64 encoding. * * @param from * The src data. * @return */ public static String encode(byte[] from) { StringBuffer to = new StringBuffer((int) (from.length * 1.34) + 3); int num = 0; char currentByte = 0; for (int i = 0; i < from.length; i++) { num = num % 8; while (num < 8) { switch (num) { case 0: currentByte = (char) (from[i] & lead6byte); currentByte = (char) (currentByte >>> 2); break; case 2: currentByte = (char) (from[i] & last6byte); break; case 4: currentByte = (char) (from[i] & last4byte); currentByte = (char) (currentByte << 2); if ((i + 1) < from.length) { currentByte |= (from[i + 1] & lead2byte) >>> 6; } break; case 6: currentByte = (char) (from[i] & last2byte); currentByte = (char) (currentByte << 4); if ((i + 1) < from.length) { currentByte |= (from[i + 1] & lead4byte) >>> 4; } break; } to.append(encodeTable[currentByte]); num += 6; } } if (to.length() % 4 != 0) { for (int i = 4 - to.length() % 4; i > 0; i--) { to.append("="); } } return to.toString(); } public static byte[] compressBmpToFile(Bitmap bmp){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100;//个人喜欢从80开始, bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); while (baos.toByteArray().length / 1024 > 1024) { baos.reset(); options -= 5; bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); } return baos.toByteArray(); } /** *//** * 把字节数组保存为一个文件 * @Author Sean.guo */ public static File getFileFromBytes(byte[] b, String outputFile){ BufferedOutputStream stream = null; File file = null; try{ file = new File(outputFile); FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e){ e.printStackTrace(); } finally{ if (stream != null){ try{ stream.close(); } catch (IOException e1){ e1.printStackTrace(); } } } return file; } public static Bitmap getBitmap(String imgPath) { // Get bitmap through image path BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = false; newOpts.inPurgeable = true; newOpts.inInputShareable = true; // Do not compress newOpts.inSampleSize = 1; newOpts.inPreferredConfig = Config.RGB_565; return BitmapFactory.decodeFile(imgPath, newOpts); } public static Bitmap compressImageFromFile(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = 720f;// float ww = 1280f;// int be = 1; if (w > h && w > ww) { be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) { be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置采样率 newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设 newOpts.inPurgeable = true;// 同时设置才会有效 newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap; } /** * 压缩图片 * @param bitmap 源图片 * @param width 想要的宽度 * @param height 想要的高度 * @param isAdjust 是否自动调整尺寸, true图片就不会拉伸,false严格按照你的尺寸压缩 * @return Bitmap */ public static Bitmap reduce(Bitmap bitmap, int width, int height, boolean isAdjust) { // 如果想要的宽度和高度都比源图片小,就不压缩了,直接返回原图 if (bitmap.getWidth() < width && bitmap.getHeight() < height) {return bitmap;} // 根据想要的尺寸精确计算压缩比例, 方法详解:public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode); // scale表示要保留的小数位, roundingMode表示如何处理多余的小数位,BigDecimal.ROUND_DOWN表示自动舍弃 float sx = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN).floatValue(); float sy = new BigDecimal(height).divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue(); if (isAdjust) {// 如果想自动调整比例,不至于图片会拉伸 sx = (sx < sy ? sx : sy);sy = sx;// 哪个比例小一点,就用哪个比例 } Matrix matrix = new Matrix(); matrix.postScale(sx, sy);// 调用api中的方法进行压缩,就大功告成了 return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } }
注意:本文归作者所有,未经作者允许,不得转载