简体   繁体   中英

Stream the content of a scanned image to a file in Java

I'm trying to scan an image and save it to a file given a specific format (Tiff or Jpeg) with a Swing application, using Morena and Sane.

I load the whole image in memory with this process:

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, 0, 0, null);

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

I'm pretty sure there is a better way to do this without eating all my memory, like streaming the content of my scanned image in cache to the file with a Consumer / Observer, but I couldn't wrap my mind good enough around these notions to create my own solution.

Could you please help me down the path to better image processing? Thanks in advance, david

You should attach an ImageConsumer (that will write image to OutputStream using your favorite image format) directly to an ImageProducer (SaneSource or MorenaImage if you wish). You can find ImageConsumer example that encodes image as PPM and transfers it to OutputStream here . You'll need to write something like this to use this example:

ImageProducer prod = ... your producer here ....;
PpmEncoder ppm = new PpmEncoder(prod, myOutputStream);
ppm.encode();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM