减少一个位图的大小来Android中指定的尺寸 [英] Reduce the size of a bitmap to a specified size in Android

查看:150
本文介绍了减少一个位图的大小来Android中指定的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲减少一个位图的大小恰好200KB。我从SD卡,COM preSS得到它的形象,它用不同的名称再次保存到SD卡到不同的目录中。 COM pression工作正常(3 MB象图像为com pressed为100 KB左右)。我写的codeS以下行此​​:

 字符串镜像文件=/ SD卡/ DCIM / 100ANDRO / DSC_0530.jpg
位图BM = ShrinkBitmap(镜像文件,300,300);//此方法COM presses图像并保存到SD卡中的位置
    位图ShrinkBitmap(字符串的文件,诠释的宽度,高度INT){         BitmapFactory.Options bmpFactoryOptions =新BitmapFactory.Options();
            bmpFactoryOptions.inJustDe codeBounds = TRUE;
            位图位图= BitmapFactory.de codeFILE(文件,bmpFactoryOptions);            INT heightRatio =(int)的Math.ceil(bmpFactoryOptions.outHeight /(浮点)高度);
            INT widthRatio =(int)的Math.ceil(bmpFactoryOptions.outWidth /(浮点)宽度);            如果(heightRatio→1 || widthRatio→1)
            {
             如果(heightRatio> widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             }其他{
              bmpFactoryOptions.inSampleSize = widthRatio;
             }
            }            bmpFactoryOptions.inJustDe codeBounds = FALSE;
            位= BitmapFactory.de codeFILE(文件,bmpFactoryOptions);            ByteArrayOutputStream流=新ByteArrayOutputStream();
            bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,流);
            字节[] = imageInByte stream.toByteArray();
            //这给了以KB为COM pressed图像的大小
            长lengthbmp = imageInByte.length / 1024;            尝试{
                bitmap.com preSS(比较pressFormat.JPEG,100,新的FileOutputStream(/ SD卡/ mediaAppPhotos / COM pressed_new.jpg));
            }赶上(FileNotFoundException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }
         返回位图;
        }


解决方案

我发现,完美的作品对我的回答是:

  / **
     *减小了图像的大小
     * @参数图片
     * @参数MAXSIZE
     * @返回
     * /
    公共位图getResizedBitmap(位图图像​​,诠释MAXSIZE){
        INT宽度= image.getWidth();
        INT高度= image.getHeight();        浮动bitmapRatio =(浮点)宽/(浮点)高度;
        如果(bitmapRatio大于0){
            宽度= MAXSIZE;
            高度=(int)的(宽度/ bitmapRatio);
        }其他{
            高度= MAXSIZE;
            宽度=(INT)(高* bitmapRatio);
        }
        返回Bitmap.createScaledBitmap(图像,宽度,高度,真);
    }

调用方法:

 位图converetdImage = getResizedBitmap(照片,500);


  • 照片是你的位图

I want to reduce the size of a bitmap to 200kb exactly. I get an image from the sdcard, compress it and save it to the sdcard again with a different name into a different directory. Compression works fine (3 mb like image is compressed to around 100 kb). I wrote the following lines of codes for this:

String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg";
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);

//this method compresses the image and saves into a location in sdcard
    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();   
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
            byte[] imageInByte = stream.toByteArray(); 
            //this gives the size of the compressed image in kb
            long lengthbmp = imageInByte.length / 1024; 

            try {
                bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


         return bitmap;
        }

解决方案

I found an answer that works perfectly for me:

/**
     * reduces the size of the image
     * @param image
     * @param maxSize
     * @return
     */
    public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }

calling the method:

Bitmap converetdImage = getResizedBitmap(photo, 500);

  • photo is your bitmap

这篇关于减少一个位图的大小来Android中指定的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆