1

I have a handwritten text as a BufferedImage object. I need to generate it as a tiff image file with CCITT T.4 compression technique.

But CCITT T.4 requires the image to be a 1 bit depth image. My image in the BufferedImage is in 32 bit depth. When I convert it to 1 bit using convert1() funtion, the black handriting is gone and the entire image turns black.

I read elsewhere, that as 1 bit images are black&white, so while converting to 1bit it is converting the transparent layer bits to black. My handwritten notes are in black color too so the entire image becomes black.

I'm able to generte tiff using LZW technique without issues. and even with CCITT T.4 but only on 24 bit images. When the image is 32 bit (with extra transparency layer) the image is turning to black. Can anyone guide me as to how to convert the image to CCITT T.4 format for images with transparency.

Harald K
  • 26,314
  • 7
  • 65
  • 111
Jaison Varghese
  • 1,302
  • 1
  • 13
  • 24
  • 1
    Have you tried putting the image on a white background before converting? If your image is transparent with black text, you can also draw a fully opaque white rectangle "behind" your text using the correct Porter/Duff mode (I think it's `DstAtop`, see `AlphaComposite` class). – Harald K Oct 29 '15 at 12:34
  • I added AlphaComposite.DsAtop and then used setPaint White and fillrect method to set the background as white and now its working. Thanks a ton! However, the black handwrittten text on it is still not continuous lines. The lines are coming as broken (dotted) lines. Any ideas how to smooth out the lines? – Jaison Varghese Oct 29 '15 at 18:20
  • Not entirely sure how the `convert1()` method works (nor do I have your handwriting image), so I'm only guessing. Normally, you can adjust a threshold value, to make more or less pixels black. Or you could try adjusting the brightness/contrast, using [`RescaleOp`](http://stackoverflow.com/a/3433298/1428606). – Harald K Oct 29 '15 at 18:46

1 Answers1

1

Thanks to valuable pointers from haraldK, I was able to convert the 32 bit TIF image to 1 bit image and compress it using CCiTT T.4 compression. The resulting image is clear and has no breaks in the line.

Here's my modified code :

BufferedImage image32bit; 
//image32bit is populated

Graphics2D g=(Graphics2D)image.createGraphics(); 

AlphaComposite ac=AlphaComposite.getInstance(AlphaComposite.DST_ATOP,0.85f);
//0.85f is the opacity threshold value to make more or less pixels black. 

g.setColor(Color.WHITE);
g.fillRect(0,0,image.getWidth(),image.getHeight());
g.dispose()

//Convert to monochrome 1 bit image using image4j jar - convert1 method
BufferedImage singleBitImage=ConvertUtil.convert1(image32bit);

//Use JAI jar to set compression as CCITT T.4 and write out the image
//Insert code to create writer and writeParam ...
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("CCITT T.4");

writer.write(null, iioImage, writeParam);
Jaison Varghese
  • 1,302
  • 1
  • 13
  • 24