Stream content of scanned image to file in Java

I am trying to scan an image and save it to a file based on a specific format (Tiff or Jpeg) using the Swing application using Morena and Sane.

I load the entire image into memory using 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 am sure that there is a better way to do this without eating all of my memory, for example, streaming the contents of my scanned image in a cache with a file using Consumer / Observer, but I could not recoup my mind enough around these concepts to create my own solution.

Could you help me along the way of improving image processing? Thanks in advance, david

+4
source share
1 answer

You must attach ImageConsumer (which will record the image to an OutputStream using your favorite image format) directly in ImageProducer (SaneSource or MorenaImage, if you want). You can find an example ImageConsumer that encodes an image as PPM and passes it to an 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(); 
+1
source

Source: https://habr.com/ru/post/1383913/


All Articles