java Android,压缩图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10267523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, Compressing an image
提问by Arran
I am sending an image over the network via wifi or the mobile network to be stored in a server and retrieved again. I've done that but due to the size of images taken by the camera it's making my app slow, just to point out I'm opening the gallery and taking the pictures from there and not taking the picture directly from the app. I have noticed that images from whatsapp that have been taken from the camera and gallery have been compressed to approx. 100kb.
我通过 wifi 或移动网络通过网络发送图像以存储在服务器中并再次检索。我已经这样做了,但是由于相机拍摄的图像的大小,它使我的应用程序变慢,只是指出我正在打开图库并从那里拍摄照片,而不是直接从应用程序拍摄照片。我注意到从相机和画廊拍摄的来自 whatsapp 的图像已被压缩到大约。100kb。
At the moment my code takes a file and converts it to bytes and then sends it. Here is the method for taking a file and converting it to bytes.
目前我的代码需要一个文件并将其转换为字节然后发送。这是获取文件并将其转换为字节的方法。
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();
}
}
So my question is how would I go about compressing my image before sending? Also I don't need the image to retain a high pixel count as when the app uses the image it will only take up half of the devices screen.
所以我的问题是我将如何在发送之前压缩我的图像?此外,我不需要图像来保留高像素数,因为当应用程序使用图像时,它只会占据设备屏幕的一半。
Thank you for any help given.
感谢您提供的任何帮助。
采纳答案by Kennet
The BitMap
http://developer.android.com/reference/android/graphics/Bitmap.htmlclass have a compress
method.
But you might need to scale the image createScaledBitmap
, also available in the same class.
该BitMap
http://developer.android.com/reference/android/graphics/Bitmap.html类有一个compress
方法。但是您可能需要缩放图像createScaledBitmap
,在同一类中也可用。
回答by Dhruv Gairola
Try using the following method:
尝试使用以下方法:
//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;
}
回答by Nathan Teyou
The Android library named SiliCompressorhave been optimized to do all the image compression for you. Check it on Github
名为SiliCompressor的 Android 库已经过优化,可以为您完成所有图像压缩。在Github上查看