java byte bitmap_如何将byte []转换为Bitmap而不会降低质量

在Android中,从相机的onPictureTaken方法获取的字节数组转换为Bitmap时出现了质量损失。直接保存字节到文件可以保持更好的质量。问题在于BitmapFactory.decodeByteArray可能会导致图像质量下降。如何在转换过程中避免质量损失是关键。
摘要由CSDN通过智能技术生成

我的目标是

从相机onPictureTaken捕获字节

将byte []转换为Bitmap BitmapFactory.decodeByteArray

保存位图

我在步骤2中遇到问题 . 由于某种原因,此步骤会使图片包含锯齿状边缘和质量损失 . 如果我执行第1步并将字节直接保存到文件中(没有第2步),那么图像看起来好多了 .

如何在不损失质量的情况下将字节转换为位图(步骤2)?

@Override

public void onPictureTaken(byte[] byteData, Camera camera) {

String filePath = "PATH_TO_IMAGE_FILE";

//Save bytes directly

{

File pictureFile = new File(filePath + ".fromBytes");

try {

FileOutputStream fos = new FileOutputStream(pictureFile);

fos.write(byteData);

fos.close();

} catch (Exception error) {

Log.d( "File not saved: " , error.getMessage());

}

}

//convert to bitmap -> convert to bytes -> then save

{

//I think this is where is loses quality ??

Bitmap decodedBitmap = BitmapFactory.decodeByteArray(byteData, 0, byteData.length);

//

ByteArrayOutputStream blob = new ByteArrayOutputStream();

decodedBitmap.compress(Bitmap.CompressFormat.PNG, 100, blob);

byte[] bitmapdata = blob.toByteArray();

File pictureFile = new File(filePath + ".fromBitmap");

try {

FileOutputStream fos = new FileOutputStream(pictureFile);

fos.write(bitmapdata);

fos.close();

} catch (Exception error) {

Log.d( "File not saved: " , error.getMessage());

}

}

}

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值