Java 在Android Studio中实现人脸检测的照片标记
我正在开发一个android应用程序,该程序使用人脸检测在图像上标记照片。该应用程序就像Facebook检测到照片中的人脸后在Facebook上标记照片一样 我的主要问题是,我不知道如何返回faceID和做图像标记,我做了很多尝试,但没有成功。这是我的人脸检测编码Java 在Android Studio中实现人脸检测的照片标记,java,android,android-studio,face-detection,Java,Android,Android Studio,Face Detection,我正在开发一个android应用程序,该程序使用人脸检测在图像上标记照片。该应用程序就像Facebook检测到照片中的人脸后在Facebook上标记照片一样 我的主要问题是,我不知道如何返回faceID和做图像标记,我做了很多尝试,但没有成功。这是我的人脸检测编码 package com.application.nurfatinahjannah.muka; import android.os.Bundle; import com.microsoft.projectoxford.face.*;
package com.application.nurfatinahjannah.muka;
import android.os.Bundle;
import com.microsoft.projectoxford.face.*;
import com.microsoft.projectoxford.face.contract.*;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.io.*;
import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;
import android.graphics.*;
import android.widget.*;
import android.provider.*;
公共类CameraActivity扩展到第二个{
ImageButton takephoto;
private FaceServiceClient faceServiceClient =
new FaceServiceRestClient("09eec022662e429ba6f2df36454ff120");
private final int PICK_IMAGE = 1;
private ProgressDialog detectionProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
takephoto = (ImageButton)findViewById(R.id.takephoto);
takephoto.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent gallIntent = new Intent(Intent.ACTION_GET_CONTENT);
gallIntent.setType("image/*");
startActivityForResult(Intent.createChooser(gallIntent, "Select Picture"), PICK_IMAGE);
}
});
detectionProgressDialog = new ProgressDialog(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ImageView imageView = (ImageView) findViewById(R.id.imageview1);
imageView.setImageBitmap(bitmap);
detectAndFrame(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void detectAndFrame(final Bitmap imageBitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
ByteArrayInputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray());
AsyncTask<InputStream, String, Face[]> detectTask = new AsyncTask<InputStream, String, Face[]>()
{
@Override
protected Face[] doInBackground(InputStream... params) {
try {
publishProgress("Detecting...");
Face[] result = faceServiceClient.detect(
params[0],
true , // returnFaceId
false, // returnFaceLandmarks
null // returnFaceAttributes: a string like "age, gender"
);
if (result == null)
{
publishProgress("Detection Finished. Nothing detected");
return null;
}
publishProgress(
String.format("Detection Finished. %d face(s) detected",
result.length));
return result;
} catch (Exception e) {
publishProgress("Detection failed");
return null;
}
}
@Override
protected void onPreExecute() {
detectionProgressDialog.show();
}
@Override
protected void onProgressUpdate(String... progress) {
detectionProgressDialog.setMessage(progress[0]);
}
@Override
protected void onPostExecute(Face[] result) {
detectionProgressDialog.dismiss();
if (result == null) return;
ImageView imageView = (ImageView)findViewById(R.id.imageview1);
imageView.setImageBitmap(drawFaceRectanglesOnBitmap(imageBitmap, result));
imageBitmap.recycle();
}
};
detectTask.execute(inputStream);
}
private static Bitmap drawFaceRectanglesOnBitmap(Bitmap originalBitmap, Face[] faces) {
Bitmap bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
int stokeWidth = 2;
paint.setStrokeWidth(stokeWidth);
if (faces != null) {
for (Face face : faces) {
FaceRectangle faceRectangle = face.faceRectangle;
canvas.drawRect(
faceRectangle.left,
faceRectangle.top,
faceRectangle.left + faceRectangle.width,
faceRectangle.top + faceRectangle.height,
paint);
}
}
return bitmap;
}
ImageButton拍照;
专用FaceServiceClient FaceServiceClient=
新FaceServiceRestClient(“09eec022662e429ba6f2df36454ff120”);
私有最终整数选取图像=1;
私有进程对话框检测进程对话框;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_摄像头);
takephoto=(ImageButton)findViewById(R.id.takephoto);
takephoto.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Intent gallIntent=新意图(Intent.ACTION\u GET\u CONTENT);
gallIntent.setType(“image/*”);
startActivityForResult(Intent.createChooser(gallIntent,“选择图片”),PICK_IMAGE);
}
});
detectionProgressDialog=新建ProgressDialog(此);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==PICK_IMAGE&&resultCode==RESULT_OK&&data!=null&&data.getData()!=null){
Uri=data.getData();
试一试{
位图Bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
ImageView ImageView=(ImageView)findViewById(R.id.imageview1);
设置图像位图(位图);
检测帧(位图);
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
私有void detectAndFrame(最终位图图像位图)
{
ByteArrayOutputStream outputStream=新建ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
ByteArrayInputStream输入流=
新建ByteArrayInputStream(outputStream.toByteArray());
AsyncTask detectTask=新建AsyncTask()
{
@凌驾
受保护的面[]doInBackground(输入流…参数){
试一试{
出版进度(“检测…”);
Face[]结果=faceServiceClient.detect(
参数[0],
true,//returnFaceId
false,//returnFaceLandmarks
null//returnFaceAttributes:类似“年龄、性别”的字符串
);
如果(结果==null)
{
publishProgress(“检测完成,未检测到任何内容”);
返回null;
}
出版进展(
格式(“检测完成,检测到%d个面”,
结果(长度);
返回结果;
}捕获(例外e){
发布进度(“检测失败”);
返回null;
}
}
@凌驾
受保护的void onPreExecute(){
detectionProgressDialog.show();
}
@凌驾
受保护的void onProgressUpdate(字符串…进度){
detectionProgressDialog.setMessage(进度[0]);
}
@凌驾
受保护的void onPostExecute(面[]结果){
detectionProgressDialog.Disclose();
if(result==null)返回;
ImageView ImageView=(ImageView)findViewById(R.id.imageview1);
setImageBitmap(drawFaceRectanglesOnBitmap(imageBitmap,结果));
recycle();
}
};
执行(inputStream);
}
专用静态位图drawFaceRectanglesOnBitmap(位图原始位图,面[]面){
位图Bitmap=originalBitmap.copy(Bitmap.Config.ARGB_8888,true);
画布=新画布(位图);
油漆=新油漆();
paint.setAntiAlias(真);
绘制.设置样式(绘制.样式.笔划);
油漆。设置颜色(颜色。红色);
宽度=2;
油漆。设置行程宽度(行程宽度);
如果(面!=null){
用于(面:面){
FaceRectangle FaceRectangle=face.FaceRectangle;
canvas.drawRect(
faceRectangle.left,
faceRectangle.top,
faceRectangle.left+faceRectangle.width,
faceRectangle.top+faceRectangle.height,
油漆);
}
}
返回位图;
}
}你解决了这个问题吗?你解决了这个问题吗?