Search code examples
androidcameragridlinesglsurfaceview

Android - show grid lines on camera


I am newbie in android application development. I want to create an application that get stream from camera and show on SurfaceView or on FrameLayout.

I need an option shows above on streaming "Show Grid Lines", when user click on it grid lines will show on camera stream.

Can anybody help me that how can I show grid lines on camera streaming???

Any Help will be appreciable...

Thank you. Mohsin


Solution

  • If you want to draw the lines dynamically as per your screen size then you should use the following code in your camera preview class.

        @Override  
        protected void onDraw(Canvas canvas)  
        { 
            if(grid){
            //  Find Screen size first  
            DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();  
            int screenWidth = metrics.widthPixels;  
            int screenHeight = (int) (metrics.heightPixels*0.9);  
    
            //  Set paint options  
            paint.setAntiAlias(true);  
            paint.setStrokeWidth(3);  
            paint.setStyle(Paint.Style.STROKE);  
            paint.setColor(Color.argb(255, 255, 255, 255));  
    
            canvas.drawLine((screenWidth/3)*2,0,(screenWidth/3)*2,screenHeight,paint);
            canvas.drawLine((screenWidth/3),0,(screenWidth/3),screenHeight,paint);
            canvas.drawLine(0,(screenHeight/3)*2,screenWidth,(screenHeight/3)*2,paint);
            canvas.drawLine(0,(screenHeight/3),screenWidth,(screenHeight/3),paint);
            }
        } 
    

    You also need to add the following line in the constructor of your camera preview class:

    this.setWillNotDraw(false);