原始数据转JPEG格式 - JAVA [英] Raw data to JPEG format - JAVA

查看:35
本文介绍了原始数据转JPEG格式 - JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 JPEGEncoder 将原始数据 ByteArray 转换为 JPEG 格式,但在移动设备中速度太慢(我已经在移动设备上对其进行了测试).我怎样才能在java中做同样的事情?我将原始数据字节发送到 java 并使用 java 将其编码为 JPEG - 我在 com.sun.* 下尝试了其中一些作为 JpegImageEncoder,但它在 jdk7 中贬值了.我怎样才能在 java 中做到这一点,或者来自做过这种事情的 Flex 移动开发人员的任何建议?

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();
        }


    }

结果:

P.S 顺便说一句,应该是我的照片 :)

P.S It should be my photo btw :)

推荐答案

为什么不将 ByteArrayInputStreamImageIO 一起使用?

Why not use a ByteArrayInputStream with ImageIO?

您可以在 API<中找到有关 ImageIO 的更多信息/a>.

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

这篇关于原始数据转JPEG格式 - JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆