public void run() { mImageMatrix = mImageView.getImageMatrix(); Bitmap faceBitmap = prepareBitmap(); mScale = 1.0F / mScale; if (faceBitmap != null) { FaceDetector detector = new FaceDetector(faceBitmap.getWidth(), faceBitmap.getHeight(), mFaces.length); mNumFaces = detector.findFaces(faceBitmap, mFaces); } if (faceBitmap != null && faceBitmap != mBitmap) { faceBitmap.recycle(); } mHandler.post( new Runnable() { public void run() { mWaitingToPick = mNumFaces > 1; makeDefault(); mImageView.invalidate(); if (mImageView.mHighlightViews.size() > 0) { mCrop = mImageView.mHighlightViews.get(0); mCrop.setFocus(true); } if (mNumFaces > 1) {} } }); }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap bitmapimage = BitmapFactory.decodeFile("/sdcard/face.jpg"); FaceDetector facedetector = new FaceDetector(bitmapimage.getWidth(), bitmapimage.getHeight(), 10); Log.d("test", "getWidth = " + bitmapimage.getWidth()); Log.d("test", "getHeight = " + bitmapimage.getHeight()); Face[] faces = new Face[10]; int num = facedetector.findFaces(bitmapimage, faces); for (int i = 0; i < num; i++) { PointF point = new PointF(); Log.d("test", "confidence = " + faces[i].confidence()); Log.d("test", "eyesDistance = " + faces[i].eyesDistance()); faces[i].getMidPoint(point); Log.d("test", "MidPoint x = " + point.x + " y=" + point.y); Log.d("test", "euler x = " + faces[i].pose(Face.EULER_X)); Log.d("test", "euler y = " + faces[i].pose(Face.EULER_Y)); Log.d("test", "euler x = " + faces[i].pose(Face.EULER_Z)); } }
@Override protected Face[] doInBackground(Bitmap... params) { mEditBitmap = detect(params[0]); mCanvas = new Canvas(mEditBitmap); Face[] faces = new Face[MAX_FACES]; FaceDetector detector = new FaceDetector(mEditBitmap.getWidth(), mEditBitmap.getHeight(), MAX_FACES); mFaceNum = detector.findFaces(mEditBitmap, faces); return faces; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 画像の読み込み String filePath = Environment.getExternalStorageDirectory().getPath() + "/sample.jpg"; Bitmap image = BitmapFactory.decodeFile(filePath); // 顔認識処理 FaceDetector faceDetector = new FaceDetector(image.getWidth(), image.getHeight(), MAX_FACES); Face[] faces = new Face[MAX_FACES]; int num = faceDetector.findFaces(image, faces); Log.d(TAG, "the number of faces found:" + String.valueOf(num)); // 結果の表示 for (int i = 0; i < num; i++) { PointF point = new PointF(); faces[i].getMidPoint(point); Log.d(TAG, "Number" + i + " confidence:" + faces[i].confidence()); Log.d(TAG, "Number" + i + " eyesDistance:" + faces[i].eyesDistance()); Log.d( TAG, "Number" + i + " the position of the mid-point between the eyes:" + point.x + "," + point.y); } // 画像に認識した結果を描画する Bitmap imageCopy = image.copy(Config.ARGB_8888, true); Canvas canvas = new Canvas(imageCopy); Paint paint = new Paint(); paint.setColor(Color.argb(255, 255, 0, 0)); paint.setStyle(Style.STROKE); paint.setStrokeWidth(3.0f); for (int i = 0; i < num; i++) { PointF point = new PointF(); faces[i].getMidPoint(point); canvas.drawRect( point.x - faces[i].eyesDistance(), point.y - faces[i].eyesDistance(), point.x + faces[i].eyesDistance(), point.y + faces[i].eyesDistance(), paint); } ImageView imageView = (ImageView) findViewById(R.id.ImageView); imageView.setImageBitmap(imageCopy); }
public void run() { mImageMatrix = mImageView.getImageMatrix(); Bitmap faceBitmap = prepareBitmap(); mScale = 1.0F / mScale; if (faceBitmap != null && mDoFaceDetection) { FaceDetector detector = new FaceDetector(faceBitmap.getWidth(), faceBitmap.getHeight(), mFaces.length); mNumFaces = detector.findFaces(faceBitmap, mFaces); } if (faceBitmap != null && faceBitmap != mBitmap) { faceBitmap.recycle(); } mHandler.post( new Runnable() { public void run() { mWaitingToPick = mNumFaces > 1; if (mNumFaces > 0) { for (int i = 0; i < mNumFaces; i++) { handleFace(mFaces[i]); } } else { makeDefault(); } mImageView.invalidate(); if (mImageView.mHighlightViews.size() == 1) { mCrop = mImageView.mHighlightViews.get(0); mCrop.setFocus(true); } if (mNumFaces > 1) { Toast t = Toast.makeText( CropImageBaseActivity.this, R.string.multiface_crop_help, Toast.LENGTH_SHORT); t.show(); } } }); }
public FaceView( Context context, String[] FilePathStrings, String[] FileNameStrings, int position) { super(context); pInnerBullsEye.setStyle(Paint.Style.FILL); pInnerBullsEye.setColor(Color.RED); pOuterBullsEye.setStyle(Paint.Style.STROKE); pOuterBullsEye.setColor(Color.RED); tmpPaint.setStyle(Paint.Style.STROKE); tmpPaint.setTextAlign(Paint.Align.CENTER); String[] filePath = FilePathStrings; String[] fileName = FileNameStrings; int pos = position; BitmapFactory.Options bfo = new BitmapFactory.Options(); bfo.inPreferredConfig = Bitmap.Config.RGB_565; bfo.inSampleSize = 2; sourceImage = BitmapFactory.decodeFile(filePath[pos], bfo); picWidth = sourceImage.getWidth(); picHeight = sourceImage.getHeight(); arrayFaces = new FaceDetector(picWidth, picHeight, NUM_FACES); arrayFaces.findFaces(sourceImage, getAllFaces); for (int i = 0; i < getAllFaces.length; i++) { getFace = getAllFaces[i]; try { PointF eyesMP = new PointF(); getFace.getMidPoint(eyesMP); eyesDistance[i] = getFace.eyesDistance(); eyesMidPts[i] = eyesMP; if (DEBUG) { Log.i( "Face", i + " " + getFace.confidence() + " " + getFace.eyesDistance() + " " + "Pose: (" + getFace.pose(FaceDetector.Face.EULER_X) + "," + getFace.pose(FaceDetector.Face.EULER_Y) + "," + getFace.pose(FaceDetector.Face.EULER_Z) + ")" + "Eyes Midpoint: (" + eyesMidPts[i].x + "," + eyesMidPts[i].y + ")"); } } catch (Exception e) { if (DEBUG) Log.e("Face", i + " is null"); } } }
@Override public void run() { // TODO Auto-generated method stub if (cancel == true) updateBarHandler.removeCallbacks(updateThread); else { nbm = Bitmap.createBitmap(bm); /* 人脸检测(人脸双眼间距必须在20pix以上) */ // 检测器对象 FaceDetector detector = new FaceDetector(nbm.getWidth(), nbm.getHeight(), 1); // 存放人脸信息 FaceDetector.Face[] face = new FaceDetector.Face[1]; // 人脸数目 int Number = detector.findFaces(nbm, face); if (Number > 0) { // 双眼距离 dist = face[0].eyesDistance(); // 双眼中点 face[0].getMidPoint(point); /* 归一化裁剪 */ int x = (int) (point.x - dist); int y = (int) (point.y - dist); int width = (int) (point.x + dist) - x; if (width > nbm.getWidth() - x) { width = nbm.getWidth() - x; } int height = (int) (point.y + dist * 1.5) - y; if (height > nbm.getHeight() - y) { height = nbm.getHeight() - y; } nbm = Bitmap.createBitmap(nbm, x, y, width, height); // 图像放大至相片尺寸 Matrix mMatrix = new Matrix(); float scalex = (float) myCamrActivity.ImageWidth / width; float scaley = (float) myCamrActivity.ImageHeight / height; mMatrix.postScale(scalex, scaley); nbm = Bitmap.createBitmap(nbm, 0, 0, nbm.getWidth(), nbm.getHeight(), mMatrix, false); bm = nbm; // 图片存入SD卡 File file = new File("./sdcard/mikepic.png"); BufferedOutputStream bos = null; try { if (!file.exists()) file.createNewFile(); bos = new BufferedOutputStream(new FileOutputStream(file)); bm.compress(Bitmap.CompressFormat.PNG, 100, bos); bos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // logout System.out.println(bm.getWidth() + " " + bm.getHeight()); // 定位眼睛 Feature.EyesLocate(leftEye, rightEye, dist, point, x, y, scalex, scaley); // 相关变量归一化变换 point.x = (point.x - x) * scalex; point.y = (point.y - y) * scaley; dist = dist * scalex; } else { // 显示浮动窗口 Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show(); return; } bar.setProgress(100 / N * 1); // myImageView.setImageBitmap(bm); updateBarHandler.post(updateThread); } }
@Override protected List<Contact2Image> doInBackground(Bitmap... bitmaps) { Bitmap bitmap; if (bitmaps == null || bitmaps.length == 0) { if (filename != null) { bitmap = loadBitmap(filename); } else { throw new IllegalArgumentException("No ImageID nor FileName is specified."); } } else { bitmap = bitmaps[0]; } // check if bitmap width is even if (bitmap.getWidth() % 2 == 1) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() + 1, bitmap.getHeight(), false); } // detect faces FaceDetector detector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5); FaceDetector.Face[] faces = new FaceDetector.Face[5]; detector.findFaces(bitmap, faces); List<Contact2Image> contact2ImageList = new ArrayList<>(); ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ContentValues values = new ContentValues(); values.put(Contact2Image.COLUMN_IMAGE_ID, imageId); int fakeContact = -1; if (mInternalBitmap) { bitmap.recycle(); } for (FaceDetector.Face face : faces) { // check for valid face if (face != null && face.confidence() > 0.5f) { if (isCancelled()) { return null; } Contact2Image contact2Image = new Contact2Image(); float x1, y1, x2, y2; PointF midPoint = new PointF(); face.getMidPoint(midPoint); final float radius = face.eyesDistance() * 1.25f; x1 = midPoint.x - radius; y1 = midPoint.y - radius + (radius / 5f); x2 = midPoint.x + radius; y2 = midPoint.y + radius + (radius / 5f); contact2Image.setImageId(imageId); contact2Image.set(x1, y1, x2, y2); values.put(Contact2Image.COLUMN_CONTACT_ID, fakeContact--); values.put(Contact2Image.COLUMN_X1, x1); values.put(Contact2Image.COLUMN_X2, x2); values.put(Contact2Image.COLUMN_Y1, y1); values.put(Contact2Image.COLUMN_Y2, y2); values.put(Contact2Image.COLUMN_DATE, Calendar.getInstance().getTimeInMillis()); ops.add( ContentProviderOperation.newInsert(SPOCContentProvider.CONTACTS_2_IMAGES_URI) .withValues(values) .build()); contact2ImageList.add(contact2Image); } } try { context.getContentResolver().applyBatch(SPOCContentProvider.AUTHORITY, ops); } catch (RemoteException | OperationApplicationException e) { Log.w(getClass().getSimpleName(), e); } return contact2ImageList; }
void doFacedetect(byte[] data, Camera camera) { long start = System.currentTimeMillis(); Bitmap bmp = CameraUtil.yuvimage2Bitmap(data, camera); Matrix matrix = new Matrix(); matrix.postRotate(270); matrix.postScale(-1, 1); Bitmap tmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); bmp.recycle(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int heightScreen = dm.heightPixels; int widthScreen = dm.widthPixels; Bitmap tmp1 = Bitmap.createScaledBitmap(tmp, widthScreen, heightScreen, true); tmp.recycle(); RectF rect = mFaceView.getRect(); Bitmap tmp2 = Bitmap.createBitmap( tmp1, (int) rect.left, (int) rect.top, (int) (rect.right - rect.left), (int) (rect.bottom - rect.top)); tmp1.recycle(); Bitmap picture = tmp2.copy(Bitmap.Config.RGB_565, true); tmp2.recycle(); int N_MAX = 1; FaceDetector faceDetector = new FaceDetector(picture.getWidth(), picture.getHeight(), N_MAX); Face[] faces = new Face[N_MAX]; faceDetector.findFaces(picture, faces); for (Face f : faces) { try { PointF midPoint = new PointF(); f.getMidPoint(midPoint); if (f.confidence() < 0.4) return; findFaceCount++; mFaceView.setFaceParameters(midPoint, f.eyesDistance()); if (findFaceCount == 3) { showCapture(); findFaceCount = 0; } if (modeSwitch.isChecked()) { ByteArrayOutputStream out = new ByteArrayOutputStream(); picture.compress(CompressFormat.JPEG, 100, out); byte[] picBytes = out.toByteArray(); Intent intent = new Intent(RegistCameraActivity.this, DisplayActivity_.class); intent.putExtra("picture", picBytes); intent.putExtra("isRegist", true); intent.putExtra("auto", true); intent.putExtra("currentCamera", CURRENT_CAMERA); startActivity(intent); } // System.out.println("YOU:"+midPoint); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); mFaceView.setFaceParameters(null, 0); mFaceView.invalidate(); findFaceCount = 0; } finally { picture.recycle(); } } long end = System.currentTimeMillis(); System.out.println("耗时:" + (end - start) + " ms"); Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { clearEyeCircle(); } }; timer.schedule(task, 200); }