您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。