如何将二进制图像数据转换为 Java 中的 jpg 文件,其中二进制数据中的每 10 位代表一个像素? [英] How to convert a binary image data to a jpg file in Java where every 10bits in the binary data represent a pixel?
问题描述
我正在通过 以太网
接收比特流.我正在 Java
中的 byte[]
数组中收集位(我将它们收集在 byte[] 中,因为我认为它相关).流是数字化图像其中每 10 位代表一个像素.有 1280*1024
个像素.每个像素由 10 位表示.因此,1280*1024*10 = 13107200 bits = 1638400 bytes
是图像大小.
I am receiving a stream of bits over the Ethernet
. I am collecting the bits in a byte[]
array in Java
(I am collecting them in a byte[] because I think its relevant).The stream is a digitized image where every 10 bits represent a pixel. There are 1280*1024
pixels. Every pixel is represented by 10 bits. Hence,1280*1024*10 = 13107200 bits = 1638400 bytes
is the image size.
推荐答案
这里有一个方法,可以将一个字节数组拆分"成 10 位一组.每个组都保存为一个整数.
Here is a method that can take a byte array and "split" it into groups of 10 bit. Each group is saved as an int.
static int[] getPixel(byte[] in) {
int bits = 0, bitCount = 0, posOut = 0;
int[] out = new int[(in.length * 8) / 10];
for(int posIn = 0; posIn < in.length; posIn++) {
bits = (bits << 8) | (in[posIn] & 0xFF);
bitCount += 8;
if(bitCount >= 10) {
out[posOut++] = (bits >>> (bitCount - 10)) & 0x3FF;
bitCount -= 10;
}
}
return out;
}
这篇关于如何将二进制图像数据转换为 Java 中的 jpg 文件,其中二进制数据中的每 10 位代表一个像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!