无法使用画布在自定义视图中显示位图

时间:2013-07-19 16:56:06

标签: android bitmap face-detection

我试图编写一个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>

MainActivity

    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();
     }

    }
    }
    }

MyView的

     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); 
}
}

2 个答案:

答案 0 :(得分:0)

如何删除线:

 bitmap.recycle();

根据android开发人员的说法:

警告:只有在确定不再使用位图时才应使用recycle()。如果您调用recycle()并稍后尝试绘制位图,您将收到错误:“Canvas:尝试使用循环位图”。

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

  

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

答案 1 :(得分:0)

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

public static void setBitmap(Bitmap bitmap) {
 myBitmap = bitmap;
 this.invalidate();
}