Finger Paint and Camera Capture

All your problems with Audio, Video and Images.

Finger Paint and Camera Capture

Postby glassfish » Tue Apr 14, 2009 8:23 am

Hi,
I'm trying to allow a user to take a picture with a camera, and then 'draw' over this image using the finger paint application in the API Demos. This works OK on the emulator, but on the phone the fingerpaint intent never shows up; is it simply too slow/intensive, or am I doing something wrong? Why does it stay black when I try to run the fingerpaint app?

The steps are as follows:
1. Create Camera Stuff, and do camera.takePicture(blah), and onCallbackJpeg (byte[] data ...) {//store data}
2. A Button in Options menu starts a new activity (the fingerpaint application), but I do Intent.putextras, and put in the byte array so the fingerpaint application has access to the bitmap data
3. In fingerpaint, do getIntent().getExtras() and recover the byte array. Then, convert the byte array to a mutable bitmap and set that bitmap as the background canvas

Anyone have any ideas how to speed this up so it works on hardware?
Camera Code is posted Below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.android.CamTest;
  3.  
  4.  
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.content.Context;
  11.  
  12. import android.content.Intent;
  13.  
  14. import android.graphics.Bitmap;
  15.  
  16. import android.hardware.Camera;
  17.  
  18. import android.hardware.Camera.PictureCallback;
  19.  
  20. import android.os.Bundle;
  21.  
  22. import android.view.Menu;
  23.  
  24. import android.view.MenuItem;
  25.  
  26. import android.view.SurfaceHolder;
  27.  
  28. import android.view.SurfaceView;
  29.  
  30. import android.view.Window;
  31.  
  32.  
  33.  
  34.  
  35.  
  36. // ----------------------------------------------------------------------
  37.  
  38.  
  39.  
  40. public class CamTest extends Activity {    
  41.  
  42.     private Preview mPreview;
  43.  
  44.    
  45.  
  46.     @Override
  47.  
  48.         protected void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);
  51.  
  52.        
  53.  
  54.         // Hide the window title.
  55.  
  56.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  57.  
  58.    
  59.  
  60.         // Create our Preview view and set it as the content of our activity.
  61.  
  62.         mPreview = new Preview(this);
  63.  
  64.         setContentView(mPreview);
  65.  
  66.     }
  67.  
  68.  
  69.  
  70.     @Override
  71.  
  72.     public boolean onCreateOptionsMenu(Menu menu) {
  73.  
  74.         super.onCreateOptionsMenu(menu);
  75.  
  76.         menu.add(0, 1, 0, "take picture");
  77.  
  78.         menu.add(0, 2, 0, "view picture");
  79.  
  80.         return true;
  81.  
  82.     }
  83.  
  84.  
  85.  
  86.     @Override
  87.  
  88.     public boolean onMenuItemSelected(int featureId, MenuItem item) {
  89.  
  90.         switch(item.getItemId()) {
  91.  
  92.         case 1:
  93.  
  94.                 try {
  95.  
  96.                         mPreview.captureImage();
  97.  
  98.                 } catch (Exception e) {
  99.  
  100.                         e.printStackTrace();
  101.  
  102.                 }
  103.  
  104.                 break;
  105.  
  106.         case 2:
  107.  
  108.                 Intent i = new Intent(this, FingerPaint.class);
  109.  
  110.                         if(mPreview.picdata != null)
  111.  
  112.                                 i.putExtra("picdata", mPreview.picdata);
  113.  
  114. //              mPreview.mCamera.release();
  115.  
  116. //                      setContentView(R.layout.main);
  117.  
  118.                 startActivity(i);
  119.  
  120.                 break;
  121.  
  122.         }
  123.  
  124.         return true;
  125.  
  126.    }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136. // ----------------------------------------------------------------------
  137.  
  138.  
  139.  
  140. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  141.  
  142.     SurfaceHolder mHolder;
  143.  
  144.     public Camera mCamera;
  145.  
  146.     public Bitmap bm;
  147.  
  148.     public byte[] picdata;
  149.  
  150.     Preview(Context context) {
  151.  
  152.         super(context);
  153.  
  154.        
  155.  
  156.         // Install a SurfaceHolder.Callback so we get notified when the
  157.  
  158.         // underlying surface is created and destroyed.
  159.  
  160.         mHolder = getHolder();
  161.  
  162.         mHolder.addCallback(this);
  163.  
  164.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  165.  
  166.     }
  167.  
  168.  
  169.  
  170.     public void surfaceCreated(SurfaceHolder holder) {
  171.  
  172.         // The Surface has been created, acquire the camera and tell it where
  173.  
  174.         // to draw.
  175.  
  176.         mCamera = Camera.open();
  177.  
  178.         mCamera.setPreviewDisplay(holder);
  179.  
  180.     }
  181.  
  182.  
  183.  
  184.     public void surfaceDestroyed(SurfaceHolder holder) {
  185.  
  186.         // Surface will be destroyed when we return, so stop the preview.
  187.  
  188.         // Because the CameraDevice object is not a shared resource, it's very
  189.  
  190.         // important to release it when the activity is paused.
  191.  
  192.         mCamera.stopPreview();
  193.  
  194.         mCamera = null;
  195.  
  196.     }
  197.  
  198.  
  199.  
  200.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  201.  
  202.         // Now that the size is known, set up the camera parameters and begin
  203.  
  204.         // the preview.
  205.  
  206.         Camera.Parameters parameters = mCamera.getParameters();
  207.  
  208.         parameters.setPreviewSize(w, h);
  209.  
  210.         mCamera.setParameters(parameters);
  211.  
  212.         mCamera.startPreview();
  213.  
  214.     }
  215.  
  216.  
  217.  
  218.     public void captureImage() {
  219.  
  220.         Camera.Parameters params = mCamera.getParameters();
  221.  
  222.    
  223.  
  224.         mCamera.setParameters(params);
  225.  
  226.         Camera.PictureCallback cb = new PictureCallback() {
  227.  
  228.                 public void onPictureTaken(byte[] data, Camera camera) {
  229.  
  230.                         picdata = data;
  231.  
  232. //                      bm = BitmapFactory.decodeByteArray(data, 0, data.length);
  233.  
  234.                         try {
  235.  
  236. //                  FileOutputStream out = new FileOutputStream("/sdcard/mypick.png");
  237.  
  238.                            
  239.  
  240. //                  myimage.compress(Bitmap.CompressFormat.PNG, 10, out);
  241.  
  242. //                         out.close();
  243.  
  244.                                
  245.  
  246.                 } catch (Exception e) {
  247.  
  248.                             e.printStackTrace();
  249.  
  250.                     }
  251.  
  252.                        
  253.  
  254. //                      ImageView mImageDisplay = (ImageView) findViewById(R.id.image_display);
  255.  
  256. //                      mImageDisplay.setImageBitmap(b);
  257.  
  258.                 }
  259.  
  260.         };
  261.  
  262.         mCamera.takePicture(null, null, cb);
  263.  
  264.         mCamera.stopPreview();
  265.  
  266. //      mCamera.release();
  267.  
  268.     }
  269.  
  270. }
  271.  
  272.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
glassfish
Freshman
Freshman
 
Posts: 9
Joined: Sun Jan 18, 2009 10:23 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 8 guests