这篇文章主要讲解了“OPENCV中文显示乱码怎么用JAVA处理”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“OPENCV中文显示乱码怎么用JAVA处理”吧!
创新互联公司长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为延川企业提供专业的做网站、网站设计,延川网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
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 MapgetFace(CascadeClassifier cc,Mat image,Scalar sc,boolean flip) throws UnsupportedEncodingException { Map resultMap=new HashMap (); 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处理这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!