我的目标是

  • 从相机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());

        }
    }
}

谢谢