大图片压缩后,保存小图片到本地

很多情况下,我们需要给服务器上传本地手机中的照片,但是手机照片的尺寸太大,好几兆,我们也没必须把这么大的照片上传给服务器,所以要压缩图片质量和尺寸,得到一个比较适应手机屏幕尺寸大小的照片。

这里没有把存放路径、压缩后图片大小,这些参数抽离出来,用户可在根据实际情况修改一下

目的:从本地path中获取bitmap,压缩后保存小图片到本地 涉及到的知识点:

  • 根据ImageView获取适当的压缩的宽和高,尽可能得到ImageView的精确的宽高
  • 通过反射获取imageview的某个属性值
  • 根据图片要显示的宽和高,对图片进行压缩,避免OOM
  • 根据需求的宽和高以及图片实际的宽和高计算SampleSize
package com.example.a.testduoxuantupian;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * Created by a on 2016/3/31.
 */
public class ImageUtils {
    /**
     * 从本地path中获取bitmap,压缩后保存小图片到本地
     *
     * @param context
     * @param path    图片存放的路径
     *                
     * @return 返回压缩后图片的存放路径
     */
    public static String saveBitmap(Context context, String path) {
        String compressdPicPath = "";

//      ★★★★★★★★★★★★★★重点★★★★★★★★★★★★★
      /*  //★如果不压缩直接从path获取bitmap,这个bitmap会很大,下面在压缩文件到100kb时,会循环很多次,
        // ★而且会因为迟迟达不到100k,options一直在递减为负数,直接报错
        //★ 即使原图不是太大,options不会递减为负数,也会循环多次,UI会卡顿,所以不推荐不经过压缩,直接获取到bitmap
        Bitmap bitmap=BitmapFactory.decodeFile(path);*/
//      ★★★★★★★★★★★★★★重点★★★★★★★★★★★★★

//        建议先将图片压缩到控件所显示的尺寸大小后,再压缩图片质量
//        首先得到手机屏幕的高宽,根据此来压缩图片,当然想要获取跟精确的控件显示的高宽(更加节约内存),可以使用getImageViewSize();
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;  // 屏幕宽度(像素)
        int height = displayMetrics.heightPixels;  // 屏幕高度(像素)
//        获取按照屏幕高宽压缩比压缩后的bitmap
        Bitmap bitmap = decodeSampledBitmapFromPath(path, width, height);

        String oldName = path.substring(path.lastIndexOf("/"), path.lastIndexOf("."));
        String name = oldName + "_compress.jpg";//★很奇怪oldName之前不能拼接字符串,只能拼接在后面,否则图片保存失败
        String saveDir = Environment.getExternalStorageDirectory()
                + "/compress";
        File dir = new File(saveDir);
        if (!dir.exists()) {
            dir.mkdir();
        }
        // 保存入sdCard
        File file = new File(saveDir, name);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

/* options表示 如果不压缩是100,表示压缩率为0。如果是70,就表示压缩率是70,表示压缩30%; */
        int options = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        while (baos.toByteArray().length / 1024 > 500) {
// 循环判断如果压缩后图片是否大于500kb继续压缩

            baos.reset();
            options -= 10;
            if (options < 11) {//为了防止图片大小一直达不到500kb,options一直在递减,当options<0时,下面的方法会报错
                // 也就是说即使达不到500kb,也就压缩到10了
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
                break;
            }
// 这里压缩options%,把压缩后的数据存放到baos中
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
        }

        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write(baos.toByteArray());
            out.flush();
            out.close();
            compressdPicPath = file.getAbsolutePath();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return compressdPicPath;
    }

    /**
     * 根据ImageView获取适当的压缩的宽和高
     * 尽可能得到ImageView的精确的宽高
     *
     * @param imageView
     * @return
     */
    public static ImageSize getImageViewSize(ImageView imageView) {

//      得到屏幕的宽度
        DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics();


        ImageSize imageSize = new ImageSize();
        ViewGroup.LayoutParams lp = imageView.getLayoutParams();

//      ------------------------------------尽可能得到ImageView的精确的宽高-------------------------------------------------------------
//      得到imageView的实际宽度(为了压缩图片,这里一定要得到imageview的宽高)必须压缩!,否则OOM!!!!!!!!!!
        int width = imageView.getWidth();

        if (width <= 0) {//可能imageView刚new出来还没有添加到容器当中,width可能为0
            width = lp.width;//获取imageView在layout中声明的宽度
        }
        if (width <= 0) {//如果imageView设置的是WRAP_CONTENT=-1或者MATHC_PARENT=-2,得到的width还是0
            // TODO: 2016/3/19 此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取,已解决
//            width = imageView.getMaxWidth();//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
            width = getImageViewFieldValue(imageView, "mMaxWidth");//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
        }
        if (width <= 0) {//如果还是得不到width,就设置为屏幕的宽度
            width = displayMetrics.widthPixels;
        }

//                                        ----------------------------------------

        //      得到imageView的实际高度(为了压缩图片,这里一定要得到imageview的宽高)必须压缩!,否则OOM!!!!!!!!!!
        int height = imageView.getHeight();

        if (height <= 0) {//可能imageView刚new出来还没有添加到容器当中,width可能为0
            height = lp.height;//获取imageView在layout中声明的高度
        }
        if (height <= 0) {//如果imageView设置的是WRAP_CONTENT=-1或者MATHC_PARENT=-2,得到的width还是0
            // TODO: 2016/3/19 此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取,已解决
//            height = imageView.getMaxHeight();//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
            height = getImageViewFieldValue(imageView, "mMaxHeight");//检查最大值(此方法在API16以上才可以使用,为了兼容低版本,一会用反射获取)
        }
        if (height <= 0) {//如果还是得不到width,就设置为屏幕的高度
            height = displayMetrics.heightPixels;
        }
//       --------------------------------尽可能得到ImageView的精确的宽高------------------------------------------------------------------

        imageSize.width = width;
        imageSize.height = height;
        return imageSize;
    }

    /**
     * 通过反射获取imageview的某个属性值(imageView.getMaxWidth()这个方法在api16以上才可以用,所以使用反射得到这个width属性值)
     *
     * @param object
     * @param fieldName
     * @return
     */
    private static int getImageViewFieldValue(Object object, String fieldName) {

        int value = 0;

        try {
            Field field = ImageView.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            int fieldValue = field.getInt(object);
            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
                value = fieldValue;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        return value;
    }

    /**
     * 根据图片要显示的宽和高,对图片进行压缩,避免OOM
     *
     * @param path
     * @param width  要显示的imageview的宽度
     * @param height 要显示的imageview的高度
     * @return
     */
    private static Bitmap decodeSampledBitmapFromPath(String path, int width, int height) {

//      获取图片的宽和高,并不把他加载到内存当中
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = caculateInSampleSize(options, width, height);
//      使用获取到的inSampleSize再次解析图片(此时options里已经含有压缩比 options.inSampleSize,再次解析会得到压缩后的图片,不会oom了 )
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        return bitmap;

    }

    /**
     * 根据需求的宽和高以及图片实际的宽和高计算SampleSize
     *
     * @param options
     * @param reqWidth  要显示的imageview的宽度
     * @param reqHeight 要显示的imageview的高度
     * @return
     * @compressExpand 这个值是为了像预览图片这样的需求,他要比所要显示的imageview高宽要大一点,放大才能清晰
     */
    private static int caculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;

        int inSampleSize = 1;

        if (width >= reqWidth || height >= reqHeight) {

            int widthRadio = Math.round(width * 1.0f / reqWidth);
            int heightRadio = Math.round(width * 1.0f / reqHeight);

            inSampleSize = Math.max(widthRadio, heightRadio);

        }

        return inSampleSize;
    }



    /**
     * This method is used to get real path of file from from uri<br/>
     * http://stackoverflow.com/questions/11591825/how-to-get-image-path-just-
     * captured-from-camera
     * 这个方法是可以使用的(网上搜了一堆好多没用的代码)
     * @param contentUri
     * @return String
     */
    public static String getRealPathFromURI(Activity activity, Uri contentUri) {
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            // Do not call Cursor.close() on a cursor obtained using this
            // method,
            // because the activity will do that for you at the appropriate time
            Cursor cursor = activity.managedQuery(contentUri, proj, null, null,
                    null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            return contentUri.getPath();
        }
    }
}
package com.example.a.testduoxuantupian;

/**
 * 存放获取imageview高宽的一个类
 */
class ImageSize {
        public int width;
        public int height;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用PIL库来处理图片。以下是一个简单的示例代码,可以将小图片旋转任意角度并粘贴到大图片中: ```python from PIL import Image import math # 打开小图片 img_small = Image.open('small_image.png') # 旋转小图片 angle = 45 # 旋转角度 img_small_rotated = img_small.rotate(angle, expand=True) # 去掉黑底 img_small_rotated = img_small_rotated.convert('RGBA') datas = img_small_rotated.getdata() new_data = [] for item in datas: if item[0] == 0 and item[1] == 0 and item[2] == 0: new_data.append((255, 255, 255, 0)) else: new_data.append(item) img_small_rotated.putdata(new_data) # 打开大图片 img_large = Image.open('large_image.png') # 计算粘贴位置 x = 100 # x坐标 y = 100 # y坐标 w, h = img_small_rotated.size angle_rad = math.radians(angle) # 将角度转换为弧度 cos_theta = math.cos(angle_rad) sin_theta = math.sin(angle_rad) x1 = int(x - w/2*cos_theta - h/2*sin_theta) y1 = int(y - w/2*sin_theta + h/2*cos_theta) # 粘贴小图片到大图片中 img_large.paste(img_small_rotated, (x1, y1), img_small_rotated) # 保存图片 img_large.save('output_image.png') ``` 这个代码首先打开小图片,然后对小图片进行旋转,并使用PIL库的`convert`方法将图片转换为RGBA格式。接着,将图片中黑色像素的alpha通道值设置为0,实现去掉黑底的效果。然后打开大图片,计算小图片在大图片中的粘贴位置,并使用PIL库的`paste`方法将小图片粘贴到大图片中。最后保存输出的图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值