开发者

How do I preserve all the original image data in a JPG in Android?

开发者 https://www.devze.com 2023-02-13 13:29 出处:网络
I\'m working with a client whose color style guides are completely immutable, and as part of their app there is a feature where JPGs that they provided are sent to the server.

I'm working with a client whose color style guides are completely immutable, and as part of their app there is a feature where JPGs that they provided are sent to the server.

I'm currently converting the images to bytestream and uploading thusly, but the images arrive with their colors slightly different th开发者_开发问答an the original.

Here's my code for the byte stream creation...

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap b = BitmapFactory.decodeResource(getResources(), uploadImgId);
b.compress(Bitmap.CompressFormat.JPEG, 100, stream);            
byte[] bitmapdata = stream.toByteArray();

I'm using 100% compression, so, I'm not sure why it's altering the image in any way.

Would it be more reliable to move the JPGs to the device's file system and simply upload a file? Alternatively, would it be more reliable to use PNGs instead of JPGs? (I'm guessing PNG treats color information differently than JPG does).

TIA


The executive summary is that you seem to be taking a bitmap and compressing it using JPEG. JPEG is lossy. If colours must be kept exactly right, you need a lossless codec, such as PNG.

The long story is as follows:

JPEG is a lossy codec. It compresses pictures by loosing information in the pictures that most humans will not notice.

This actually can occur in two stages:

A very common, although not mandatory step before JPEG compression is chroma downsampling. As the name implies, this involves loosing certain color information that humans are not usually sensitive to.

In addition, the DCT transformation, which is the next and mandatory step in JPEG, works by eliminating from the picture fine details that humans wont normally notice. This can, in certain pictures (such as with sharp edges) to cause further color changes.

PNG on the other hand is a lossless codec - it works by eliminating redundancy in the information in the image, but never loosing information. It is comprised from RLE followed by Lempel Ziv encoding, which are totally reversible.

In short, use PNG and save the original colors but beware that the pictures will be bigger so uploading time will be longer and cellular data bill might bigger as well.


.JPG & JPEG (Joint Photographic experts Group) compression is a lossy compression mechanism. This means that while the file you uncompressed may look like the original, most likely, if you do a binary compare on the file, you will find lots of differences Wikipedia lossy compression. .JPG (and I think .PNG) files are both based on a discreet cosign transform (A subset of a Fourier transform) and part of the compression throws away any imagery square roots (usually occurs what you hit a sharp boundary/like a straight line in an image), so some information is always lost. The only way to store an image without loss would be to save the raw image as a bit map, or maybe use some of the old run-length encoding schemes that existed before the .JPG days, but don't expect much in the way of compression, maybe 50% reduction at best case.

Msg back if you need a better explination, but it will get very long winded - Joe

0

精彩评论

暂无评论...
验证码 换一张
取 消