将原始数据转换为JPEG格式

将原始数据转换为JPEG格式

本文介绍了将原始数据转换为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 .

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:36