0

我正在尝试编写一个 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); 
}
}
4

2 回答 2

0

如何删除线:

 bitmap.recycle();

根据安卓开发者的说法:

注意:只有在确定不再使用位图时才应使用 recycle()。如果您调用 recycle() 并稍后尝试绘制位图,您将收到错误:“画布:尝试使用回收的位图”。

你最好让GC为你做这件事。

http://developer.android.com/training/displaying-bitmaps/manage-memory.html

于 2013-07-19T17:13:36.217 回答
0

请您尝试添加 invalidate() 调用:

public static void setBitmap(Bitmap bitmap) {
 myBitmap = bitmap;
 this.invalidate();
}
于 2013-07-19T18:06:48.907 回答