我正在开发一个Android应用程序,用于在图像上标记照片,使用人脸检测。此应用程序就像Facebook在Facebook上检测到照片中的面孔一样在Facebook上标记照片。
我的主要问题是,我不知道如何返回faceID并进行图像标记,我做了很多尝试,但没有成功。这是我的人脸检测编码。
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;
}
}