开发者

Drawing on top of a live camera feed

开发者 https://www.devze.com 2023-02-21 04:12 出处:网络
in my app i simply want to draw a rectangle on top of a live camera feed at the position of a face ..for which i am using the following code

in my app i simply want to draw a rectangle on top of a live camera feed at the position of a face ..for which i am using the following code

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


    } }

But i keep getting the error No command output when running: 'am start -n in my error log and the app force closes everytime which is really frustrating since i am stuck on this since two weeks :( plz can someone tell me hw i can get rid of this error and considering what i want to do is there any simple example similar to it


I don't know if this is related to your FC, but I wouldn't decode a byte array into a Bitmap in the onPreviewFrame callback. Whenever I've drawn a custom view on top of the camera preview, I just added it to the same RelativeLayout as the SurfaceView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号