public class MainActivity extends Activity { //照片保存地址 private static final String FILE_PATH = "/sdcard/gone.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //压缩图片 Bitmap bitmap = getBitmapFromUrl(FILE_PATH, 480, 660); // 保存图片 saveFile(bitmap, FILE_PATH); } catch (Exception e) { e.printStackTrace(); } } /** * 根据路径获取图片资源(已缩放,适配横竖屏压缩) * * @param url 图片存储路径 * @param width 缩放的宽度 * @param height 缩放的高度 * @return */ private Bitmap getBitmapFromUrl(String url, double width, double height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 设置了此属性一定要记得将值设置为false Bitmap bitmap = BitmapFactory.decodeFile(url); // 防止OOM发生 options.inJustDecodeBounds = false; int mWidth = bitmap.getWidth(); int mHeight = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = 1; float scaleHeight = 1; // try { // ExifInterface exif = new ExifInterface(url); // String model = exif.getAttribute(ExifInterface.TAG_ORIENTATION); // } catch (IOException e) { // e.printStackTrace(); // } // 按照固定宽高进行缩放 // 这里希望知道照片是横屏拍摄还是竖屏拍摄 // 因为两种方式宽高不同,缩放效果就会不同 // 这里用了比较笨的方式 if (mWidth <= mHeight) { scaleWidth = (float) (width / mWidth); scaleHeight = (float) (height / mHeight); } else { scaleWidth = (float) (height / mWidth); scaleHeight = (float) (width / mHeight); } // matrix.postRotate(90); /* 翻转90度 */ // 按照固定大小对图片进行缩放 matrix.postScale(scaleWidth, scaleHeight); Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight, matrix, true); // 用完了记得回收 bitmap.recycle(); return newBitmap; } /** * 只能竖屏压缩图片尺寸 * * @param pathName * @param targetWidth * @param targetHeight * @return */ public Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到图片的宽度、高度; float imgWidth = opts.outWidth; float imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); opts.inSampleSize = 1; if (widthRatio > 1 || widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } //设置好缩放比例后,加载图片进内容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; } /** * 存储进SD卡 * * @param bm * @param fileName * @throws Exception */ public void saveFile(Bitmap bm, String fileName) throws Exception { File dirFile = new File(fileName); //检测图片是否存在 if (dirFile.exists()) { dirFile.delete(); //删除原图片 } File myCaptureFile = new File(fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); //100表示不进行压缩,70表示压缩率为30% bm.compress(Bitmap.CompressFormat.JPEG, 70, bos); bos.flush(); bos.close(); }}
Android 照相机拍照,压缩后储存于SD卡
最新推荐文章于 2020-08-31 15:28:39 发布