我尝试使用 JPEGEncoder 将原始数据 ByteArray 转换为 JPEG 格式,但在移动设备上速度太慢(我已经在移动设备上测试过)。我怎样才能在java中做同样的事情?我将把原始数据字节发送到java并用java将其编码为JPEG - 我在com.sun.*下尝试了其中一些作为JpegImageEncoder,但它在jdk7中被贬值了。我怎样才能在java中做到这一点或者做过这样的事情的Flex移动开发人员有什么建议吗?
更新:我尝试了以下代码,但得到了一个奇怪的结果:
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:
P.S 顺便说一句,这应该是我的照片:)
为什么不使用一个ByteArrayInputStream
with ImageIO
?
您可以在以下位置找到有关 ImageIO 的更多信息API http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html.
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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)