我在檢測瀏覽圖像的人臉時遇到了一些問題。我知道這個問題是我不知道如何應用我在導入的圖像上測試的人臉檢測代碼。我正在測試的示例代碼是爲本地存儲的圖像編寫的。我相信我很接近,但你能幫我嗎?瀏覽圖像和人臉檢測
首先,我創建了一個畫廊方法
protected void gallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction("android.intent.action.GET_CONTENT");
startActivityForResult(Intent.createChooser(intent, "Choose An Image"), 1);
}
我仍然在學習的意圖等,但據我瞭解,我需要使用意圖使用Android的畫廊,因爲我的setAction得到內容,我也在用它來傳遞信息。就這樣說,我試圖將信息傳遞給一個uri。所以這就是我接下來要做的。
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == 1 && resultCode == RESULT_OK)
{
Uri uri = intent.getData();
try {
InputStream is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView image = (ImageView)findViewById(R.id.img_view);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
因此,這裏是我感到困惑的部分。我猜InputStream有圖像信息?那麼我試圖在同一個try-catch中應用人臉檢測代碼。我認爲image.setImageBitmap(位圖)完成後,即是應用人臉檢測的時間。這是人臉檢測代碼。
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == 1 && resultCode == RESULT_OK)
{
Uri uri = intent.getData();
try {
InputStream is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
ImageView image = (ImageView)findViewById(R.id.image_view);
image.setImageBitmap(bitmap);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeResource(getResources(), R.id.img_view, options);
imageWidth = bitmap.getWidth();
imageHeight = bitmap.getHeight();
detectedFaces = new FaceDetector.Face[NUM_FACES];
faceDetector= new FaceDetector(imageWidth, imageHeight, NUM_FACES);
NUM_FACE_DETECTED = faceDetector.findFaces(bitmap, detectedFaces);
mIL.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我不知道如何改變 「mFaceBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.smilingfaces,選項);」這是本地圖像,我認爲是存儲在InputStream中的圖像(或者它是?所選圖像在哪裏?)我想出了這個想法,而不是做imageView佈局,因爲圖像在佈局中。我不明白這一切是如何轉移和一起工作。無論如何,該代碼段假設檢測面部。然後onDraw()在檢測到的臉部周圍畫出正方形。我不知道該把它放在哪裏,但我把它放在onActivityResult()以外的地方。
protected void onDraw(Canvas canvas) {
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
myPaint.setDither(true);
for (int count = 0; count < NUM_FACE_DETECTED; count++) {
Face face = detectedFaces[count];
PointF midPoint = new PointF();
face.getMidPoint(midPoint);
eyeDistance = face.eyesDistance();
canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);
}
}
有什麼建議嗎?我非常接近這個工作!
我想出了一件事。我的onDraw沒有被調用,所以我添加了invalidate();另外,我有一個想法。由於我在imageView中顯示所選圖像,因此可以執行bitmap = BitmapFactory.decodeResource(getResources(),R.id.image_view,options);這些好步驟?仍然沒有工作。如果我不擴展ImageView,我不相信onDraw的作品?或者這很重要? – portfoliobuilder