0

I develop an app which involves a step of uploading image to server.But the server only support jpeg format image,so I have to convert the png format images picked or captured by users on client side.So I use the follow codes do this image format transformation:

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

but some images will loss quality,compared to png format and the eyes can see the loss.Does this method has some bugs?Are there some good methods to do png to jpeg transformation without loss quality in Android?

WeiDian_Jake
  • 55
  • 1
  • 1
  • 7

1 Answers1

0

JPEG is a lossy compression format, it will have data loss. See the post here:

Saving JPEG files in Android with no loss of pixel information

The idea behind "compression" is to reduce the file size, but there is always a tradeoff. You may try increasing the size of the PNG file and then doing "compression" - but then you may need to scale it back down and you are back where you started.

NOTE: PNG is not "lossy" and is "compressed" compared to some other formats, e.g. Bitmap. I originally posted with the assumption that compression = lossy, but some formats are considered "compressed" simply because they require less storage and without loss. For example, Zip is a file format that will change file data organization to store them with less space and without loss.

This is a different type of compression from what JPEG does, which alters the information in order to "compress" the file, and store it in a smaller file. Compression of this type is fundamentally different.

Jim
  • 10,172
  • 1
  • 27
  • 36
  • I'd like to ask why my compressed PNG file size is bigger than the original PNG? – BNK Sep 02 '15 at 04:49
  • There could be several reasons - first, jpeg at "100%" or an attempt at zero loss is not very efficient. And, like maybe in your case, it will do worse than other algorithms that are lossless. There may be other parameters you can change, for example the color depth on your PNG may be lower, or for images with a flat distribution and wide variety of colors the JPEG algorithm may be very inefficient in general. – Jim Sep 02 '15 at 04:57
  • I don't convert PNG to JPG, but PNG to PNG (it means that I create byte array from the resource PNG file in drawable folder to upload to web service) – BNK Sep 02 '15 at 05:08
  • for example, I convert some png images to jpeg,it will be dark then before – WeiDian_Jake Sep 02 '15 at 06:49
  • @Jim: my code here `private byte[] getFileDataFromDrawable(Context context, int id) { Drawable drawable = ContextCompat.getDrawable(context, id); Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); }` After check uploaded PNG file in server, its size and aspect ratio are always larger than original file – BNK Sep 02 '15 at 07:28
  • This is def a different question from your original. Also, note that you are converting your PNG to a `Drawable` and then using the bitmap of that drawable to re-create a PNG. The output would depend on the canvas size of the `Drawable` not the original PNG – Jim Sep 02 '15 at 16:26
  • And other factors that impact `Drawable` creation (like sampling, etc.) – Jim Sep 02 '15 at 16:26
  • "JPEG is a compression format, it will have data loss", the former doesn't imply the latter. PNG is also a compression format but a lossless one. The correct phrase is "JPEG is a __lossy__ compression format" – Dan M. Dec 06 '18 at 12:02