您好,登錄后才能下訂單哦!
這篇文章主要講解了“OPENCV中文顯示亂碼怎么用JAVA處理”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“OPENCV中文顯示亂碼怎么用JAVA處理”吧!
1.Imgproc.putText中文亂碼
Imgproc.putText(image, new String(("性別:" + gender + "年齡:" + age).getBytes("UTF-8")), new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_PLAIN, 0.8, sc, 1, Imgproc.LINE_AA, false);
2.問題處理思路
1.MAT轉IMAGE 2.IMAGE添加水印后轉MAT
3.代碼(轉換來自網絡)
/** * Mat轉換成BufferedImage * * @param matrix 要轉換的Mat * @param fileExtension 格式為 ".jpg", ".png", etc * @return */ public static BufferedImage Mat2BufImg(Mat matrix, String fileExtension) { // convert the matrix into a matrix of bytes appropriate for // this file extension MatOfByte mob = new MatOfByte(); Imgcodecs.imencode(fileExtension, matrix, mob); // convert the "matrix of bytes" into a byte array byte[] byteArray = mob.toArray(); BufferedImage bufImage = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufImage = ImageIO.read(in); } catch (Exception e) { e.printStackTrace(); } return bufImage; } /** * BufferedImage轉換成Mat * * @param original 要轉換的BufferedImage * @param imgType bufferedImage的類型 如 BufferedImage.TYPE_3BYTE_BGR * @param matType 轉換成mat的type 如 CvType.CV_8UC3 */ public static Mat BufImg2Mat(BufferedImage original, int imgType, int matType) { if (original == null) { throw new IllegalArgumentException("original == null"); } //System.loadLibrary("opencv_java412"); //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //System.load("E:\\opencv\\opencv\\build\\java\\x64\\opencv_java412.dll"); //System.out.println(Core.NATIVE_LIBRARY_NAME); // Don't convert if it already has correct type if (original.getType() != imgType){ // Create a buffered image BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), imgType); // Draw the image onto the new buffer Graphics2D g = image.createGraphics(); try { g.setComposite(AlphaComposite.Src); g.drawImage(original, 0, 0, null); } finally { g.dispose(); } } byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData(); Mat mat = Mat.eye(original.getHeight(), original.getWidth(), matType); mat.put(0, 0, pixels); return mat; } /** * * @param cc 識別類 * @param image 圖片 * @param sc 顏色 * @param flip 是否反轉 * @return * @throws UnsupportedEncodingException */ private static Map<String,Object> getFace(CascadeClassifier cc,Mat image,Scalar sc,boolean flip) throws UnsupportedEncodingException { Map<String,Object> resultMap=new HashMap<String,Object>(); MatOfRect face = new MatOfRect(); if(flip) { Core.flip(image, image, 1); } cc.detectMultiScale(image, face); Rect[] rects = face.toArray(); System.out.println("匹配到 " + rects.length + " 個人臉"); // 4 為每張識別到的人臉畫一個圈 for (int i = 0; i < rects.length; i++) { Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), sc); String age=analyseAge(image,rects[i]); String gender=analyseGender(image,rects[i]); // Font font = new Font("微軟雅黑", Font.PLAIN, 12); BufferedImage bufImg =Mat2BufImg(image,".png"); Graphics2D g = bufImg.createGraphics(); g.drawImage(bufImg, 0, 0, bufImg.getWidth(),bufImg.getHeight(), null); g.setFont(font); //設置字體 //設置水印的坐標 g.drawString("性別:"+gender+" 年齡:"+age, rects[i].x, rects[i].y); g.dispose(); image=ImageUtil.BufImg2Mat(bufImg, BufferedImage.TYPE_3BYTE_BGR, CvType.CV_8UC3);// CvType.CV_8UC3 /* * Imgproc.putText(image, new String(("性別:" + gender + "年齡:" + * age).getBytes("UTF-8")), new Point(rects[i].x, rects[i].y), * Imgproc.FONT_HERSHEY_PLAIN, 0.8, sc, 1, Imgproc.LINE_AA, false); */ } if(flip) { Core.flip(image, image, 1); } boolean check=rects.length<1?false:true; resultMap.put("check", check); resultMap.put("Mat", image); return resultMap; }
4.中文顯示效果圖
感謝各位的閱讀,以上就是“OPENCV中文顯示亂碼怎么用JAVA處理”的內容了,經過本文的學習后,相信大家對OPENCV中文顯示亂碼怎么用JAVA處理這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。