// Create a default HightlightView if we found no face in the picture. private void makeDefault() { HighlightView hv = new HighlightView(mImageView); int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); Rect imageRect = new Rect(0, 0, width, height); // REMOVED: make the default size about 4/5 of the width or height//* 4 / 5; int cropWidth = Math.min(width, height); int cropHeight = cropWidth; if (mAspectX != 0 && mAspectY != 0) { if (mAspectX > mAspectY) { cropHeight = cropWidth * mAspectY / mAspectX; } else { cropWidth = cropHeight * mAspectX / mAspectY; } } int x = (width - cropWidth) / 2; int y = (height - cropHeight) / 2; RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); hv.setup(mImageMatrix, imageRect, cropRect, mCircleCrop, mAspectX != 0 && mAspectY != 0); mImageView.mHighlightViews.clear(); // Thong added for rotate mImageView.add(hv); }
private void startFaceDetection() { if (isFinishing()) { return; } mImageView.setImageBitmapResetBase(mBitmap, true); Util.startBackgroundJob( this, null, "Please wait\u2026", new Runnable() { public void run() { final CountDownLatch latch = new CountDownLatch(1); final Bitmap b = (mImage != null) ? mImage.fullSizeBitmap(IImage.UNCONSTRAINED, 1024 * 1024) : mBitmap; mHandler.post( new Runnable() { public void run() { if (b != mBitmap && b != null) { mImageView.setImageBitmapResetBase(b, true); mBitmap = b; } if (mImageView.getScale() == 1F) { mImageView.center(true, true); } latch.countDown(); } }); try { latch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } mRunFaceDetection.run(); } }, mHandler); }
// For each face, we create a HightlightView for it. private void handleFace(FaceDetector.Face f) { PointF midPoint = new PointF(); int r = ((int) (f.eyesDistance() * mScale)) * 2; f.getMidPoint(midPoint); midPoint.x *= mScale; midPoint.y *= mScale; int midX = (int) midPoint.x; int midY = (int) midPoint.y; HighlightView hv = new HighlightView(mImageView); int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); Rect imageRect = new Rect(0, 0, width, height); RectF faceRect = new RectF(midX, midY, midX, midY); faceRect.inset(-r, -r); if (faceRect.left < 0) { faceRect.inset(-faceRect.left, -faceRect.left); } if (faceRect.top < 0) { faceRect.inset(-faceRect.top, -faceRect.top); } if (faceRect.right > imageRect.right) { faceRect.inset(faceRect.right - imageRect.right, faceRect.right - imageRect.right); } if (faceRect.bottom > imageRect.bottom) { faceRect.inset(faceRect.bottom - imageRect.bottom, faceRect.bottom - imageRect.bottom); } hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop, mAspectX != 0 && mAspectY != 0); mImageView.add(hv); }
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); } 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(CropImage.this, "Multi face crop help", Toast.LENGTH_SHORT); t.show(); } } }); }
@Override public void onCreate(Bundle icicle) { overridePendingTransition(R.anim.fade_in_animation, R.anim.fade_out_animation); super.onCreate(icicle); mContentResolver = getContentResolver(); getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.cropimage); /* * Added to fix a Android issue for devices that support hardware acceleration. http://android-developers.blogspot.in/2011/03/android-30 -hardware-acceleration.html */ mImageView = (CropImageView) findViewById(R.id.image); try { Method method = mImageView.getClass().getMethod("setLayerType", Integer.TYPE, Paint.class); method.invoke(mImageView, 1, null); } catch (IllegalArgumentException e) { Logger.e(getClass().getSimpleName(), "Exception during reflection", e); } catch (IllegalAccessException e) { Logger.e(getClass().getSimpleName(), "Exception during reflection", e); } catch (InvocationTargetException e) { Logger.e(getClass().getSimpleName(), "Exception during reflection", e); } catch (SecurityException e) { Logger.e(getClass().getSimpleName(), "Exception during reflection", e); } catch (NoSuchMethodException e) { Logger.e(getClass().getSimpleName(), "Exception during reflection", e); } showStorageToast(this); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { if (extras.getString(HikeConstants.Extras.CIRCLE_CROP) != null) { mCircleCrop = true; mAspectX = 1; mAspectY = 1; } if (extras.containsKey(HikeConstants.Extras.RETURN_CROP_RESULT_TO_FILE)) { returnToFile = extras.getBoolean(HikeConstants.Extras.RETURN_CROP_RESULT_TO_FILE); } mImagePath = extras.getString(HikeConstants.Extras.IMAGE_PATH); mSaveUri = extras.containsKey(MediaStore.EXTRA_OUTPUT) ? getImageUri(extras.getString(MediaStore.EXTRA_OUTPUT)) : null; // look here mBitmap = getBitmap(mImagePath); String imageOrientation = Utils.getImageOrientation(mImagePath); mBitmap = HikeBitmapFactory.rotateBitmap(mBitmap, Utils.getRotatedAngle(imageOrientation)); mAspectX = extras.getInt(HikeConstants.Extras.ASPECT_X); mAspectY = extras.getInt(HikeConstants.Extras.ASPECT_Y); mOutputX = extras.getInt(HikeConstants.Extras.OUTPUT_X); mOutputY = extras.getInt(HikeConstants.Extras.OUTPUT_Y); mScale = extras.getBoolean(HikeConstants.Extras.SCALE, true); mScaleUp = extras.getBoolean(HikeConstants.Extras.SCALE_UP, true); } if (mBitmap == null) { Toast toast = Toast.makeText(this, getResources().getString(R.string.image_failed), Toast.LENGTH_LONG); toast.show(); Logger.d(TAG, "Unable to open bitmap"); finish(); return; } // Make UI fullscreen. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); findViewById(R.id.rotateLeft) .setOnClickListener( new View.OnClickListener() { public void onClick(View v) { mBitmap = Util.rotateImage(mBitmap, -90); RotateBitmap rotateBitmap = new RotateBitmap(mBitmap); mImageView.setImageRotateBitmapResetBase(rotateBitmap, true); mRunFaceDetection.run(); } }); setupActionBar(); startFaceDetection(); }