最近看到一个视频关于人脸识别,于是就测试了一番,千百度一番没有找到好的源码,
幸好Face++官方有源码,而且注释写的特别详细,我就研究了一天,终于皇天不负有心人,我成功的写出 了第一个人脸识别项目
这是部分代码:
String str=null; ByteArrayOutputStream output= null; FileImageInputStream input= null; byte bys[] = new byte[4000]; try { input = new FileImageInputStream(newFile(path)); output = new ByteArrayOutputStream(); byte[] buf = new byte[4000]; int numBytesRead = 0; while ((numBytesRead = input.read(buf)) != -1) { output.write(buf, 0, numBytesRead); } bys = output.toByteArray(); } catch (FileNotFoundException e1) { e1.printStackTrace(); System.err.println("系统找不到指定的路径"); } catch (IOException e) { e.printStackTrace(); } finally { try { output.close(); input.close(); } catch (Exception e2) { e2.printStackTrace(); } } /* *调用Face++接口CommonOperate对象需要传入三个参数 *第一个apiKey *第二个apiSecret *第三个isInternationalVersion 是否是使用国际版 *response.getStatus()返回状态码,状态这里可以查到 *https://console.faceplusplus.com.cn/documents/5672651 *response.getContent()返回参数,是字节数组,转成String输出JSON串 */ CommonOperate iOperate = new CommonOperate( "PDr8aqmZFWMvkFFtUNx_PFrbwi-j_Etx", "Ek54PzZn7au29e2TAUxlOzwOSfqNubps",false); try { Response response = iOperate.detectByte(bys,1, "gender,age,smiling,glass,headpose,blur"); if (response.getStatus()==200) { str = new String(response.getContent()); }else{ str = ""; } } catch (Exception e) { e.printStackTrace(); } return str; } 源码已上传至github
地址为:https://github.com/systemmin/Face.git