Drawing on Camera Preview?

All your problems with Audio, Video and Images.

Drawing on Camera Preview?

Postby srajpal » Thu Jan 29, 2009 9:54 pm

Does anybody know how to draw on the camera preview?

I created a camera preview using SurfaceView like in the example and i was wondering how to draw an image on top of that preview.
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

Top

SOLVED!

Postby srajpal » Sun Feb 01, 2009 4:59 am

I was able to solve this easily
I created another View and placed it on top of the camera preview using addContentView
here is the full code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.content.Context;
  5.  
  6. import android.graphics.Canvas;
  7.  
  8. import android.graphics.Color;
  9.  
  10. import android.graphics.Paint;
  11.  
  12. import android.hardware.Camera;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.SurfaceHolder;
  17.  
  18. import android.view.SurfaceView;
  19.  
  20. import android.view.View;
  21.  
  22. import android.view.Window;
  23.  
  24. import android.view.ViewGroup.LayoutParams;
  25.  
  26.  
  27.  
  28. public class TestCameraOverlay extends Activity {
  29.  
  30.     /** Called when the activity is first created. */
  31.  
  32.     @Override
  33.  
  34.     public void onCreate(Bundle savedInstanceState) {
  35.  
  36.         super.onCreate(savedInstanceState);
  37.  
  38.        
  39.  
  40.  
  41.  
  42.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  43.  
  44.  
  45.  
  46.         Preview mPreview = new Preview(this);
  47.  
  48.         DrawOnTop mDraw = new DrawOnTop(this);
  49.  
  50.    
  51.  
  52.         setContentView(mPreview);
  53.  
  54.         addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  55.  
  56.        
  57.  
  58.     }
  59.  
  60. }
  61.  
  62.  
  63.  
  64. class DrawOnTop extends View {
  65.  
  66.  
  67.  
  68.         public DrawOnTop(Context context) {
  69.  
  70.                 super(context);
  71.  
  72.                 // TODO Auto-generated constructor stub
  73.  
  74.         }
  75.  
  76.  
  77.  
  78.         @Override
  79.  
  80.         protected void onDraw(Canvas canvas) {
  81.  
  82.                 // TODO Auto-generated method stub
  83.  
  84.                
  85.  
  86.                 Paint paint = new Paint();
  87.  
  88.                 paint.setStyle(Paint.Style.FILL);
  89.  
  90.                 paint.setColor(Color.BLACK);
  91.  
  92.                 canvas.drawText("Test Text", 10, 10, paint);
  93.  
  94.                
  95.  
  96.                 super.onDraw(canvas);
  97.  
  98.         }
  99.  
  100.        
  101.  
  102. }
  103.  
  104.  
  105.  
  106. //----------------------------------------------------------------------
  107.  
  108.  
  109.  
  110. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  111.  
  112.     SurfaceHolder mHolder;
  113.  
  114.     Camera mCamera;
  115.  
  116.    
  117.  
  118.     Preview(Context context) {
  119.  
  120.         super(context);
  121.  
  122.        
  123.  
  124.         // Install a SurfaceHolder.Callback so we get notified when the
  125.  
  126.         // underlying surface is created and destroyed.
  127.  
  128.         mHolder = getHolder();
  129.  
  130.         mHolder.addCallback(this);
  131.  
  132.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.     public void surfaceCreated(SurfaceHolder holder) {
  139.  
  140.         // The Surface has been created, acquire the camera and tell it where
  141.  
  142.         // to draw.
  143.  
  144.         mCamera = Camera.open();
  145.  
  146.         mCamera.setPreviewDisplay(holder);
  147.  
  148.     }
  149.  
  150.  
  151.  
  152.     public void surfaceDestroyed(SurfaceHolder holder) {
  153.  
  154.         // Surface will be destroyed when we return, so stop the preview.
  155.  
  156.         // Because the CameraDevice object is not a shared resource, it's very
  157.  
  158.         // important to release it when the activity is paused.
  159.  
  160.         mCamera.stopPreview();
  161.  
  162.         mCamera = null;
  163.  
  164.     }
  165.  
  166.  
  167.  
  168.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  169.  
  170.         // Now that the size is known, set up the camera parameters and begin
  171.  
  172.         // the preview.
  173.  
  174.         Camera.Parameters parameters = mCamera.getParameters();
  175.  
  176.         parameters.setPreviewSize(w, h);
  177.  
  178.         mCamera.setParameters(parameters);
  179.  
  180.         mCamera.startPreview();
  181.  
  182.     }
  183.  
  184.  
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google Feedfetcher and 14 guests