Trying to take picture with camera

All your problems with Audio, Video and Images.

Trying to take picture with camera

Postby oxalus » Mon Sep 06, 2010 3:18 pm

Hi, everybody!

I'm newbie programming Android and it may look like a stupid question. :? :oops:

I have seen a lot of examples on how to take a picture using the device camera, but I could not make work any of them.

http://www.anddev.org/take_picture_from_camera_emulator-t168.html
http://marakana.com/forums/android/examples/39.html
http://github.com/commonsguy/cw-advandroid/tree/master/Camera/Picture
http://www.anddev.org/post22718.html

I have been a couple of weeks trying it, but I am really stuck. So, sorry if my question is a bit stupid but I think I'm complicating a very simple problem. :oops:

Here you have my code:
TestCamara1.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.camara1;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5.  
  6. import android.app.Activity;
  7. import android.graphics.PixelFormat;
  8. import android.hardware.Camera;
  9. import android.hardware.Camera.Size;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.widget.FrameLayout;
  15. import android.widget.TextView;
  16.  
  17. public class TestCamara1 extends Activity {
  18.         private static final String TAG = "TestCamara";                
  19.        
  20.         private Camera m_Camera = null;                                                        
  21.        
  22.         private String m_PictureName = null;                                           
  23.         private String m_FilePath = "/sdcard/img_folder";                              
  24.         private Size m_Size = null;                                                                    
  25.        
  26.         private FileOutputStream m_BitmapFos = null;                           
  27.        
  28.         private static final int CAPTURE_IMG = Menu.FIRST;                     
  29.        
  30.         private TextView txt_Test = null;
  31.                
  32.     /** Called when the activity is first created. */
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.        
  38.         Log.e(TAG,"Inicio");
  39.         txt_Test = (TextView)findViewById(R.id.txt_Test);
  40.     }
  41.    
  42.     @Override
  43.     public boolean onCreateOptionsMenu(Menu menu) {
  44.         super.onCreateOptionsMenu(menu);
  45.         menu.add(0, CAPTURE_IMG, 0, "Capture image");
  46.         return true;
  47.     }
  48.    
  49.     @Override
  50.     public boolean onMenuItemSelected(int featureId, MenuItem item) {
  51.         switch(item.getItemId()) {
  52.         case CAPTURE_IMG:
  53.                 Log.d(TAG,"Call take picture");
  54.             capturePicture();
  55.             return true;
  56.         }
  57.        
  58.         return super.onMenuItemSelected(featureId, item);
  59.     }    
  60.    
  61.         /**
  62.          * Take picture with specified parameters and format
  63.          */
  64.         public void capturePicture(){
  65.                 try
  66.                 {
  67.                         m_Camera.startPreview();                //NOTICE THAT: I added this because it failed to call Camera.open(); (fail to connect to camera service)
  68.                         m_Camera = Camera.open();
  69.                
  70.                         if (m_Camera == null)
  71.                 {
  72.                                 Log.e(TAG, "CapturePicture : Camera not available");
  73.                     return;
  74.                 }
  75.                                        
  76.                         Camera.Parameters params = m_Camera.getParameters();             
  77.                         m_Size = params.getPictureSize();
  78.                         params.setPictureFormat(PixelFormat.JPEG);             
  79.                         m_Camera.setParameters(params);
  80.                                                
  81.                         m_Camera.takePicture(null, null, photoCallback);
  82.                 } catch (Exception e) {
  83.                         txt_Test.setText(e.getMessage());
  84.                         releaseCamera();
  85.                 }
  86.         }
  87.  
  88.         /**
  89.          * Handles the callback for when a picture is taken.
  90.          */
  91.          Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
  92.                 public void onPictureTaken(byte[] data, Camera camera) {
  93.                         try {
  94.                                 Log.d(TAG,String.format(m_FilePath + "/%d.jpg", System.currentTimeMillis()).toString());
  95.                                
  96.                                 m_PictureName = String.format(m_FilePath + "/%d.jpg", System.currentTimeMillis());
  97.                                
  98.                                 m_BitmapFos = new FileOutputStream(m_PictureName);
  99.                                 m_BitmapFos.write(data);
  100.                                 m_BitmapFos.close();
  101.                                
  102.                                 releaseCamera();
  103.                         } catch (IOException e) {
  104.                                 // TODO Auto-generated catch block
  105.                                 releaseCamera();
  106.                                 e.printStackTrace();
  107.                         }
  108.                 }
  109.         };
  110.        
  111.         private void releaseCamera()
  112.         {
  113.                 if (m_Camera != null)
  114.                         m_Camera.release();            
  115.         }
  116. }
  117.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4



main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView  
  8.         android:id="@+id/txt_Test"
  9.    android:layout_width="fill_parent"
  10.    android:layout_height="wrap_content"
  11.    android:text="@string/hello"
  12.    />
  13. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.test.camara1"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.      
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  8.         <activity android:name=".TestCamara1"
  9.                  android:label="@string/app_name">
  10.             <intent-filter>
  11.                 <action android:name="android.intent.action.MAIN" />
  12.                 <category android:name="android.intent.category.LAUNCHER" />
  13.             </intent-filter>
  14.         </activity>
  15.        
  16.     </application>
  17.     <uses-sdk android:minSdkVersion="3" />
  18.    
  19.     <uses-permission android:name="android.permission.CAMERA"></uses-permission>
  20.     <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
  21.    
  22.     <uses-feature android:name="android.hardware.camera" />
  23.         <uses-feature android:name="android.hardware.camera.autofocus" />
  24.    
  25. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


strings.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, TestCamara1!</string>
  4.     <string name="app_name">TestCamara1</string>
  5. </resources>
  6.  
  7.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


The problem is that when showing the menu and selecting "Take Picture" option, nothing happens.

I have an HTC Legend device connected through USB to my computer and I am debugging directly on it (via USB debug option). I work with Eclipse 3.6.0 and my device runs under Android 2.1, but I would like to set the SDK version to 3 (that's Android 1.5) to make sure it works in the most devices possible.

Sorry for my English, I hope it is clear :oops: ... And once again, excuse me if it is a stupid question but I am really stuck and I really need some help :cry: .

Thanks a lot!
oxalus
Freshman
Freshman
 
Posts: 2
Joined: Fri Aug 27, 2010 1:05 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests