Camera API error, help?


Last Updated:

  1. neonv

    neonv New Member This Topic's Starter

    Joined:
    Nov 18, 2010
    Messages:
    2
    Likes Received:
    0
    Hi, I'm new to Android developing. I've started to write an camera application based on the Camera Preview code offered in the Android Developers's Resources site. However, when running the code that I have I get the error "Application Camera Demo (process com.ev.android.camera) has stopped unexpectedly. Please Try again later" and a forced exit.

    All I did to the Camera Preview code was to include a button in the screen that will allow users to snap photos when click and of course, the takepicture() function to take a picture.

    Here's my code:

    Code (Text):
    1. package com.ev.android.camera;
    2.  
    3. import java.io.IOException;
    4. import java.util.List;
    5.  
    6. import android.app.Activity;
    7. import android.content.Context;
    8. import android.hardware.Camera;
    9. import android.hardware.Camera.PictureCallback;
    10. import android.hardware.Camera.ShutterCallback;
    11. import android.hardware.Camera.Size;
    12. import android.os.Bundle;
    13. import android.view.SurfaceHolder;
    14. import android.view.SurfaceView;
    15. import android.view.View;
    16. import android.view.Window;
    17. import android.widget.Button;
    18. import android.widget.FrameLayout;
    19.  
    20. public class CameraApp extends Activity {
    21.     /** Called when the activity is first created. */
    22.    
    23.     private Preview mPreview;
    24.     private Button buttonSnap;
    25.    
    26.     @Override
    27.     public void onCreate(Bundle savedInstanceState) {
    28.         super.onCreate(savedInstanceState);
    29.        
    30.         //hide window title
    31.         requestWindowFeature(Window.FEATURE_NO_TITLE);
    32.        
    33.         mPreview = new Preview(this); //pass this as the context
    34.         //add the preview as a FrameLayout object
    35.         ((FrameLayout)findViewById(R.id.preview)).addView(mPreview);
    36.        
    37.         buttonSnap = (Button)findViewById(R.id.buttonSnap);
    38.        
    39.         //Camera will take the picture when the button is pressed.
    40.         buttonSnap.setOnClickListener(new View.OnClickListener(){
    41.             public void onClick(View v){
    42.                 mPreview.mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
    43.             }
    44.         });
    45.        
    46.         setContentView(mPreview);
    47.     }
    48.    
    49.     //Called when shutter is open
    50.     ShutterCallback shutterCallback = new ShutterCallback() {
    51.         public void onShutter() {
    52.             System.out.println("Taking picture...");
    53.         }
    54.     };
    55.    
    56.     //Handles data for raw picture
    57.     PictureCallback rawCallback = new PictureCallback(){
    58.         public void onPictureTaken(byte[] data, Camera camera){
    59.            
    60.         }
    61.     };
    62.    
    63.     //Handles data for JPEG picture
    64.     PictureCallback jpegCallback = new PictureCallback(){
    65.         public void onPictureTaken(byte[] data, Camera camera){
    66.            
    67.             // implement data handling here.
    68.         }
    69.     };
    70.    
    71.    
    72. }
    73.  
    74.  
    75. //-------------------Preview Class-------------------------
    76.  
    77. // Preview class for camera must be created for Camera to start its view
    78.  
    79. class Preview extends SurfaceView implements SurfaceHolder.Callback{
    80.     //member fields of the class Preview
    81.    
    82.     SurfaceHolder mHolder;
    83.     Camera mCamera;
    84.    
    85.     //class constructor
    86.     Preview(Context context){
    87.         super(context);
    88.        
    89.         mHolder = getHolder();
    90.         mHolder.addCallback(this);  //adds a SurfaceHolder.Callback in the holder.
    91.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    92.        
    93.        
    94.     }
    95.    
    96.     public void surfaceCreated(SurfaceHolder holder){
    97.         //surface has been created, camera can be notified to draw on the canvas
    98.        
    99.         mCamera = Camera.open();    // create a camera object
    100.         try{
    101.             mCamera.setPreviewDisplay(holder);  //set the cam's display to as the holder holding the view
    102.         //  mCamera.startPreview();
    103.         } catch (IOException exception){
    104.             mCamera.release();  // destroy the camera object
    105.             mCamera = null;
    106.         }
    107.     }
    108.    
    109.     public void surfaceDestroyed(SurfaceHolder holder){
    110.         mCamera.stopPreview();
    111.         mCamera.release();
    112.         mCamera = null;
    113.     }
    114.    
    115.      //------------------------------------------------------------------------------
    116.     //optimization function borrowed from Android Developers code: CameraPreview.java
    117.        private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    118.             final double ASPECT_TOLERANCE = 0.05;
    119.             double targetRatio = (double) w / h;
    120.             if (sizes == null) return null;
    121.  
    122.             Size optimalSize = null;
    123.             double minDiff = Double.MAX_VALUE;
    124.  
    125.             int targetHeight = h;
    126.  
    127.             // Try to find an size match aspect ratio and size
    128.             for (Size size : sizes) {
    129.                 double ratio = (double) size.width / size.height;
    130.                 if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
    131.                 if (Math.abs(size.height - targetHeight) < minDiff) {
    132.                     optimalSize = size;
    133.                     minDiff = Math.abs(size.height - targetHeight);
    134.                 }
    135.             }
    136.  
    137.             // Cannot find the one match the aspect ratio, ignore the requirement
    138.             if (optimalSize == null) {
    139.                 minDiff = Double.MAX_VALUE;
    140.                 for (Size size : sizes) {
    141.                     if (Math.abs(size.height - targetHeight) < minDiff) {
    142.                         optimalSize = size;
    143.                         minDiff = Math.abs(size.height - targetHeight);
    144.                     }
    145.                 }
    146.             }
    147.             return optimalSize;
    148.         }
    149.        
    150.        
    151.        //----------------------------------------------------------------------------
    152.            
    153.        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
    154.            // Now that the size is known, set up the camera parameters and begin
    155.            // the preview.
    156.            Camera.Parameters parameters = mCamera.getParameters();
    157.  
    158.            List<Size> sizes = parameters.getSupportedPreviewSizes();
    159.            Size optimalSize = getOptimalPreviewSize(sizes, w, h);
    160.            parameters.setPreviewSize(optimalSize.width, optimalSize.height);
    161.  
    162.             mCamera.setParameters(parameters);
    163.             mCamera.startPreview();
    164.         }
    165. }
    I've also added the permission in the manifest file:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.ev.android.camera"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    7.         <activity android:name=".CameraApp"
    8.                   android:label="@string/app_name">
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.         </activity>
    14.  
    15.     </application>
    16.    
    17.     <uses-feature android:name="android.hardware.camera" />
    18.     <uses-permission android:name="android.permission.CAMERA" />
    19.  
    20.  
    21. </manifest>
    This application is written at API level 5, and I'm using an emulator at API level 7 (Motorola Milestone's emulator). I've tried porting the application to the Milestone device itself and the same error appears.

    I've read that camera errors could be caused by lack of memory, declaration in the manifest file or the setPreviewSizes() function in the surfaceChanged() method, but I can't pin point what exactly is wrong with my project.

    Can someone please help me? >_<
     

    Advertisement

Share This Page

Loading...