需求:
采用java去识别图片中的是否含有人物头像。
实现:
基于jviolajones实现,具体的jar信息如下:
org.eclipse jviolajones 2.0 org.jdom jdom 1.1
依赖一个描述文件haarcascade_frontalface_default.xml,详情参见附件
具体代码如下:
public static void findFaces() throws Exception { String fileName1="/home/test/1.jpg"; String fileName2="/home/test/2.jpg"; String fileName3="/home/test/3.jpg"; Detector detector= Detector.create("haarcascade_frontalface_default.xml"); BufferedImage bi1=ImageIO.read(new File(fileName1)); BufferedImage bi2=ImageIO.read(new File(fileName2)); BufferedImage bi3=ImageIO.read(new File(fileName3)); Listres=detector.getFaces(bi1, 2, 1.25f, 0.1f,3,true); System.out.println(res); res=detector.getFaces(bi2, 2, 1.25f, 0.1f,3,true); System.out.println(res); res=detector.getFaces(bi3, 2, 1.25f, 0.1f,3,true); System.out.println(res); }
返回结果说明:
List<Rectangle>:为图片中头像出现的区域,包括起始坐标(x,y)以及对于的宽和高。如果需要加涂层可以基于Rectangle进行图层添加