在现场摄像头上绘图

时间:2011-04-01 19:42:03

标签: android android-camera

在我的应用程序中,我只是想在一个面部位置的实时摄像头上绘制一个矩形..我正在使用以下代码

@Override
    public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
             try {         
                 camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
                    public void onPreviewFrame(byte[]_data, Camera _camera) {
                    // TODO Do something with the preview image.
                        byte data[]=_data;
                        Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
                    DrawOnTop  mDraw.bitmap=b;

                    addContentView(mDraw, new LayoutParams 
                                (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    //detecteye(bitmap);

                    }
                    });
 } catch (IOException e)
             {         e.printStackTrace();    
             }    
             camera.startPreview();
}
class DrawOnTop extends View { 

    Bitmap bitmap;

    public DrawOnTop(Context context,AttributeSet attrs) { 

            // TODO Auto-generated constructor stub 
         super(context,attrs); 

    } 


    @Override 
    protected void onDraw(Canvas canvas) { 
            // TODO Auto-generated method stub 
        Paint ditherPaint = new Paint();
        Paint drawPaint = new Paint();
        if(null != bitmap)
        {                  int width = bitmap.getWidth();
        int height = bitmap.getHeight(); 
        FaceDetector detector = new FaceDetector(width, height,1);
        Face[] faces = new Face[1];
        //Bitmap bitmap565 = Bitmap.createBitmap(width, height, Config.RGB_565);

        ditherPaint.setDither(true);  
        drawPaint.setColor(Color.RED); 
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeWidth(2); 

        //canvas.setBitmap(bitmap565); 
        //canvas.drawBitmap(bitmap, 0, 0, ditherPaint);
        int facesFound = detector.findFaces(bitmap, faces);
        PointF midPoint = new PointF();
        float eyeDistance = 0.0f;
       // float confidence = 0.0f; 
       // Log.i("FaceDetector", "Number of faces found: " + facesFound);
        if(facesFound > 0)                 
        {                        
            for(int index=0; index<facesFound; ++index)
            {   // Get the eye distance, detected eye mid point and confidence  
                faces[index].getMidPoint(midPoint); 
                eyeDistance = faces[index].eyesDistance();
                //confidence = faces[index].confidence();
                //Log.i("FaceDetector",
                    //  "Confidence: " + confidence +
                        //", Eye distance: " + eyeDistance +   
                        //", Mid Point: (" + midPoint.x + ", " + midPoint.y + ")");
                // Draw a small rectangle frame around the eye   
                canvas.drawRect((int)midPoint.x - eyeDistance ,  
                        (int)midPoint.y - eyeDistance ,   
                        (int)midPoint.x + eyeDistance,  
                        (int)midPoint.y + eyeDistance, drawPaint); 
                }    

        }
        }




            super.onDraw(canvas); 


    } }

但是我在运行时不断收到错误没有命令输出:'我在我的错误日志中启动-n并且应用程序强制每次都关闭这真是令人沮丧,因为我被困在这两个星期以来:( PLZ有人能告诉我我可以摆脱这个错误,并考虑我想做的是有任何类似的简单例子

1 个答案:

答案 0 :(得分:0)

我不知道这是否与您的FC有关,但我不会将字节数组解码为onPreviewFrame回调中的Bitmap。每当我在相机预览上绘制自定义视图时,我只是将它添加到与SurfaceView相同的RelativeLayout。