Java Android,压缩图像

Java Android,压缩图像,java,android,image,compression,Java,Android,Image,Compression,我正在通过wifi或移动网络通过网络发送图像,以存储在服务器中并再次检索。我已经这样做了,但由于相机拍摄的图像太大,这让我的应用程序速度变慢了,我要指出的是,我正在打开画廊,从那里拍照,而不是直接从应用程序中拍照。我注意到从相机和多媒体资料中拍摄的whatsapp图像已压缩到约100kb 此时,我的代码接收一个文件并将其转换为字节,然后发送它。下面是获取文件并将其转换为字节的方法 private void toBytes(String filePath){ try{ Fi

我正在通过wifi或移动网络通过网络发送图像,以存储在服务器中并再次检索。我已经这样做了,但由于相机拍摄的图像太大,这让我的应用程序速度变慢了,我要指出的是,我正在打开画廊,从那里拍照,而不是直接从应用程序中拍照。我注意到从相机和多媒体资料中拍摄的whatsapp图像已压缩到约100kb

此时,我的代码接收一个文件并将其转换为字节,然后发送它。下面是获取文件并将其转换为字节的方法

private void toBytes(String filePath){
    try{
        File file = new File(filePath);
        InputStream is = new BufferedInputStream(new FileInputStream(file));  
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        bytes = new byte[(int) filePath.length()];
        int bytes_read;
        while((bytes_read = is.read(bytes, 0, bytes.length)) != -1){
            buffer.write(bytes, 0, bytes_read);
        }
        is.close();               
        bytes = buffer.toByteArray();
    }catch(Exception err){
        Toast.makeText(getApplicationContext(), err.toString(), Toast.LENGTH_SHORT).show();
    }
}
所以我的问题是,在发送之前,我将如何压缩我的图像?此外,我不需要图像来保持高像素计数,因为当应用程序使用图像时,它只占设备屏幕的一半

感谢您提供的帮助。

位图
类有一个
压缩
方法。

但是您可能需要缩放图像
createScaledBitmap
,该图像在同一类中也可用。

尝试使用以下方法:

    //decodes image and scales it to reduce memory consumption
    //NOTE: if the image has dimensions which exceed int width and int height
    //its dimensions will be altered.
    private Bitmap decodeToLowResImage(byte [] b, int width, int height) {
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o);

            //The new size we want to scale to
            final int REQUIRED_SIZE_WIDTH=(int)(width*0.7);
            final int REQUIRED_SIZE_HEIGHT=(int)(height*0.7);

            //Find the correct scale value. It should be the power of 2.
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE_WIDTH || height_tmp/2<REQUIRED_SIZE_HEIGHT)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o2);
        } catch (OutOfMemoryError e) {
        }
        return null;
    }
//解码图像并对其进行缩放以减少内存消耗
//注意:如果图像的尺寸超过int-width和int-height
//它的尺寸将被改变。
专用位图解码LowResImage(字节[]b,整数宽度,整数高度){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的ByteArrayInputStream(b),null,o);
//我们要扩展到的新尺寸
所需的最终整型尺寸宽度=(整型)(宽度*0.7);
所需最终整型尺寸高度=(整型)(高度*0.7);
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2名为的Android库已经过优化,可以为您进行所有图像压缩。请查看

此链接是否可以帮助您进行可能的复制?您能否解释为什么在解决方案中将所需的宽度和高度乘以0.7?谢谢。嗯,我现在推荐此解决方案: