簡體   English   中英

在android中使用面部檢測裁剪圖像

[英]Crop Image with face detection in android

我需要一個演示,可以使用面部檢測功能裁剪任何圖像。

固定

但經過幾個小時的沖浪,我沒有參加一個演示,所以我准備了一個演示,結合了我在網上找到的幾個演示。

我准備了一個演示來裁剪圖像。

我的演示裁剪圖像矩形和圓形。

它還可以檢測臉部並根據臉部檢測裁剪圖像。

我使用下面的圖像來裁剪它。

主要形象

並且裁剪結果的屏幕截圖是:

截圖

該示例的xml是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >

<View
    android:id="@+id/part1"
    android:layout_width="fill_parent"
    android:layout_height="100dp" >
</View>

<View
    android:id="@+id/part2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="30dp" >
</View>

</LinearLayout>

Activity的java代碼:

public class MainActivity extends Activity {
public View part1, part2;
int viewHeight, viewWidth;
private FaceDetector myFaceDetect;
private FaceDetector.Face[] myFace;
float myEyesDistance;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    part1 = findViewById(R.id.part1);
    part2 = findViewById(R.id.part2);
    part1.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            viewHeight = part1.getMeasuredHeight();
            viewWidth = part1.getMeasuredWidth();
            try {

                Paint paint = new Paint();
                paint.setFilterBitmap(true);
                Bitmap bitmapOrg = BitmapFactory.decodeResource(
                        getResources(),
                        R.drawable.sachin_tendulkar_10102013);

                int targetWidth = bitmapOrg.getWidth();
                int targetHeight = bitmapOrg.getHeight();

                Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
                        targetHeight, Bitmap.Config.ARGB_8888);

                RectF rectf = new RectF(0, 0, viewWidth, viewHeight);

                Canvas canvas = new Canvas(targetBitmap);
                Path path = new Path();

                path.addRect(rectf, Path.Direction.CW);
                canvas.clipPath(path);

                canvas.drawBitmap(
                        bitmapOrg,
                        new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg
                                .getHeight()), new Rect(0, 0, targetWidth,
                                targetHeight), paint);

                Matrix matrix = new Matrix();
                matrix.postScale(1f, 1f);

                BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
                bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;

                bitmapOrg = BitmapFactory.decodeResource(getResources(),
                        R.drawable.sachin_tendulkar_10102013,
                        bitmapFatoryOptions);

                myFace = new FaceDetector.Face[5];
                myFaceDetect = new FaceDetector(targetWidth, targetHeight,
                        5);
                int numberOfFaceDetected = myFaceDetect.findFaces(
                        bitmapOrg, myFace);
                Bitmap resizedBitmap = null;
                if (numberOfFaceDetected > 0) {
                    PointF myMidPoint = null;
                    Face face = myFace[0];
                    myMidPoint = new PointF();
                    face.getMidPoint(myMidPoint);
                    myEyesDistance = face.eyesDistance();

                    if (myMidPoint.x + viewWidth > targetWidth) {
                        while (myMidPoint.x + viewWidth > targetWidth) {
                            myMidPoint.x--;
                        }
                    }
                    if (myMidPoint.y + viewHeight > targetHeight) {
                        while (myMidPoint.y + viewHeight > targetHeight) {
                            myMidPoint.y--;
                        }
                    }
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,
                            (int) (myMidPoint.x - myEyesDistance),
                            (int) (myMidPoint.y - myEyesDistance),
                            viewWidth, viewHeight, matrix, true);
                } else {
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                            viewWidth, viewHeight, matrix, true);
                }
                /* convert Bitmap to resource */
                // Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap,
                // 0,
                // 0, viewWidth, viewHeight, matrix, true);
                BitmapDrawable bd = new BitmapDrawable(resizedBitmap);

                part1.setBackgroundDrawable(bd);

            } catch (Exception e) {
                System.out.println("Error1 : " + e.getMessage()
                        + e.toString());
            }
        }
    });
    part2.post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            viewHeight = part2.getMeasuredHeight();
            viewWidth = part2.getMeasuredWidth();
            try {

                Paint paint = new Paint();
                paint.setFilterBitmap(true);
                Bitmap bitmapOrg = BitmapFactory.decodeResource(
                        getResources(),
                        R.drawable.sachin_tendulkar_10102013);

                int targetWidth = bitmapOrg.getWidth();
                int targetHeight = bitmapOrg.getHeight();

                Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
                        targetHeight, Bitmap.Config.ARGB_8888);

                RectF rectf = new RectF(0, 0, viewWidth, viewHeight);

                Canvas canvas = new Canvas(targetBitmap);
                Path path = new Path();

                path.addRect(rectf, Path.Direction.CW);
                canvas.clipPath(path);

                canvas.drawBitmap(
                        bitmapOrg,
                        new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg
                                .getHeight()), new Rect(0, 0, targetWidth,
                                targetHeight), paint);

                Matrix matrix = new Matrix();
                matrix.postScale(1f, 1f);

                BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
                bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;

                bitmapOrg = BitmapFactory.decodeResource(getResources(),
                        R.drawable.sachin_tendulkar_10102013,
                        bitmapFatoryOptions);

                myFace = new FaceDetector.Face[5];
                myFaceDetect = new FaceDetector(targetWidth, targetHeight,
                        5);
                int numberOfFaceDetected = myFaceDetect.findFaces(
                        bitmapOrg, myFace);
                Bitmap resizedBitmap = null;
                if (numberOfFaceDetected > 0) {
                    PointF myMidPoint = null;
                    Face face = myFace[0];
                    myMidPoint = new PointF();
                    face.getMidPoint(myMidPoint);
                    myEyesDistance = face.eyesDistance() + 20;

                    if (myMidPoint.x + viewWidth > targetWidth) {
                        while (myMidPoint.x + viewWidth > targetWidth) {
                            myMidPoint.x--;
                        }
                    }
                    if (myMidPoint.y + viewHeight > targetHeight) {
                        while (myMidPoint.y + viewHeight > targetHeight) {
                            myMidPoint.y--;
                        }
                    }
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,
                            (int) (myMidPoint.x - myEyesDistance),
                            (int) (myMidPoint.y - myEyesDistance),
                            viewWidth, viewHeight, matrix, true);
                } else {
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                            viewWidth, viewHeight, matrix, true);
                }
                /* convert Bitmap to resource */
                // Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap,
                // 0,
                // 0, viewWidth, viewHeight, matrix, true);
                BitmapDrawable bd = new  BitmapDrawable(resizedBitmap);

                part2.setBackgroundDrawable(new BitmapDrawable(
                        getCroppedBitmap(bd.getBitmap())));

            } catch (Exception e) {
                System.out.println("Error1 : " + e.getMessage()
                        + e.toString());
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public Bitmap getCroppedBitmap(Bitmap bitmap) {
    // Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    // bitmap.getHeight(), Config.ARGB_8888);
    // Canvas canvas = new Canvas(output);
    //
    // final int color = 0xff424242;
    // final Paint paint = new Paint();
    // final Rect rect = new Rect(0, 0, bitmap.getWidth(),
    // bitmap.getHeight());
    //
    // paint.setAntiAlias(true);
    // canvas.drawARGB(0, 0, 0, 0);
    // paint.setColor(color);
    // // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    // canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
    // bitmap.getWidth() / 2, paint);
    // paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    // canvas.drawBitmap(bitmap, rect, rect, paint);
    // // Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    // // return _bmp;
    // return output;

    int targetWidth = bitmap.getWidth();
    int targetHeight = bitmap.getHeight();
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2,
            ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) /    2),
            Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = bitmap;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
            sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
            targetHeight), null);
    return targetBitmap;

}

}

該演示適用於放在drawable文件夾中的任何圖像,

但是,如果要裁剪任何動態圖像,例如從庫中下載或選擇的任何圖像,請在代碼中進行少量更改:

看到這一行:

Bitmap bitmapOrg = BitmapFactory.decodeResource(
                    getResources(),
                    R.drawable.sachin_tendulkar_10102013);

在這里我從drawable文件夾中獲取圖像,現在對於任何下載的圖像,你只需要將該圖像保存在bitmapOrg變量中,所以只需將上面的行更改兩次,一個用於part1用於矩形,part2用於ciculart用於下載的圖像保存為bitmapOrg作為位圖,並使用演示,它將以矩形和圓形方式裁剪您的圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM