java image转byte_java – 将ImageOutputStream转换为byte []

一直在尝试使用JAI将

ImageOutputStream转换为byte []一段时间.任何投入都表示赞赏.谢谢.

对不起,这是代码片段,我正在努力.我不得不提前发布.我面临的问题是,我能够从ImageOutputStream获取ByteArrayOutputStream.但它总是给我零字节.但是如果我使用FileOutputStream而不是ByteArrayOuputStream,我可以写入一个非零字节的文件. :

File file = new File("C:/TIFFImages/tiff-image.tiff");

FileInputStream in = new FileInputStream(file);

long filelength = file.length();

byte[] bytes = new byte[(int)filelength];

int offset = 0;

int numRead = 0;

while (offset < bytes.length && (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {

offset += numRead;

}

if (offset < bytes.length) {

throw new IOException("Could not completely read file "+file.getName());

}

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

RenderedImage src = JAI.create("stream", SeekableStream.wrapInputStream(bais, true));

RenderedOp renderedOp = MedianFilterDescriptor.create(src, MedianFilterDescriptor.MEDIAN_MASK_SQUARE , 1, null);

BufferedImage image = renderedOp.getAsBufferedImage();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

//Instead of baos if I pass a FileOutputStream to the above function. It writes non zero

//bytes to the output file

TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();

ImageWriter writer = tiffspi.createWriterInstance();

RenderedImage renderedImage = PlanarImage.wrapRenderedImage(src);

writer.setOutput(ios);

writer.write(image);

writer.write(null,new IIOImage(image, null, null), param);

System.out.println("After tiff ImageIO operations" + baos.toByteArray().length);

谢谢 ,

维奈

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,将Image对象转换byte数组可以通过以下步骤实现: ```java import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class ImageUtil { public static byte[] imageToByteArray(Image image, String format) throws IOException { BufferedImage bufferedImage = toBufferedImage(image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, format, baos); byte[] bytes = baos.toByteArray(); return bytes; } private static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage) image; } BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); bufferedImage.getGraphics().drawImage(image, 0, 0, null); return bufferedImage; } } ``` 上述代码中,我们定义了一个ImageUtil类,其中包含一个imageToByteArray()方法,用于将Image对象转换byte数组。该方法接受两个参数,第一个参数为要转换Image对象,第二个参数为转换后的图片格式,例如"jpg"、"png"等。该方法实现的具体步骤如下: 1. 将Image对象转换为BufferedImage对象,以便进行后续处理。 2. 创建一个ByteArrayOutputStream对象,用于在内存中存储转换后的byte数组。 3. 调用ImageIO.write()方法将BufferedImage对象转换为指定格式的byte数组,并将转换后的byte数组写入ByteArrayOutputStream对象中。 4. 最终,通过调用ByteArrayOutputStream对象的toByteArray()方法,获取转换后的byte数组。 需要注意的是,上述代码中使用了toBufferedImage()方法将Image对象转换为BufferedImage对象,以便进行后续处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值