android图片压缩

1. 质量压缩方法

private Bitmap compressImage(Bitmap image) {  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
    int options = 100;  
    while ( baos.toByteArray().length / 1024>100) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩         
        baos.reset();//重置baos即清空baos  
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中  
        options -= 10;//每次都减少10  
    }  
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中  
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片  
    return bitmap;  
}  

2. 图片按比例大小压缩方法

public static boolean getCacheImage(String filePath,String cachePath){
    OutputStream out = null;
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inJustDecodeBounds = true;  //设置为true,只读尺寸信息,不加载像素信息到内存
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, option);  //此时bitmap为空
    option.inJustDecodeBounds = false;
    int bWidth = option.outWidth;
    int bHeight= option.outHeight;
    int toWidth = 400;
    int toHeight = 800;
    int be = 1;  //be = 1代表不缩放
    if(bWidth/toWidth>bHeight/toHeight&&bWidth>toWidth){
        be = (int)bWidth/toWidth;
    }else if(bWidth/toWidth<bheight bheight="">toHeight){
        be = (int)bHeight/toHeight;
    }
    option.inSampleSize = be; //设置缩放比例
    bitmap  = BitmapFactory.decodeFile(filePath, option);
    try {
        out = new FileOutputStream(new File(cachePath));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap.compress(CompressFormat.JPEG, 100, out);
}

3. 正常情况下我们应该把两者相结合的

public static File scal(Uri fileUri){
    String path = fileUri.getPath();
    File outputFile = new File(path);
    long fileSize = outputFile.length();
    final long fileMaxSize = 200 * 1024;
     if (fileSize >= fileMaxSize) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
            int height = options.outHeight;
            int width = options.outWidth;

            double scale = Math.sqrt((float) fileSize / fileMaxSize);
            options.outHeight = (int) (height / scale);
            options.outWidth = (int) (width / scale);
            options.inSampleSize = (int) (scale + 0.5);
            options.inJustDecodeBounds = false;

            Bitmap bitmap = BitmapFactory.decodeFile(path, options);
            outputFile = new File(PhotoUtil.createImageFile().getPath());
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(, sss ok  + outputFile.length());
            if (!bitmap.isRecycled()) {
                bitmap.recycle();
            }else{
                File tempFile = outputFile;
                outputFile = new File(PhotoUtil.createImageFile().getPath());
                PhotoUtil.copyFileUsingFileChannels(tempFile, outputFile);
            }
             
        }
     return outputFile;
     
}


public static Uri createImageFile(){
    // Create an image file name
    String timeStamp = new SimpleDateFormat(yyyyMMdd_HHmmss).format(new Date());
    String imageFileName = JPEG_ + timeStamp + _;
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = null;
    try {
        image = File.createTempFile(
            imageFileName,  /* prefix */
            .jpg,         /* suffix */
            storageDir      /* directory */
        );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Save a file: path for use with ACTION_VIEW intents
    return Uri.fromFile(image);
}



public static void copyFileUsingFileChannels(File source, File dest){
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } finally {
        try {
            inputChannel.close();
            outputChannel.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 非原创,只是整理,如果里面发现引用的内容没有标识出来,欢迎指出。 一、基本知识 (1)两种图片: 1)矢量图: 矢...
    风再起时ME阅读 2,482评论 0 19
  • 先发一张昨天去看我雷哥演唱会的皂片然后再说正文哈哈。 简介 由于工作原因,boss下达的任务就大概说了对图片进行压...
    我叫王菜鸟阅读 5,300评论 2 16
  • 开发中遇到需要上传图片的场景还是很常见的,这就涉及到图片的压缩处理。如果不进行压缩,势必造成消耗大量的流量,下载图...
    laogui阅读 20,904评论 19 164
  • 关键词: Bitmap,质量压缩,比例压缩,采样率压缩,微信分享 前言 android 系统的图片压缩大体上有三种...
    江风凌晓阅读 3,499评论 6 57
  • 简单吹下牛:很多app都会要加载图片,但是如果不压缩图片就很容易OOM, 个人看来OOM 出现原因总的来说分为两种...
    sakura_L阅读 1,273评论 2 4