当你在android上保存为字节数组的图像(本例中是PNG)时,如何缩放它以获得以字节数组格式缩放的新图像?
请记住,要缩放的图像尺寸较小,避免数据丢失。
答案 0 :(得分:0)
这样做的一个简单方法就是让Android为我们解析算法。 因此,我们将字节数组转换为位图,位图可以创建一个新的位图,并定义新的大小,最后转换回字节数组。
private byte[] getScaledImage(byte[] originalImage, int newWidth, int newHeight) {
// PNG has not losses, it just ignores this field when compressing
final int COMPRESS_QUALITY = 0;
// Get the bitmap from byte array since, the bitmap has the the resize function
Bitmap bitmapImage = (BitmapFactory.decodeByteArray(originalImage, 0, originalImage.length));
// New bitmap with the correct size, may not return a null object
Bitmap mutableBitmapImage = Bitmap.createScaledBitmap(bitmapImage,newWidth, newHeight, false);
// Get the byte array from tbe bitmap to be returned
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mutableBitmapImage.compress(Bitmap.CompressFormat.PNG, 0 , outputStream);
if (mutableBitmapImage != bitmapImage) {
mutableBitmapImage.recycle();
} // else they are the same, just recycle once
bitmapImage.recycle();
return outputStream.toByteArray();
}
答案 1 :(得分:0)
图像裁剪字节uri图像转换字节数组的步骤 InputStream iStream = getContentResolver()。openInputStream(uri); byte [] inputData = getBytes(iStream);