OPENCV中文显示乱码 JAVA处理

网上搜索的都是C++或者python的解决思路。

这里给个java的处理思路

1.Imgproc.putText中文乱码

1Imgproc.putText(image, new String(("性别:" + gender + "年龄:" + age).getBytes("UTF-8")), 2 new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_PLAIN, 0.8, sc, 1, Imgproc.LINE_AA, false);

2.问题处理思路

11.MATIMAGE 22.IMAGE添加水印后转MAT

3.代码(转换来自网络)

1/** 2 * Mat转换成BufferedImage 3 * 4 * @param matrix 要转换的Mat 5 * @param fileExtension 格式为 ".jpg", ".png", etc 6 * @return 7 */ 8 public static BufferedImage Mat2BufImg(Mat matrix, String fileExtension) { 9 // convert the matrix into a matrix of bytes appropriate for 10 // this file extension 11 MatOfByte mob = new MatOfByte(); 12 Imgcodecs.imencode(fileExtension, matrix, mob); 13 // convert the "matrix of bytes" into a byte array 14 byte[] byteArray = mob.toArray(); 15 BufferedImage bufImage = null; 16 try { 17 InputStream in = new ByteArrayInputStream(byteArray); 18 bufImage = ImageIO.read(in); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 return bufImage; 23 } 24 25/** 26 * BufferedImage转换成Mat 27 * 28 * @param original 要转换的BufferedImage 29 * @param imgType bufferedImage的类型 如 BufferedImage.TYPE_3BYTE_BGR 30 * @param matType 转换成mat的type 如 CvType.CV_8UC3 31 */ 32 public static Mat BufImg2Mat(BufferedImage original, int imgType, int matType) { 33 if (original == null) { 34 throw new IllegalArgumentException("original == null"); 35 } 36 //System.loadLibrary("opencv_java412"); 37 //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 38 //System.load("E:\\opencv\\opencv\\build\\java\\x64\\opencv_java412.dll"); 39 //System.out.println(Core.NATIVE_LIBRARY_NAME); 40 // Don't convert if it already has correct type 41 if (original.getType() != imgType){ 42 // Create a buffered image 43 BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), imgType); 44 45 // Draw the image onto the new buffer 46 Graphics2D g = image.createGraphics(); 47 try { 48 g.setComposite(AlphaComposite.Src); 49 g.drawImage(original, 0, 0, null); 50 } finally { 51 g.dispose(); 52 } 53 } 54 55 byte[] pixels = ((DataBufferByte) original.getRaster().getDataBuffer()).getData(); 56 Mat mat = Mat.eye(original.getHeight(), original.getWidth(), matType); 57 mat.put(0, 0, pixels); 58 return mat; 59 } 60 /** 61 * 62 * @param cc 识别类 63 * @param image 图片 64 * @param sc 颜色 65 * @param flip 是否反转 66 * @return 67 * @throws UnsupportedEncodingException 68 */ 69 private static Map<String,Object> getFace(CascadeClassifier cc,Mat image,Scalar sc,boolean flip) throws UnsupportedEncodingException { 70 Map<String,Object> resultMap=new HashMap<String,Object>(); 71 72 MatOfRect face = new MatOfRect(); 73 if(flip) { 74 Core.flip(image, image, 1); 75 } 76 cc.detectMultiScale(image, face); 77 78 Rect[] rects = face.toArray(); 79 System.out.println("匹配到 " + rects.length + " 个人脸"); 80 81 // 4 为每张识别到的人脸画一个圈 82 for (int i = 0; i < rects.length; i++) { 83 Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y), 84 new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), sc); 85 86 String age=analyseAge(image,rects[i]); 87 String gender=analyseGender(image,rects[i]); 88 89 // 90 Font font = new Font("微软雅黑", Font.PLAIN, 12); 91 BufferedImage bufImg =Mat2BufImg(image,".png"); 92 Graphics2D g = bufImg.createGraphics(); 93 g.drawImage(bufImg, 0, 0, bufImg.getWidth(),bufImg.getHeight(), null); 94 g.setFont(font); //设置字体 95 96 //设置水印的坐标 97 g.drawString("性别:"+gender+" 年龄:"+age, rects[i].x, rects[i].y); 98 g.dispose(); 99 100 image=ImageUtil.BufImg2Mat(bufImg, BufferedImage.TYPE_3BYTE_BGR, CvType.CV_8UC3);// CvType.CV_8UC3 101 102 /* 103 * Imgproc.putText(image, new String(("性别:" + gender + "年龄:" + 104 * age).getBytes("UTF-8")), new Point(rects[i].x, rects[i].y), 105 * Imgproc.FONT_HERSHEY_PLAIN, 0.8, sc, 1, Imgproc.LINE_AA, false); 106 */ 107 108 } 109 110 if(flip) { 111 Core.flip(image, image, 1); 112 } 113 114 boolean check=rects.length<1?false:true; 115 resultMap.put("check", check); 116 resultMap.put("Mat", image); 117 return resultMap; 118 }

4.中文显示效果图

5.欢迎star

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java编码问题总结

对于Java,由于默认的编码方式是UNICODE,所以用中文也易出问题,常见的解决是:Strings2newString(s1.getBytes(“ISO88591”),”GBK”);1、utf8解决JSP中文乱码问题一般说来在每个页面的开始处,加入:<%@pagelanguage"java"contentTyp