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
原创粉丝点击