裁剪图像的人脸检测的机器人 [英] Crop Image with face detection in android

查看:212
本文介绍了裁剪图像的人脸检测的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个演示,其中任何一幅图像可以与人脸检测功能被裁剪。

固定

但很少冲浪小时后,我没来一个演示,所以prepared一个演示与我在网上找到一些演示相结合。

我有prepared演示裁切图像。

我的演示裁剪图像的矩形,圆形和也。

此外,它检测的脸部和裁剪图像根据脸部检测

我用下面的图像裁剪。

主图像

和的作物结果截图:

在XML的例子是:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=中心
机器人:方向=垂直
工具:上下文=MainActivity。>

<查看
    机器人:ID =@ + ID /第一部分
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =100dp>
< /视图>

<查看
    机器人:ID =@ + ID / 2部分
    机器人:layout_width =100dp
    机器人:layout_height =100dp
    机器人:layout_marginTop =30dp>
< /视图>

< / LinearLayout中>
 

在Java code的活动:

 公共类MainActivity延伸活动{
公共查看第一部分,第二部分;
INT viewHeight,viewWidth;
私人FaceDetector myFaceDetect;
私人FaceDetector.Face [] myFace;
浮动myEyesDistance;

@燮pressWarnings(德precation)
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    第一部分= findViewById(R.id.part1);
    第二部分= findViewById(R.id.part2);
    part1.post(新的Runnable(){

        @覆盖
        公共无效的run(){
            // TODO自动生成方法存根
            viewHeight = part1.getMeasuredHeight();
            viewWidth = part1.getMeasuredWidth();
            尝试 {

                涂料粉刷=新的油漆();
                paint.setFilterBitmap(真正的);
                位图bitmapOrg = BitmapFactory.de codeResource(
                        getResources(),
                        R.drawable.sachin_tendulkar_10102013);

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

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

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

                帆布油画=新的Canvas(targetBitmap);
                路径path =新路径();

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

                canvas.drawBitmap(
                        bitmapOrg,
                        新的Rect(0,0,bitmapOrg.getWidth(),bitmapOrg
                                .getHeight()),新的Rect(0,0,targetWidth,
                                targetHeight),漆);

                字模=新的Matrix();
                matrix.postScale(1F,1F);

                BitmapFactory.Options bitmapFatoryOptions =新BitmapFactory.Options();
                bitmapFatoryOptions.in preferredConfig = Bitmap.Config.RGB_565;

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

                myFace =新FaceDetector.Face [5];
                myFaceDetect =新FaceDetector(targetWidth,targetHeight,
                        5);
                INT numberOfFaceDetected = myFaceDetect.findFaces(
                        bitmapOrg,myFace);
                位图resizedBitmap = NULL;
                如果(numberOfFaceDetected大于0){
                    的PointF myMidPoint = NULL;
                    脸的脸= myFace [0];
                    myMidPoint =新的PointF();
                    face.getMidPoint(myMidPoint);
                    myEyesDistance = face.eyesDistance();

                    如果(myMidPoint.x + viewWidth> targetWidth){
                        而(myMidPoint.x + viewWidth> targetWidth){
                            myMidPoint.x--;
                        }
                    }
                    如果(myMidPoint.y + viewHeight> targetHeight){
                        而(myMidPoint.y + viewHeight> targetHeight){
                            myMidPoint.y--;
                        }
                    }
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,
                            (INT)(myMidPoint.x  -  myEyesDistance)
                            (INT)(myMidPoint.y  -  myEyesDistance)
                            viewWidth,viewHeight,矩阵,真正的);
                } 其他 {
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,
                            viewWidth,viewHeight,矩阵,真正的);
                }
                / *转换成位图资源* /
                //位图resizedBitmap = Bitmap.createBitmap(targetBitmap,
                // 0,
                // 0,viewWidth,viewHeight,矩阵,真正的);
                BitmapDrawable BD =新BitmapDrawable(resizedBitmap);

                part1.setBackgroundDrawable(BD);

            }赶上(例外五){
                的System.out.println(ERROR1:+ e.getMessage()
                        + e.toString());
            }
        }
    });
    part2.post(新的Runnable(){

        @覆盖
        公共无效的run(){
            // TODO自动生成方法存根
            viewHeight = part2.getMeasuredHeight();
            viewWidth = part2.getMeasuredWidth();
            尝试 {

                涂料粉刷=新的油漆();
                paint.setFilterBitmap(真正的);
                位图bitmapOrg = BitmapFactory.de codeResource(
                        getResources(),
                        R.drawable.sachin_tendulkar_10102013);

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

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

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

                帆布油画=新的Canvas(targetBitmap);
                路径path =新路径();

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

                canvas.drawBitmap(
                        bitmapOrg,
                        新的Rect(0,0,bitmapOrg.getWidth(),bitmapOrg
                                .getHeight()),新的Rect(0,0,targetWidth,
                                targetHeight),漆);

                字模=新的Matrix();
                matrix.postScale(1F,1F);

                BitmapFactory.Options bitmapFatoryOptions =新BitmapFactory.Options();
                bitmapFatoryOptions.in preferredConfig = Bitmap.Config.RGB_565;

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

                myFace =新FaceDetector.Face [5];
                myFaceDetect =新FaceDetector(targetWidth,targetHeight,
                        5);
                INT numberOfFaceDetected = myFaceDetect.findFaces(
                        bitmapOrg,myFace);
                位图resizedBitmap = NULL;
                如果(numberOfFaceDetected大于0){
                    的PointF myMidPoint = NULL;
                    脸的脸= myFace [0];
                    myMidPoint =新的PointF();
                    face.getMidPoint(myMidPoint);
                    myEyesDistance = face.eyesDistance()+ 20;

                    如果(myMidPoint.x + viewWidth> targetWidth){
                        而(myMidPoint.x + viewWidth> targetWidth){
                            myMidPoint.x--;
                        }
                    }
                    如果(myMidPoint.y + viewHeight> targetHeight){
                        而(myMidPoint.y + viewHeight> targetHeight){
                            myMidPoint.y--;
                        }
                    }
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,
                            (INT)(myMidPoint.x  -  myEyesDistance)
                            (INT)(myMidPoint.y  -  myEyesDistance)
                            viewWidth,viewHeight,矩阵,真正的);
                } 其他 {
                    resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,
                            viewWidth,viewHeight,矩阵,真正的);
                }
                / *转换成位图资源* /
                //位图resizedBitmap = Bitmap.createBitmap(targetBitmap,
                // 0,
                // 0,viewWidth,viewHeight,矩阵,真正的);
                BitmapDrawable BD =新BitmapDrawable(resizedBitmap);

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

            }赶上(例外五){
                的System.out.println(ERROR1:+ e.getMessage()
                        + e.toString());
            }
        }
    });
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}

公共位图getCroppedBitmap(位图位图){
    //位图输出= Bitmap.createBitmap(bitmap.getWidth()
    // bitmap.getHeight(),Config.ARGB_8888);
    //帆布油画=新的Canvas(输出);
    //
    //最终诠释色= 0xff424242;
    //最后的涂料粉刷=新的油漆();
    //最后矩形矩形=新的Rect(0,0,bitmap.getWidth(),
    // bitmap.getHeight());
    //
    // paint.setAntiAlias​​(真正的);
    // canvas.drawARGB(0,0,0,0);
    // paint.setColor(彩色);
    // // canvas.drawRoundRect(rectF,roundPx,roundPx,油漆);
    // canvas.drawCircle(bitmap.getWidth()/ 2,bitmap.getHeight()/ 2,
    // bitmap.getWidth()/ 2,油漆);
    // paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
    // canvas.drawBitmap(位图,矩形,矩形,油漆);
    // //位图_bmp = Bitmap.createScaledBitmap(输出,60,60,FALSE);
    // //返回_bmp;
    //返回输出;

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

    帆布油画=新的Canvas(targetBitmap);
    路径path =新路径();
    path.addCircle(((浮点)targetWidth  -  1)/ 2,
            ((浮点)targetHeight  -  1)/ 2,
            (Math.min(((浮点)targetWidth),((浮点)targetHeight))/ 2),
            Path.Direction.CCW);

    canvas.clipPath(路径);
    sourceBitmap中位图=位图;
    canvas.drawBitmap(sourceBitmap中,新的Rect(0,0,sourceBitmap.getWidth(),
            sourceBitmap.getHeight()),新的Rect(0,0,targetWidth,
            targetHeight),NULL);
    返回targetBitmap;

}

}
 

解决方案

该演示工程放在绘制文件夹中的任何图像,

但是,如果你要裁剪任何动态图像,例如被下载或画廊选择任何图像,使在code一些变化:

请参阅该行:

 位图bitmapOrg = BitmapFactory.de codeResource(
                    getResources(),
                    R.drawable.sachin_tendulkar_10102013);
 

在这里,我走的是图像的绘制文件夹,现在任何下载的图片,你只需要保存图像中的bitmapOrg变量,因此只需更改上面的行两次,一个是第一部分的矩形和第二部分的ciculart与您下载的图像保存到bitmapOrg为位图,并使用演示,它会裁剪图像的矩形和圆形的方式。

I needed a demo where any image can be cropped with the face detection function.

FIXED

But after few surfing hours I didn't come to a single demo, so I prepared a single demo with conjunction of few demos that I found online.

I have prepared a demo to crop the image.

My demo crops the image rectangle, and circular as well.

Also it detects the face and crops the image according to the face detection.

I am using the following image to crop it.

And the screenshot of the crop result is :

The xml for the example is :

<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>

The java code for the Activity :

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;

}

}

解决方案

The demo works for any image put in the drawable folder,

But if you want to crop any dynamic image, for example any image which is downloaded or chosen from gallery, make few changes in the code :

See the line :

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

Here I am taking the image from the drawable folder, now for any downloaded image, you just need to save that image in the bitmapOrg variable, so just change the above line twice, one for part1 for rectangle and part2 for ciculart with your downloaded image saving to bitmapOrg as bitmap, and use the demo, it will crop your image in rectangle and circular way.

这篇关于裁剪图像的人脸检测的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆