jpeg decoding when data is given in array - java

i am using LibJpeg library for decoding jpeg image ( given in form of byte array ) into rgb color map .
but it come different from my sample output i want to check by java programme .
how to do this in by java programme ?
what is use of APPn in header ?
how to decode jpeg image into rgb pixel .

Why not use ImageIO.read together with ByteArrayInputStream to read the byte array into an image then no extra library is needed (pure java solution):
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

Related

Android Camera2 API Image Color space

I used this Tutorial to learn and try understand how to make a simple picture taking android app using the Camera2 API. I have added some snippets from the code to see if you all can help me understand some questions I have.
I am trying to find out how the image is saved as. Is it RGB, or BGR?
Is it stored in the variable bytes?
ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG, 1);
#Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
}
The image is received in JPEG format (as specified in the first line). Android uses YUV (to be more exact, YCbCr) color space for JPEG. Jpeg size is variable, it is compressed with lossy compression, and you have very little control over the level of compression.
Normally, you receive a JPEG buffer in onImageAvailable() and decode this JPEG to receive a Bitmap. You can get pixels of this Bitmap as an int array of packed SRGB pixels. The format for this array will be ARGB_8888.
You don't need JNI to convert it to BGR, see this answer.
You can access Bitmap objects from C++, see ndk/reference/group/bitmap. There you can find the pixel format of this bitmap. If it was decoded from JPEG, you should expect this to be ANDROID_BITMAP_FORMAT_RGBA_8888.
The variable bytes contains an entire compressed JPEG file. You need to decompress it to do anything much with it, such as with BitmapFactory.decodeByteArray or ImageDecoder (newer API levels).
It's not an uncompressed array of RGB values in any sense. If you want uncompressed data, the camera API supports the YUV_420_888 format, which will give you uncompressed 4:2:0 YUV data; still not RGB, though.

Java - reading a compressed bitmap

I'm looking to read in the RGB values of a bitmap (or the hex colour codes, either work).
I have tried both this code :
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
dimX = buffer.getWidth();
dimY = buffer.getHeight();
byte[] pixlesB = (byte[]) buffer.getRaster().getDataElements(0, 0, buffer.getWidth(), buffer.getHeight(), null);
and this code :
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffer, "bmp", baos );
baos.flush();
byte[] pixlesB = baos.toByteArray();
baos.close();
The both work fine for a small bitmap, but when I load a large bitmap, the data gets compressed and the array returns a bunch of semi random numbers.
for example:
A green pixel will read 2,2,2 instead of 0,255,0
A red pixel will read 5,5,5 instead of 255,0,0
A yellow pixel will read 8,8,8 instead of 255,255,0
The bitmaps I'm using only include the colours red, yellow and green.
My problem is I have no way of knowing what colour 2,2,2 relates to without checking it manually (which I cannot do since it changes with each bitmap)
I know that there is some metadata in the bitmap that specifies 2 is green, but I don't know how to access it or use it to turn 2 back into 0,255,0
And this is not a duplicate of Java - get pixel array from image since that doesn't mention compressed files.
And while I did ask this question a while back, it was just redirected to the above site.
Thanks in advance!
EDIT: Just thought this might make the question a bit clearer. I believe the file is being read correctly, it is just compressed. How do I decompress it?
If you want to read an image as a bitmap or as rgb values, you need to transform the image's format first.
Jpeg is a compressed image format, you need to use a tool or library in order to read as rgb.
check this answer:
How to get the rgb values of a jpeg image in TYPE_3BYTE_BGR?
Hope this helps

Modify image data in ByteBuffer

I would like to modify the per-pixel RGB color data of a PNG image that I have loaded as a ByteBuffer, preferably a simple, lightweight solution.
I currently load the data directly from the file into a ByteBuffer using a ReadableByteChannel, which does not decode the PNG data.
So the question is, how do I
Decode the ByteBuffer PNG data into something where I can modify the pixel data
Turn it back into a valid ByteBuffer ('valid' means that it would be accepted by an OpenGl shader)
The PNG image encoding includes (among other things) a ZLIB compression, you cannot access the pixel values directly. You need (practically speaking) to decode the image, for example reading it into a BufferedImage with ImageIO.read( )
If for some reason (huge image, memory constraints) you don't like to load the full image in memory in a BufferedImage you could use a progressive reader (eg PNGJ)

Converting Byte Array of a JPEG Image but with wrong extension

I am having an image file downloaded as byte array inside my java method. Need to convert this byte array into BufferedImage.
InputStream inputStream = new ByteArrayInputStream(imagebyteArray);
BufferedImage originalImage = ImageIO.read(inputStream);
ImageIO.write(originalImage, "jpg", new File("/Users/abc/test.jpg"));
Above code snippet is used to convert the image byte array and write into jpeg file. But I am getting a zero sized image file.
But issue with the downloaded image. Image is actually JPEG format but the extension is wrong like image.doc
Is it a problem ? , if that is an issue Is there any way to convert the extension of the image (byte array) using java2d , and then convert to BufferedImage?

How to convert inputstream of grayscale image to binary?

I am reading an grayscale tiff image into inputstream using
InputStream is=objCMBObject.getDataStream();
bytes = IOUtils.toByteArray(is);
response.setContentType(mimeType);
OutputStream os = response.getOutputStream();
and writing this stream into applet .but now I want to convert this grayscale image to binary image before writing to the applet. I don't want to save the image in file.
How to convert inputstream of grayscale to binary?
I can convert the image into binary if I have x,y coordinates but I don't know how to get it from inputstream.
Please guide.
When you want to manipulate an image, javax.imageio.ImageIO can be of great use. You can use that to load the image from the stream, apply some operation like a ColorConvertOp, and write the result back to a stream.
As ImageIO doesn't neccessarily support TIFF out of the box, you might have to use a suitable library to provide TIFF support. Answers to another question suggest using Java Advanced Imaging for this.

Categories