Barcode reading on PDF pages

I would like to release two useful JAVA functions to help decoding PDF documents to image and scan for barcodes on…

These functions are parts of my Barcode Tools Server, what can recognize and stamp barcodes from/on PDF documents.

Required classes:
JPEDAL (Java Pdf Extraction Decoding Access Library)
J4L Barcode Vision for the Java Platform

PDF2Image: read PDF document from a file, URL or byte array, and render a page with specified resolution (dpi) into an image

public static BufferedImage PDF2Image(String pdfFile, String pdfUrl,
		byte[] pdfArray, int page, int dpi) {

	BufferedImage image = null;

	try {
		PdfDecoder decoder = new PdfDecoder();

		decoder.setExtractionMode(PdfDecoder.RAWIMAGES
				+ PdfDecoder.FINALIMAGES + PdfDecoder.XFORMMETADATA
				+ PdfDecoder.TEXT, dpi, dpi / 72);

		if (pdfFile != null) {
			decoder.openPdfFile(pdfFile);
		} else if (pdfUrl != null) {
			decoder.openPdfFileFromURL(pdfUrl);
		} else if (pdfArray != null) {
			decoder.openPdfArray(pdfArray);
		} else {
			// missing input
			System.err.println("Missing PDF input, please specify a file, an url or a stream");
			return (null);
		}

		// print PDF info
		System.out.println(" Number of pages: "
				+ decoder.getNumberOfPages());
		System.out.println(" Encrypted: " + decoder.isEncrypted());

		if (decoder.isFileViewable()) {
			image = decoder.getPageAsImage(page);
		} else {
			// problem with image!
			System.err.println("Problem with image...");
		}

		decoder.flushObjectValues(true);

		decoder.closePdfFile();
	} catch (Exception err) {
		err.printStackTrace();
	}

	return (image);
}

scanImage: read image and scan for barcodes with specified type (barcodeType)

public static int[] symbologies = new int[] {
		Barcode1DReader.CODE39 | Barcode1DReader.CODE128
				| Barcode1DReader.EAN8 | Barcode1DReader.EAN13
				| Barcode1DReader.IDENTCODE | Barcode1DReader.INTERLEAVED25
				| Barcode1DReader.UPCA | Barcode1DReader.UPCE, // 0 = ALL
		Barcode1DReader.CODE39, // 1
		Barcode1DReader.CODE128, // 2
		Barcode1DReader.EAN8, // 3
		Barcode1DReader.EAN13, // 4
		Barcode1DReader.IDENTCODE, // 5
		Barcode1DReader.INTERLEAVED25, // 6
		Barcode1DReader.UPCA, // 7
		Barcode1DReader.UPCE // 8
};

public static BarcodeData[] scanImage(BufferedImage image, int barcodeType)
		throws Exception {

	// set the system property for minimal barcode image length
	System.setProperty("com.java4less.vision.minbarlength", "10");

	// init barcode reader
	Barcode1DReader bcreader = new Barcode1DReader();

	// set barcode type(s)
	if (barcodeType <= 0 || barcodeType >= symbologies.length) {
		// set all barcode types to scan
		bcreader.setSymbologies(symbologies[0]);
	} else {
		// set one barcode type to scan (faster)
		bcreader.setSymbologies(symbologies[barcodeType]);
	}

	// disable progress view
	bcreader.setProgressListener(null);

	// start barcode scan
	BarcodeData[] barcodes = null;
	try {
		barcodes = bcreader.scan(new RImage((BufferedImage) image));
	} catch (VisionException e) {
		// Error in barcode reader
		e.printStackTrace();
		return (null);
	} finally {

	}

	return (barcodes);
}