|
package com.example.pineoc.facedetection; |
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
import android.os.Bundle; |
|
import android.app.Activity; |
|
import android.app.AlertDialog; |
|
import android.graphics.Bitmap; |
|
import android.graphics.BitmapFactory; |
|
import android.graphics.Canvas; |
|
import android.graphics.Color; |
|
import android.graphics.Paint; |
|
import android.graphics.RectF; |
|
import android.graphics.drawable.BitmapDrawable; |
|
import android.os.Bundle; |
|
import android.util.Log; |
|
import android.util.SparseArray; |
|
import android.view.View; |
|
import android.widget.Button; |
|
import android.widget.ImageView; |
|
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; |
|
|
|
import java.util.List; |
|
|
|
public class MainActivity extends AppCompatActivity { |
|
|
|
@Override |
|
protected void onCreate(Bundle savedInstanceState) { |
|
super.onCreate(savedInstanceState); |
|
setContentView(R.layout.activity_main); |
|
|
|
Button btn = (Button) findViewById(R.id.button); |
|
btn.setOnClickListener(new View.OnClickListener() { |
|
@Override |
|
public void onClick(View v) { |
|
ImageView myImageView = (ImageView) findViewById(R.id.imgview); |
|
BitmapFactory.Options options = new BitmapFactory.Options(); |
|
options.inMutable = true; |
|
Bitmap myBitmap = BitmapFactory.decodeResource( |
|
getApplicationContext().getResources(), |
|
R.drawable.test1, |
|
options); |
|
|
|
//rect color paint |
|
Paint myRectPaint = new Paint(); |
|
myRectPaint.setStrokeWidth(5); |
|
myRectPaint.setColor(Color.RED); |
|
myRectPaint.setStyle(Paint.Style.STROKE); |
|
//circle color paint |
|
Paint Pnt = new Paint(); |
|
Pnt.setColor(Color.GREEN); |
|
Pnt.setStrokeWidth(5); |
|
Pnt.setStyle(Paint.Style.STROKE); |
|
|
|
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); |
|
Canvas tempCanvas = new Canvas(tempBitmap); |
|
tempCanvas.drawBitmap(myBitmap, 0, 0, null); |
|
|
|
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext()) |
|
.setTrackingEnabled(false) |
|
.setLandmarkType(FaceDetector.ALL_LANDMARKS) |
|
.build(); |
|
if(!faceDetector.isOperational()){ |
|
new AlertDialog.Builder(v.getContext()).setMessage("couldn't set up the face detector!"); |
|
return; |
|
} |
|
|
|
Frame frame = new Frame.Builder().setBitmap(myBitmap).build(); |
|
SparseArray<Face> faces = faceDetector.detect(frame); |
|
|
|
for(int i=0; i<faces.size(); i++){ |
|
Face thisFace = faces.valueAt(i); |
|
float x1 = thisFace.getPosition().x; |
|
float y1 = thisFace.getPosition().y; |
|
float x2 = x1 + thisFace.getWidth(); |
|
float y2 = y1 + thisFace.getHeight(); |
|
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, myRectPaint); |
|
|
|
//draw landmarks |
|
List<Landmark> landmarks = thisFace.getLandmarks(); |
|
//for make landmark circle |
|
for(int j=0; j<landmarks.size(); j++){ |
|
Landmark thisLand = landmarks.get(j); |
|
float landx = thisLand.getPosition().x; |
|
float landy = thisLand.getPosition().y; |
|
tempCanvas.drawCircle(landx, landy, 10, Pnt); |
|
} |
|
} |
|
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap)); |
|
faceDetector.release(); |
|
} |
|
}); |
|
} |
|
} |