如何在 Android 中以 JPG 格式保存所有原始图像数据?

发布于 2024-10-19 01:33:02 字数 553 浏览 1 评论 0原文

我正在与一个客户合作,其颜色样式指南是完全不可变的,作为他们的应用程序的一部分,有一个功能可以将他们提供的 JPG 发送到服务器。

我目前正在将图像转换为字节流并因此上传,但图像到达时的颜色与原始图像略有不同。

这是我的字节流创建代码...

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

我使用 100% 压缩,所以,我不确定为什么它会以任何方式改变图像。

将 JPG 移动到设备的文件系统并简单地上传文件会更可靠吗? 或者,使用 PNG 代替 JPG 会更可靠吗? (我猜 PNG 处理颜色信息的方式与 JPG 不同)。

TIA

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 than 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城仅有旧梦在 2024-10-26 01:33:02

执行摘要是,您似乎正在获取位图并使用 JPEG 对其进行压缩。 JPEG 是有损的。如果必须保持颜色完全正确,则需要无损编解码器,例如 PNG。

长话短说:

JPEG 是一种有损编解码器。它通过丢失图片中大多数人不会注意到的信息来压缩图片。

这实际上可以分两个阶段进行:

在 JPEG 压缩之前,一个非常常见但不是强制性的步骤是色度下采样。顾名思义,这涉及丢失人类通常不敏感的某些颜色信息。

此外,DCT 变换是 JPEG 的下一个强制性步骤,它的工作原理是从图片中消除人类通常不会注意到的细节。在某些图片(例如具有锐利边缘的图片)中,这可能会导致进一步的颜色变化。

另一方面,PNG 是一种无损编解码器 - 它的工作原理是消除图像中信息的冗余,但绝不会丢失信息。它由 RLE 和 Lempel Ziv 编码组成,这是完全可逆的。

简而言之,使用 PNG 并保存原始颜色,但要注意图片会更大,因此上传时间会更长,蜂窝数据费用也可能会更高。

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.

假装爱人 2024-10-26 01:33:02

.JPG 和JPEG(联合图像专家组)压缩是一种有损压缩机制。这意味着,虽然您解压缩的文件可能看起来像原始文件,但如果您对文件进行二进制比较,很可能会发现很多差异 维基百科有损压缩。 .JPG(我认为 .PNG)文件都基于离散余弦变换(傅里叶变换的子集),并且部分压缩会丢弃任何图像平方根(通常发生在遇到尖锐边界/像直线一样的情况)图像中的线),因此总会丢失一些信息。无损存储图像的唯一方法是将原始图像保存为位图,或者可能使用 .JPG 时代之前存在的一些旧的游程长度编码方案,但不要对这种方式期望太多压缩率,最好情况下可能减少 50%。

如果您需要更好的解释,请回复消息,但这会变得很冗长 - Joe

.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文