Binarize a color pdf page to a bw image (JAVA)

I would like to convert a pdf page to a 1bit bw image via Bradley’s method.
Finally I need the result as a BufferedImage. So far I did this which works:

public static BufferedImage renderBinarizedImage(Document document, int pageNumber, int renderDpi) {
	Page page = document.getPages().get_Item(pageNumber);
	PngDevice pngDevice = new PngDevice(new Resolution(renderDpi));
	BufferedImage rgbImage = pngDevice.processToBufferedImage(page);
	try (RasterImage rasterImage = ImageExtensions.fromJava(rgbImage)) {
		rasterImage.binarizeBradley(8.0);
		BufferedImage result = ImageExtensions.toJava(rasterImage, BufferedImage.TYPE_BYTE_BINARY);
		return result;
	}
}

Is there a simpler way to binarize a pdf page into a BufferedImage without using the file system like in the code above?

E.g. I hoped to find something like this
RasterImage rasterImage = page.binarizeBradley(8.0);

Or something at the device level
RasterImage rasterImage = pngDevice.processToRasterImage(page);

@martin.duerig

At the moment, the API offers a way to binarize TIFF image using Bradley Algorithm. However, we have logged an investigation ticket as PDFJAVA-40408 in our issue tracking system for your specific requirements. We will further look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like

The issues you have found earlier (filed as PDFJAVA-40408) have been fixed in Aspose.PDF for Java 21.5.