Raw data to JPEG format - JAVA
Asked Answered
N

4

10

I tried to convert raw data ByteArray to JPEG format using JPEGEncoder but its too slow in mobile (I've tested it on mobile). How can I do the same thing in java? I will send raw data byte to java and encode it to JPEG with java - I tried some of them as JpegImageEncoder under com.sun.* but it's depreciated in jdk7. How can I do this in java Or any suggestions from Flex mobile developers who have done such thing?

UPDATE: I tried the following code but I'm getting a strange result:

public void rawToJpeg(byte[] rawBytes, int width, int height, File outputFile){

        try{

            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

            int count = 0; 
            for(int h=0;h<height;h++){
                for(int w=0;w<width;w++){
                    bi.setRGB(w, h, rawBytes[count++]);
                }
            }


            Graphics2D ig2 = bi.createGraphics();

            Iterator imageWriters = ImageIO.getImageWritersByFormatName("jpeg");
            ImageWriter imageWriter = (ImageWriter) imageWriters.next(); 

            ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);
            imageWriter.setOutput(ios);
            imageWriter.write(bi);


        }catch(Exception ex){
            ex.printStackTrace();
        }


    }

RESULT: enter image description here

P.S It should be my photo btw :)

Nameplate answered 25/9, 2012 at 4:18 Comment(6)
I have the same problem. Have you found any solution?Pylorus
No, for now I'm doing all encodings on client side, but it takes long to processNameplate
To me doesn't matter in client or server, what language and technique are you using now?Pylorus
I'm using Flex Mobile on client side and I'm doing encoding using ActionScript.. I can give it to you if it fits you (its not a best solution for me as it takes a while to encode ByteArray on mobile devices)Nameplate
Would you please send it to me (post at ehsunbehravesh dot com) it may give me some idea.Pylorus
Does jpeg have alpha? A_RBG might be the problem.Statuary
C
2

Why not use a ByteArrayInputStream with ImageIO?

You find more Information about ImageIO in the API.

public static void rawToJpeg(byte[] bytes, File outputFile) {
    try {
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
        ImageIO.write(img, "jpg", outputFile);
    } catch (IOException e) {
        // Handle exception
    }
}
Chicken answered 28/9, 2012 at 19:48 Comment(3)
BufferedImage img = ImageIO.read(new ByteArrayInputStream(resimBytes)); return null img object that causes an java.lang.IllegalArgumentException !!!Nameplate
I created bytes by reading a JPEG image in Java and then passed that byte array to this piece of code. Everything worked fine. In the API of Java is written: "If no registered ImageReader claims to be able to read the stream, null is returned." I suggest you check if there are any ImageReaders available and if your byte array is really a JPEG image.Chicken
Oh, I see my blindness. I assumed your byte array contained a JPEG. Instead, it is raw color data. Sorry.Chicken
H
0

bi.setRGB takes a 4 byte "int" value, which is the ARGB 0xAARRGGBB

You then increment your byte offset counter by ONE, so the next pixel will get 0xRRGGBBAA, then 0xGGBBAARR and so forth.

Assuming the byte[] you are passing is in the correct 4 byte format, you need to either be adding 4 to "count" each time, or change what you pass to an int[] (which would actually be more correct, since it really does contain int values).

Hattiehatton answered 16/3, 2013 at 21:38 Comment(0)
C
0

Hi i was facing same problem, i was setting the width and height values as hardcoded lets say (300,300) causing similar output. then i referenced this link. Raw byte[] to jpeg image you can ignore the bitmap part in it. I am assuming you are also hardcoding the width and height values.

Cystolith answered 2/6, 2014 at 12:39 Comment(0)
L
0

You could try to replace your for-loops by this

for(int w = 0; w < width; w++)
{
    for(int h = 0; h < height; h++)
    {
            //alpha should be eiter 0 or 255
            //if you use the wrong value your image will be transparent

            int alpha = 0 << 8*3;
            int red = rawBytes[count*3 + 0] << 8*2;
            int green = rawBytes[count*3 + 1] << 8*1;
            int blue = rawBytes[count*3 + 2] << 8*0;

            int color = alpha + red + green + blue;

            //color is an int with the format of TYPE_INT_ARGB (0xAARRGGBB)

            bi.setRGB(w, h, color);
            count += 3;
    }
}

Things that may went wrong with your code:

  1. You usually write line by line not row by row

  2. You need to read 3 bytes and build an int instead of writing the bytes directly in your Pixel (TYPE_INT_ARGB)

This link explains TYPE_INT_ARGB: Format of TYPE_INT_RGB and TYPE_INT_ARGB

I hope this helps a bit and isn't too confusing =)

Loxodromics answered 3/8, 2014 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.