Streaming the contents of scanned images to files in Java

I'm trying to scan images using Morena and sane and save them to a file in a given format (TIFF or JPEG) using the swing application

I use this process to load the entire image into memory:

SaneSource source = /* source implemented here */;
MorenaImage morenaImage = new MorenaImage(source);

Image image=Toolkit.getDefaultToolkit().createImage(morenaImage);
BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics2D g = bimg.createGraphics();
g.drawImage(image,null);

ImageIO.write(bimg,"jpg",new File(filename));

I'm sure there is a better way to do this without occupying all my memory, such as transferring my content in the scanned image to a file through the consumer / observer, but I can't wrap it well and create my own solution

Can you help me embark on the road of better image processing? Thank you first, David

Solution

You should attach the imageconsumer (which will write the image to the OutputStream using your preferred image format) directly to the imageproducer (you can use sanesource or morenaimage if you like) You can find an imageconsumer example that encodes an image into ppm and transfers it to OutputStream here You need to write something like this to use this example:

ImageProducer prod = ... your producer here ....;
PpmEncoder ppm = new PpmEncoder(prod,myOutputStream);
ppm.encode();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>