OPENCV中文顯示亂碼 JAVA處理

網上搜索的都是C++或者python的解決思路。

這裏給個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.中文顯示效果圖

5.歡迎star

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章