我正在尝试编写一个 android 应用程序,该应用程序允许您通过意图从图库中选择图像,然后 FaceDetector 将对其进行分析并在同一视图中显示。但是,到目前为止,我在显示图像时遇到了麻烦。有人可以帮忙吗??提前致谢
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearlayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/loadimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image"
/>
<TextView
android:id="@+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<view
class="com.example.facedetectionstaticcamera.myView"
android:id="@+id/analysedimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
主要活动
package com.example.facedetectionstaticcamera;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
TextView textTargetUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view){
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri), null, BitmapFactoryOptionsbfo);
LinearLayout v = (LinearLayout) findViewById(R.id.linearlayout);
myView newView = new myView(this, null);
myView.setBitmap(bitmap);
v.addView(newView);
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
我的观点
package com.example.facedetectionstaticcamera;
import java.io.File;
import com.example.facedetectionstaticcamera.R;
import android.content.Context;
import android.content.Intent;
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.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class myView extends View{
static Bitmap myBitmap;
public static void setBitmap(Bitmap bitmap) {
myBitmap = bitmap;
}
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(myBitmap, 0, 0, null);
}
}