How to keep image quality same in BitmapFactory

后端 未结 2 530
攒了一身酷
攒了一身酷 2021-01-22 07:17

I\'ve converted an bitmap image into string to save it:

............
Bitmap photo = extras.getParcelable(\"data\");
ByteArrayOutputStream baos = new ByteArrayOut         


        
相关标签:
2条回答
  • 2021-01-22 07:50

    JPEG is lossy, no matter what quality settings you use. If you want to keep the image unchanged, you have to use lossless compression. for example Bitmap.CompressFormat.PNG

    0 讨论(0)
  • 2021-01-22 08:03

    You are having here a tradeoff situation between picture quality and memory usage. Take a look at this line:

    photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    

    photo.compress is obviously decreasing your image resolution in a factor given by the second parameter, unfortunately, this is the best quality you can get, since between 0 - 100, 100 stands for the best quality you can get. Now, you have another option, depending on the original picture's size you can just save the image without compressing it, but be aware that most cases this doesn't work and Jalvik can throw an OutofMemoryException, hope this helps.

    0 讨论(0)
提交回复
热议问题