【发布时间】:2015-05-21 19:14:06
【问题描述】:
我正在尝试编写一个接受图像(Bitmap) 并返回byte[] array 的方法。最后,我试着把这个byte[] array写到一个文件夹里,这样我就可以看到区别了,但是我的byte[] array不能显示,另外,它没有按比例缩小!这是我的方法:
private byte[] changeSize(Bitmap image) {
byte[] picture;
int width = image.getWidth();
int height = image.getHeight();
int newHeight = 0, newWidth = 0;
if (width > 250 || height > 250) {
if (width > height) { //landscape-mode
newHeight = 200;
newWidth = (newHeight * width) / height;
} else { //portrait-mode
newWidth = 200;
newHeight = (newWidth * height) / width;
}
} else {
Toast.makeText(this, "Something wrong!", Toast.LENGTH_LONG).show();
}
Bitmap sizeChanged = Bitmap.createScaledBitmap(image, newWidth, newHeight, true);
//Convert bitmap to a byte array
int bytes = sizeChanged.getByteCount();
ByteBuffer bb = ByteBuffer.allocate(bytes);
sizeChanged.copyPixelsFromBuffer(bb);
picture = bb.array();
//Write to a hd
picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String fileName = edFile.getText().toString() + "_downscaled" + ".jpg";
File file = new File(picturePath, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(picture);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
我尝试了几个小时让我的byte[] array 可见,但我根本做不到。非常感谢任何帮助或提示我出轨的地方。
【问题讨论】:
-
您从
copyPixelsFromBuffer()获得的字节数组包含出现在主内存中的原始 图像像素数据。此数据肯定是不是 JPEG 编码的,而是需要根据位图的像素格式解释的值数组。你真正想做什么? -
1.相对于宽度/高度缩小我的位图。 2. 获取新位图
sizeChanged。 3.将新的Bitmap实例转换为byte[] array´ -->byte[]图片. 4. writingpicture`到文件系统中查看。
标签: java android bitmap bytearray scale