First, the idea of implementation

1 correction picture this can refer to the previous article Java call OpencV picture correction

2. Because the size of the ID card is fixed, the area where the ID number can be intercepted in proportion can be used

3. Identify the captured image with Tess4J

4. The rendering

Two, part of the code

2.1 Picture Editing

public static Mat cutRect(Mat image) { Mat clone=image.clone(); Mat src=image.clone(); Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0); HighGui.imshow("GaussianBlur1", clone); Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY); HighGui.imshow("GRY1", clone); int lowThresh=20; Imgproc.Canny(clone, clone,lowThresh, lowThresh*3,3); HighGui.imshow("Canny1", clone); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(Clone, contours, hierarchy, imgproc. RETR_EXTERNAL, imgproc.chain_approx_None); System. The out. Println (" outline: "+ contours. The size ()); Double area = imgproc.boundingRect (contours.get(0)).area(); double area = imgproc.boundingRect (contours.get(0)).area(); int index = 0; For (int I = 0; i < contours.size(); i++) { double tempArea = Imgproc.boundingRect(contours.get(i)).area(); if (tempArea > area) { area = tempArea; index = i; } } MatOfPoint2f matOfPoint2f = new MatOfPoint2f(contours.get(index).toArray()); RotatedRect rect = Imgproc.minAreaRect(matOfPoint2f); Mat temp = new Mat(src , rect.boundingRect()); Mat t = new Mat(); temp.copyTo(t); HighGui.imshow("cut", temp); return t; }Copy the code

2.2 Id card extraction location reference

Public static String card(Mat Mat){Point point1=new Point(mat.cols()*0.34,mat.rows()*0.80); Point point2 = new Point (mat. Cols () * 0.34, mat. The rows () * 0.80); Point point3 = new Point (mat. Cols () * 0.89, mat. The rows () * 0.91); Point point4 = new Point (mat. Cols () * 0.89, mat. The rows () * 0.91); List<Point> list=new ArrayList<>(); list.add(point1); list.add(point2); list.add(point3); list.add(point4); Mat card= shear(mat,list); card=ImageUtil.drawContours(card,50); HighGui.imshow("card", card); Imgproc.cvtColor(card, card, imgproc. COLOR_BGR2GRAY); Imgproc.GaussianBlur(card, card, new Size(3, 3), 0, 0); Imgproc.threshold(card, card, 165, 255, Imgproc.THRESH_BINARY); / / Imgproc. Canny (image, image, lowThresh lowThresh * 3, 3); System.out.println(ImageUtil.getImageMessage(Mat2BufImg(card,".jpg"),"eng")); return null; }Copy the code

2.3 Image number recognition tess4J

Downloads of language packs

pom

< the dependency > < groupId >.net. Sourceforge. Tess4j < / groupId > < artifactId > tess4j < / artifactId > < version > 4.5.4 < / version > </dependency>Copy the code
public static String getImageMessage(BufferedImage img,String language){ String result=""; try{ ITesseract instance = new Tesseract(); instance.setTessVariable("user_defined_dpi", "300"); File tessDataFolder = new File("E:\ tessdata-master"); instance.setLanguage(language); instance.setDatapath(tessDataFolder.getAbsolutePath()); result = instance.doOCR(img); System.out.println(result); }catch(Exception e){ e.printStackTrace(); } return result; }Copy the code