gpt4 book ai didi

java - JPEG 格式的原始数据 - JAVA

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:25:52 25 4
gpt4 key购买 nike

我尝试使用 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();
}


}

结果:enter image description here

P.S 这应该是我的照片 btw :)

最佳答案

为什么不将 ByteArrayInputStreamImageIO 一起使用?

您可以在 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
}
}

关于java - JPEG 格式的原始数据 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12576152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com