美文网首页
android图片压缩

android图片压缩

作者: yong_history | 来源:发表于2017-08-17 14:47 被阅读0次

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();
        }
    }
}

相关文章

  • 图片压缩

    Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法、比例压缩法(根据路径获取图片并压缩)和比例压缩...

  • Android:图片压缩的几种方式

    1、前言 在Android中,图片的压缩对于内存的优化很重要 通过这篇文章对Android中的几种图片压缩方式进行...

  • 图片压缩

    Android图片压缩常用的有质量压缩、尺寸压缩、采样率压缩以及通过JNI调用libjpeg库来进行压缩(尺寸压缩...

  • 【Android开发基础系列】图片专题

    1 图片编辑处理 1.1 图片裁切 转载自:bitmap的六种压缩方式,Android图片压缩 http://bl...

  • 资源图片优化

    tinypng 在线图片压缩 webP android studio选中图片右键Convert to webP

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • bitmap的六种压缩方式,Android图片压缩

    此处分享一个图片的压缩处理方式 : bitmap的六种压缩方式,Android图片压缩 转载链接,点击查看详情 !

  • Android图片压缩

    1.采样率压缩 采样率压缩是改变了图片的像素,他是通过先读取图片的边,然后在自己设定图片的边,然后根据设定,读取图...

  • android图片压缩

    质量压缩 尺寸压缩 缩放法压缩(matrix) RGB_565法(比ARGB_888少一半) createScal...

  • Android图片压缩

    首先,需要注意的是图片在内存中的大小是根据分辨率来决定的,即height*width BitmapFactory这...

网友评论

      本文标题:android图片压缩

      本文链接:https://www.haomeiwen.com/subject/vavzwttx.html