降低的图像的文件大小 [英] Reduce file size of an image
问题描述
我已经采取了Android相机拍摄照片。结果是一个字节数组。我写它放在SD卡(FileOutputStream中)保存。结果为具有几乎3MB的文件大小的图像。我想减少这种文件大小,所以COM preSS的形象。
I have taken a picture with the android camera. The result is an byte array. I save it by writing it on the sdcard (FileOutputStream). The result is an image with a filesize of nearly 3mb. I would like to reduce this filesize, so compress the image.
如果有可能,以减少文件大小之前,我会写字节数组输出流这将是很好。那是可能的,或者我必须先救它?
It would be nice if it is possible to reduce the filesize before i would write the byte array to the output stream. Is that possible or do I have to save it first?
推荐答案
我通常调整从而降低它的大小图像
I usually resize the image which reduces the size of it
Bitmap bitmap = resizeBitMapImage1(exsistingFileName, 800, 600);
您也可以融为一体preSS与此code图片
You can also compress an image with this code
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg")
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
重新上浆code
Re-sizing Code
public static Bitmap resizeBitMapImage1(String filePath, int targetWidth, int targetHeight) {
Bitmap bitMapImage = null;
try {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth
- targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
sampleSize = scaleByHeight ? options.outHeight / targetHeight : options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
} catch (Exception ex) {
}
return bitMapImage;
}
这篇关于降低的图像的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!