Scan QR code in PDF

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.ns.support.QrPdf;

public class My20190205QrCodeTest {

	private static String path = "C:\\NS2\\test_files\\520044839973-ARV.pdf";//

	public static void main(String[] args) {

		My20190205QrCodeTest a = new My20190205QrCodeTest();
		try {
			a.testPdf();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("---------------- END ------------------");
	}

	private void testPdf() {
		try {
			String line = scanQRCode(1);
			System.out.println("---------------- test7 result:::" + line);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private String scanQRCode(int pageIndex) {

		// Hints for scanning
		Vector<BarcodeFormat> decodeFormat = new Vector<BarcodeFormat>();
		decodeFormat.add(BarcodeFormat.QR_CODE);

		Hashtable<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>();
		hintMap.put(DecodeHintType.TRY_HARDER, true);
		hintMap.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormat);

		MultiFormatReader qrcodeReader = new MultiFormatReader();
		qrcodeReader.setHints(hintMap);

		try {
			// Try lowest DPI first.
			BufferedImage pageImage = getPageImage();
			LuminanceSource source = new BufferedImageLuminanceSource(pageImage);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			// By using decodeWithState, we keep the Hints that we set earlier.
			Result result = qrcodeReader.decodeWithState(bitmap);
			String text = result.getText();
			// System.out.println(" -- text:::" + text);
			return text;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();

		}
		// This should never happen, ever...
		return null;
	}

	private BufferedImage getPageImage() throws IOException {
		Path docPath = Paths.get(path);
		PDDocument pdfDoc = PDDocument.load(docPath.toFile());
		PDFRenderer renderer = new PDFRenderer(pdfDoc);
		// renderImageWithDPI(page number, image size, format)
		BufferedImage image = renderer.renderImageWithDPI(0, 150, ImageType.BINARY); // entire page info
		// BufferedImage image = renderer.renderImage(0);
		pdfDoc.close();
		return image;
	}
}

Spring REST PUT byte array

POM.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>

API


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

private void test() {

	byte[] bytes = readBytesFromFile(path);
	// Prepare acceptable media type
	List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
	acceptableMediaTypes.add(MediaType.ALL);

	String uri = "http://localhost:8080/upload/{company}/{terminal}/{user}/{docType}/{docNum}/{trx}/?addendum=false&index=0&channel=ui";
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

	// Prepare header
	HttpHeaders headers = new HttpHeaders();
	headers.setAccept(acceptableMediaTypes);
	HttpEntity<byte[]> entity = new HttpEntity<byte[]>(bytes, headers);

	Map<String, String> param = new HashMap<String, String>();
	param.put("company", "750");
	param.put("terminal", "7501");
	param.put("user", "cmanshande");
	param.put("docType", "HAWS");
	param.put("docNum", "20222608918");
	param.put("trx", "750110714862");

	// Send the request as PUT
	restTemplate.exchange(uri, HttpMethod.PUT, entity, byte[].class, param);
}

Client

@RequestMapping(value = "/upload/{company}/{terminal}/{user}/{docType}/{txId}/**", method = RequestMethod.PUT)
@ResponseBody
public ResponseEntity<String> processUpload(
		@RequestBody byte[] content,
		@PathVariable String company, 
		@PathVariable String terminal,
		@PathVariable String user, 
		@PathVariable String docType,
		@PathVariable String txId, 
		HttpServletRequest httpRequest) {
.
.
.
.
}