在ImageView

时间:2018-02-17 11:32:28

标签: android imageview

我正在尝试在Android应用中使用Google Mobile Vision实现面部检测。在应用程序中,我有一个Imageview和一个带文本的按钮," Process"。

点击该按钮后,代码会与Google的Vision API连接并检测到脸部。在检测到脸部后,我试图在脸部周围画一个矩形。为此我正在使用" Canvas"适用于Android。

错误(不完全是)我无法看到脸部周围的矩形。这是我的MainActivity.java文件中的代码:

package com.startertutorials.googlefacedetect;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;
import com.google.android.gms.vision.face.Landmark;

public class MainActivity extends Activity {

    ImageView imageView;
    Button btnProcess;

    Bitmap myBitmap;
    Canvas canvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.imageView);
        btnProcess = (Button)findViewById(R.id.btnProcess);

        myBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.me);
        //imageView.setImageBitmap(myBitmap);

        Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), myBitmap.getConfig());
        canvas = new Canvas(tempBitmap);

        btnProcess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext())
                        .setTrackingEnabled(false)
                        .setLandmarkType(FaceDetector.ALL_LANDMARKS)
                        .setMode(FaceDetector.FAST_MODE)
                        .build();

                if (!faceDetector.isOperational()) {
                    Toast.makeText(MainActivity.this, "Face Detector Setup Failed!", Toast.LENGTH_SHORT).show();
                    return;
                }

                Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
                SparseArray<Face> sparseArray = faceDetector.detect(frame);
                for (int i = 0; i < sparseArray.size(); i++) {
                    Face face = sparseArray.valueAt(i);
                    detectLandmarks(face);
                }
            }
        });
    }

    private void detectLandmarks(Face face) {
        float height = face.getHeight();
        float width = face.getWidth();
        PointF p = face.getPosition();
        float cx = p.x;
        float cy = p.y;
        drawOnImageView(cx, cy, height, width);
        /*
        for(Landmark landmark:face.getLandmarks())
        {
            int cx = (int)landmark.getPosition().x;
            int cy = (int)landmark.getPosition().y;

            drawOnImageView(landmark.getType(),cx,cy);
        }
        */
    }

    private void drawOnImageView(float cx, float cy, float height, float width) {
        Paint myPaint = new Paint();
        myPaint.setColor(Color.GREEN);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(2);
        canvas.drawBitmap(myBitmap, 0, 0, null);
        canvas.drawRect(0, 0, 200, 200, myPaint);
        imageView.setImageBitmap(myBitmap);
        Log.d("Success", "Face detected successfully");
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

点击&#34;处理&#34;后的输出应用程序中的按钮如下:

enter image description here

在Android工作室的logcat中,我可以看到消息&#34; Face检测到成功&#34;。所以,我假设我的所有代码都在运行。但我无法看到矩形。

请先帮我显示一个矩形。稍后我会调整它以显示在脸部周围。

1 个答案:

答案 0 :(得分:-1)

您必须扩展Detector类:

https://developers.google.com/android/reference/com/google/android/gms/vision/Detector

定义RectangleDetector类。检测矩形的代码将通过覆盖detect()方法来实现。您需要自己实现,因为在移动视觉中还没有用于检测矩形的代码。

如果你有这个,你就可以将它与CameraSource和移动视觉API的其他部分一起使用。