Android 图片压缩上传
来源:互联网 发布:敦煌博物馆 淘宝 编辑:程序博客网 时间:2024/06/04 20:15
当前的手机拍摄的照片都是5、6M的。这样图片上传既浪费流量还影响用户使用体验。所以对图片进行压缩上传。
//调用压缩图片的方法,返回压缩后的图片path String src_path = fileupload.getAbsolutepath();//原图片的路径 String targetPath = Environment.getExternalStorageDirectory() + "/download/" + fileupload.getBussiness_pkid() + ".jpg";//压缩后图片的路径 final String compressImage = Utils.compressImage(src_path, targetPath, 30);//进行图片压缩,返回压缩后图片的路径 final File file = new File(compressImage); //压缩后的图片 //上传图片
//图片压缩代码
public static String compressImage(String filePath, String targetPath, int quality) { Bitmap bm = getSmallBitmap(filePath);//获取一定尺寸的图片 int degree = readPictureDegree(filePath);//获取相片拍摄角度 if (degree != 0) {//旋转照片角度,防止头像横着显示 bm = rotateBitmap(bm, degree); } File outputFile = new File(targetPath); try { if (!outputFile.exists()) { outputFile.getParentFile().mkdirs(); //outputFile.createNewFile(); } else { outputFile.delete(); } FileOutputStream out = new FileOutputStream(outputFile); bm.compress(Bitmap.CompressFormat.JPEG, quality, out); } catch (Exception e) { } return outputFile.getPath(); } /** * 根据路径获得图片信息并按比例压缩,返回bitmap */ public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//只解析图片边沿,获取宽高 BitmapFactory.decodeFile(filePath, options); // 计算缩放比 options.inSampleSize = calculateInSampleSize(options, 480, 800); // 完整解析图片返回bitmap options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } /** * 获取照片角度 * * @param path * @return */ public static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } /** * 旋转照片 * * @param bitmap * @param degress * @return */ public static Bitmap rotateBitmap(Bitmap bitmap, int degress) { if (bitmap != null) { Matrix m = new Matrix(); m.postRotate(degress); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); return bitmap; } return bitmap; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
0 0
- android图片压缩上传
- Android 图片压缩上传
- android 图片压缩上传
- Android 图片压缩上传
- Android 图片压缩上传问题.
- Android图片压缩批量上传
- android上传图片压缩图片大小
- Android图片压缩上传系列
- Android图片压缩加密上传
- Android图片压缩加密上传
- Android图片压缩加密上传
- Android上传头像,图片剪裁,压缩图片
- Android上传头像,图片剪裁,压缩图片
- Android上传头像,图片剪裁,压缩图片
- 图片压缩上传 上传 压缩
- Android如何压缩图片上传服务端
- Android图片压缩上传之详情介绍
- android 图片转Base64压缩上传
- 化学公式编辑器怎么画聚合物?
- MAC下配置Tomcat
- sql查询rownum下标不对导致结果不正确
- Etcd 集群稳定性测试记录
- 跳表
- Android 图片压缩上传
- error: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getIm
- socket通讯,TCP,UDP,HTTP的区别
- golang Context包的使用
- Java事务处理全解析(六)—— 使用动态代理(Dynamic Proxy)完成事务
- AbaBoost训练 d_createsamples.dat
- Socket、Tcp、Udp、Http 的认识及区别
- 集合框架-ArrayList集合存储自定义对象的排序案例
- 百度地图画多边形代码