0

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

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


    } }

但是我在运行时不断收到错误No command output:'am start -n in my error log and the app force每次都关闭,这真的很令人沮丧,因为我两周以来一直坚持这个问题:(请有人告诉我我可以摆脱这个错误,考虑到我想要做什么,有没有类似的简单例子

4

1 回答 1

0

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

于 2011-04-01T22:20:58.257 回答