简体   繁体   English

原始数据为JPEG格式 - JAVA

[英]Raw data to JPEG format - JAVA

I tried to convert raw data ByteArray to JPEG format using JPEGEncoder but its too slow in mobile (I've tested it on mobile). 我试图使用JPEGEncoder将原始数据ByteArray转换为JPEG格式,但它在移动设备上的速度太慢(我在移动设备上测试过它)。 How can I do the same thing in java? 我怎样才能在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. 我将原始数据字节发送到java并使用java将其编码为JPEG - 我尝试将它们中的一些作为com.sun。*下的JpegImageEncoder。但它在jdk7中被折旧。 How can I do this in java Or any suggestions from Flex mobile developers who have done such thing? 我如何在java中执行此操作或者已经完成此类操作的Flex移动开发人员的任何建议?

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: 结果: 在此输入图像描述

PS It should be my photo btw :) PS它应该是我的照片顺便说一句:)

Why not use a ByteArrayInputStream with ImageIO ? 为什么不在ImageIO使用ByteArrayInputStream

You find more Information about ImageIO in the API . 您可以在API中找到有关ImageIO的更多信息。

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

bi.setRGB takes a 4 byte "int" value, which is the ARGB 0xAARRGGBB bi.setRGB采用4字节“int”值,即ARGB 0xAARRGGBB

You then increment your byte offset counter by ONE, so the next pixel will get 0xRRGGBBAA, then 0xGGBBAARR and so forth. 然后,将字节偏移计数器递增1,这样下一个像素将获得0xRRGGBBAA,然后是0xGGBBAARR,依此类推。

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). 假设您传递的byte []是正确的4字节格式,您需要每次添加4到“count”,或者更改传递给int []的内容(这实际上更正确,因为它确实包含int值)。

Hi i was facing same problem, i was setting the width and height values as hardcoded lets say (300,300) causing similar output. 嗨,我面临同样的问题,我设置宽度和高度值硬编码让我们说(300,300)导致类似的输出。 then i referenced this link. 然后我引用了这个链接。 Raw byte[] to jpeg image you can ignore the bitmap part in it. 原始byte []到jpeg图像可以忽略其中的位图部分。 I am assuming you are also hardcoding the width and height values. 我假设你也在硬编码宽度和高度值。

You could try to replace your for-loops by this 您可以尝试用此替换for循环

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) 您需要读取3个字节并构建一个int而不是直接在Pixel(TYPE_INT_ARGB)中写入字节

This link explains TYPE_INT_ARGB: Format of TYPE_INT_RGB and TYPE_INT_ARGB 此链接说明TYPE_INT_ARGB:TYPE_INT_RGB和TYPE_INT_ARGB的格式

I hope this helps a bit and isn't too confusing =) 我希望这有点帮助,而不是太混乱=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM