我有byte[]
存储我的图片。
现在我希望能够将其缩放并存储在另一个byte[]
中。
我知道如何扩展Bitmap
,并且可以将我的byte[]
转换为Bitmap
。
但我无法将缩放后的位图重新变为byte[]
。
黑莓
EncodedImage thumbnail = image.scaleImageToFill(50, 50);
什么也没做。
我正在尝试创建50x50的缩略图。不一定要准确。它存储在byte []。
中如何调整存储在byte []中的图像的大小,并将其保存在新的字节[]中。
byte[] imageTaken;
//Create thumbnail from image taken
EncodedImage image = EncodedImage.createEncodedImage(imageTaken, 0, -1);
image.getBitmap();
EncodedImage thumbnail = image.scaleImageToFill(50, 50);
byte[] thumbArray = thumbnail.getData();
答案 0 :(得分:6)
试试这段代码
bitmap = resizeImage(your image, 50,50);
private static EncodedImage resizeImage(EncodedImage image, int width, int height) {
if (image == null) {
return image;
}
//return if image does not need a resizing
if (image.getWidth() <= width && image.getHeight() <= height) {
return image;
}
double scaleHeight, scaleWidth;
if (image.getWidth() > width && image.getHeight() > height) { //actual image is bigger than scale size
if (image.getWidth() > image.getHeight()) { //actual image width is more that height then scale with width
scaleWidth = width;
scaleHeight = (double)width / image.getWidth() * image.getHeight();
} else { //scale with height
scaleHeight = height;
scaleWidth = (double)height / image.getHeight() * image.getWidth();
}
} else if (width < image.getWidth()) { //scale with scale width or height
scaleWidth = width;
scaleHeight = (double)width / image.getWidth() * image.getHeight();
} else {
scaleHeight = height;
scaleWidth = (double)height / image.getHeight() * image.getWidth();
}
int w = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP((int)scaleWidth));
int h = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP((int)scaleHeight));
return image.scaleImage32(w, h);
}
然后将位图转换为字节 -
JPEGEncodedImage encoder=JPEGEncodedImage.encode(bitmap,100);
byte[] array=encoder.getData();
int length=array.length;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
Base64OutputStream base64OutputStream = new Base64OutputStream( byteArrayOutputStream );
try{
base64OutputStream.write( array, 0, length );
base64OutputStream.flush();
base64OutputStream.close();
}
catch (IOException ioe){
//Dialog.alert("Error in encodeBase64() : "+ioe.toString());
System.out.println("Error in encodeBase64() : "+ioe.toString());
}
try {
byte[] data = Base64InputStream.decode(byteArrayOutputStream.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}