这是一个娱乐性的app,用户可以通过拍照或者从本地图库选择自己或者朋友的照片,然后分析出年龄,性别,还可以把分析的结果和照片分享到地图上,其他用户也可以看到您分享的照片和测试出来的年龄。
对图片进行了深度的优化,防止内存溢出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | // TODO Auto-generated method stub dialog = createLoadingDialog(MainActivity. this , "分析中..." ); dialog.show(); // textView.setText("Waiting ..."); FaceppDetect faceppDetect = new FaceppDetect(); faceppDetect.setDetectCallback( new DetectCallback() { public void detectResult( final JSONObject rst) { // use the red paint Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(Math.max(img.getWidth(), img.getHeight()) / 100f); // create a new canvas final Bitmap bitmap = Bitmap.createBitmap( img.getWidth(), img.getHeight(), img.getConfig()); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(img, new Matrix(), null ); try { // find out all faces final int count = rst.getJSONArray( "face" ).length(); for ( int i = 0 ; i < count; ++i) { float x, y, w, h; // get the center point x = ( float ) rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "position" ) .getJSONObject( "center" ).getDouble( "x" ); y = ( float ) rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "position" ) .getJSONObject( "center" ).getDouble( "y" ); // get face size w = ( float ) rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "position" ) .getDouble( "width" ); h = ( float ) rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "position" ) .getDouble( "height" ); age = rst.getJSONArray( "face" ).getJSONObject(i) .getJSONObject( "attribute" ) .getJSONObject( "age" ) .getString( "value" ); range = rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "attribute" ) .getJSONObject( "age" ) .getString( "range" ); gender = rst.getJSONArray( "face" ) .getJSONObject(i) .getJSONObject( "attribute" ) .getJSONObject( "gender" ) .getString( "value" ); // change percent value to the real size x = x / 100 * img.getWidth(); w = w / 100 * img.getWidth() * 0 .7f; y = y / 100 * img.getHeight(); h = h / 100 * img.getHeight() * 0 .7f; // draw the box to mark it out canvas.drawLine(x - w, y - h, x - w, y + h, paint); canvas.drawLine(x - w, y - h, x + w, y - h, paint); canvas.drawLine(x + w, y + h, x - w, y + h, paint); canvas.drawLine(x + w, y + h, x + w, y - h, paint); } // save new image // img = bitmap; MainActivity. this .runOnUiThread( new Runnable() { public void run() { dialog.dismiss(); String msg = null ; try { if (Integer.valueOf(age) <= 15 ) { msg = Integer.valueOf(age) + Integer.valueOf(range) + "" ; } else if (Integer.valueOf(age) <= 18 && Integer.valueOf(age) >= 15 ) { msg = age; } else if (Integer.valueOf(age) > 25 && Integer.valueOf(age) <= 35 ) { int i = (Integer.valueOf(age) - 25 ) / Integer.valueOf(range); msg = Integer.valueOf(age) - i + "" ; } else if (Integer.valueOf(age) > 35 ) { msg = Integer.valueOf(age) - Integer.valueOf(range) + "" ; } else { msg = age; } age = msg; } catch (Exception e) { showToast( "分析失败." ); return ; } if (gender.equals( "Male" )) { gender = "男" ; } else if (gender.equals( "Female" )) { gender = "女" ; } msg = "分析结果:" + gender + " " + msg + "岁" ; showToast(msg); // show the image imageView.setImageBitmap(bitmap); // textView.setText("Finished, "+ count + // " faces."); showdialog(msg, age); age = "" ; }; }); } catch (JSONException e) { e.printStackTrace(); MainActivity. this .runOnUiThread( new Runnable() { public void run() { showToast( "JSONException" ); } }); } } }); faceppDetect.detect(img); |
联系客服