Android – Camera –an Example

Slides:



Advertisements
Similar presentations
- Rohit Vobbilisetty. What is the Camera API Capture Photos Capture Videos Camera API - Android2.
Advertisements

Multimedia.  Android provides built-in encoding/decoding for a variety of common media types  Allows you to play & record audio, still images & video.
Cosc 5/4730 Android media Part 2: Pictures and Video.
Manifest File, Intents, and Multiple Activities. Manifest File.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Android Development (Basics)
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Data Storage: Part 1 (Preferences)
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Introducing the Sudoku Example
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Chapter 2: Simplify! The Android User Interface
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android - Broadcast Receivers
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Mobile Device Development Camera and Sensors Dr.YingLiang Ma.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Android media Part 2: Pictures and Video
A UGMENTED F UTURE OF M OBILE R EALITY Oleg Novosad Mobile / Web / Game Engineer.
Lecture 2: Android Concepts
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
Chapter 2: Simplify! The Android User Interface
Android Programming - Features
Android Introduction Hello World
Lecture 2: Android Concepts
Camera.
Android – Event Handling
Android Introduction Hello World.
Android – Read/Write to External Storage
Android Introduction Camera.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Many thanks to Jun Bum Lim for his help with this tutorial.
Cannon Game App Android How to Program
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Activities and Intents
CIS 470 Mobile App Development
Android Notifications
Lecture 2: Android Concepts
Activities and Fragments
Chapter 5 Your Second Activity.
CIS 694/EEC 693 Android Sensor Programming
CIS 470 Mobile App Development
Presentation transcript:

Android – Camera –an Example L. Grewe See http://developer.android.com/guide/topics/media/camera.html See http://developer.android.com/reference/android/hardware/Camera.html

Android Devices have cameras What can you do with a camera?

Must ask permission to use Android Camera or its features Examples (first one is the one we use)…PUT in Manifest.xml file  <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />

Two Options…for coding See http://developer.android.com/guide/topics/media /camera.html OPTION 1) Create Intent to existing Camera App--- just to take quick picture and get file returning to your app…camera picture controls not part of your app. OPTION 2) Create your own camera controls inside your app and take a picture

Some Classes involved….. Option 1: via Intent to existing Camera App Intent An intent action type of MediaStore.ACTION_IMAGE_CAPTURE or MediaStore.ACTION_VIDEO_CAPTURE can be used to capture Option 2: via Your own controls and Interface HAS 2 Options Before API 21 Camera This class is the primary API for controlling device cameras. This class is used to take pictures or videos when you are building a camera application.. SurfaceView This class is used to present a live camera preview to the user. MediaRecorder This class is used to record video from the camera. Released with API 21 Newer android.hardware.camera2

Option 1: Create Intent to call existing Camera App Existing Android Camera App takes picture and return then to your application with data stored into a file that your app can access --uses Intents and existing Camera App to minimize code you have to write just to get a picture (or video) taken. --good when you don’t need camera controls , etc. to be part of your apps interface

Option 1: Intent to existing Camera App Newer with later Android SDK version http://developer.android.com/guide/topics/media /camera.html

Opton 1: Intent to existing Camera App Steps Compose a Camera Intent - Create an Intent that requests an image or video, using one of these intent types: MediaStore.ACTION_IMAGE_CAPTURE - Intent action type for requesting an image from an existing camera application. MediaStore.ACTION_VIDEO_CAPTURE - Intent action type for requesting a video from an existing camera application. Start the Camera Intent - Use the startActivityForResult() method to execute the camera intent. After you start the intent, the (already existing) Camera application user interface appears on the device screen and the user can take a picture or video. Receive the Intent Result - Set up an onActivityResult() method in your application to receive the callback and data from the camera intent. When the user finishes taking a picture or video (or cancels the operation), the system calls this method. This is like a callback function

Option 1: with/ existing Camera App Lets look at some code Remember: you must ask for permission in the Manifest xml file!!! <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="grewe.example.Camera.Simple" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="12" /> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CameraSimpleActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

OPTION1: CameraSimple.java package grewe.example.Camera.Simple; import android.app.Activity; import android.os.Bundle; import android.provider.MediaStore; import android.content.Intent; import android.net.Uri; import java.io.File; import android.os.Environment; import android.util.Log; import java.text.SimpleDateFormat; import java.util.Date; import android.widget.Toast; public class CameraSimpleActivity extends Activity { //variables to determine Image versus video private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; private Uri fileUri; //location of where will store image or video public static final int MEDIA_TYPE_IMAGE = 1; public static final int MEDIA_TYPE_VIDEO = 2;

OPTION1: CameraSimple.java – method to create and start Intent /** Called when the activity is first created. --- CREATES INTENT AND STARTS IT */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //get the picture taken by external existing Camera App right //away at start...could do instead as a result of Event handling // create Intent to take a picture and return control to the calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Create Intent //setup File URL for location for image fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name // start the image capture Intent startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); }

OPTION1: CameraSimple.java– callback method when Camera Intent done //METHOD to Get Intents Recieved --in this case when camera app done // will put message in TextView on main apps interface @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { // say that we saved image TextView t = (TextView)findViewById(R.id.textView_Results); t.setText("Success"); } else if (resultCode == RESULT_CANCELED) { // User cancelled the image capture t.setText("Camera Cancelled"); } else { // Image capture failed, advise user t.setText("Failure"); } if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { // Video captured and saved to fileUri specified in the Intent Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); } else if (resultCode == RESULT_CANCELED) { // User cancelled the video capture } else { // Video capture failed, advise user }

OPTION1: CameraSimple.java– methods for getting File URL //METHODS to handle file storage /** Create a file Uri for saving an image or video */ private static Uri getOutputMediaFileUri(int type){ return Uri.fromFile(getOutputMediaFile(type)); } /** Create a File for saving an image or video */ private static File getOutputMediaFile(int type){ // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "MyCameraApp"); // This location works best if you want the created images to be shared // between applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ Log.d("MyCameraApp", "failed to create directory"); return null; // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE){ mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { "VID_"+ timeStamp + ".mp4"); } else { return null; } return mediaFile;

OPTION 1: Lets run it

OPTION 1: Running results

Suppose you want the image data Option 1 load the Image from the File you specified Option 2 the Intent returned from using Camera App has a “small version” of the image bitmap that was taken you can grab directly

Option 1: Loading (regular size) image from saved File private void setPic() {     // Get the dimensions of the View - NOTE: assumes the Activity has a ImageView object mImageView     int targetW = mImageView.getWidth();     int targetH = mImageView.getHeight();       // Get the dimensions of the bitmap –NOTE: assumes mCurrentPhotoPath is location of image you took     BitmapFactory.Options bmOptions = new BitmapFactory.Options();     bmOptions.inJustDecodeBounds = true;     BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);     int photoW = bmOptions.outWidth;     int photoH = bmOptions.outHeight;       // Determine how much to scale down the image     int scaleFactor = Math.min(photoW/targetW, photoH/targetH);       // Decode the image file into a Bitmap sized to fill the View     bmOptions.inJustDecodeBounds = false;     bmOptions.inSampleSize = scaleFactor;     bmOptions.inPurgeable = true;       Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);     mImageView.setImageBitmap(bitmap); }

Option 2: getting small image stored directly in Intent returned from Camera App Alter your onActivityResults method protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { //The Intent has the image –call a method passing this Intent handleSmallCameraPhoto(Intent intent); }}//****rest of method*** } //assume Activity as an ImageView object called //mImageView and an ImageBitmap object to store data called mImageBitmap private void handleSmallCameraPhoto(Intent intent) {     Bundle extras = intent.getExtras();     mImageBitmap = (Bitmap) extras.get("data");     mImageView.setImageBitmap(mImageBitmap); } Good Only for small Icon type Images

Option 2: have your application create code to control camera

OPTION 2: Have application itself create controls to take picture (don’t leave APP) An app that takes a picture…..you create the interface to take picture and show preview,etc. (does not use existing app). Requires use of API classes like Camera, SurfaceView More complicated but, you get more…appropriate for apps that need it

OPTION 2: Camera inside your app…..Classes involved Camera This class is the primary API for controlling device cameras. This class is used to take pictures or videos when you are building a camera application.. (if device or emulator has multiple cameras uses first rear facing camera can find) SurfaceView This class is used to present a live camera preview to the user. MediaRecorder This class is used to record video from the camera.

OPTION2: some devices have more than one camera Android devices can have multiple cameras, for example a back-facing camera for photography and a front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the number of cameras available on a device using the Camera.getNumberOfCameras() method.

OPTION 2: Camera inside App…The Steps Detect and Access Camera - Create code to check for the existence of cameras and request access. Create a Preview Class - Create a camera preview class that extends SurfaceView and implements the SurfaceHolder interface. This class previews the live images from the camera. Build a Preview Layout - Once you have the camera preview class, create a view layout that incorporates the preview and the user interface controls you want. Setup Listeners for Capture - Connect listeners for your interface controls to start image or video capture in response to user actions, such as pressing a button. Capture and Save Files - Setup the code for capturing pictures or videos and saving the output. Release the Camera - After using the camera, your application must properly release it for use by other applications.

OPTION 2: STEP 1--detect if Camera /** Check if this device has a camera */private boolean checkCameraHardware(Context context) { if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { // this device has a camera return true; } else { // no camera on this device return false; } } //above uses PackageManager.hasSystemFeature() method to see if camera available on device

OPTION 2: STEP 1 - access Camera /** A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } //More than 1 Camera?: On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using Camera.open(int). The example code above will access the first, back-facing camera on a device with more than one camera.

OPTION 2: STEP 1 – (optional) check camera capabilities using the Camera.getParameters() method and checking the returned Camera.Parameters object for supported capabilities. When using API Level 9 or higher, use the Camera.getCameraInfo() to determine if a camera is on the front or back of the device, and the orientation of the image.

OPTION 2: Step 2- create Preview Interface Users (usually) must be able to see what the device camera sees. SurfaceView =can display the live image data coming from a camera THE CODE next: how to create basic camera preview class that can be included in a View layout. implements SurfaceHolder.Callback in order to capture the callback events for creating and destroying the view, which are needed for assigning the camera preview input.

OPTION 2- Step 2 – Preview Interface code public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera //where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); package grewe.examples.Camera.InApp; import android.graphics.Canvas; import android.graphics.Rect; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.hardware.Camera; import android.content.*; import java.io.IOException; import android.util.Log; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera;

OPTION 2- Step 2 – Preview Interface code public void surfaceDestroyed(SurfaceHolder holder) { // empty. Take care of releasing the Camera preview in your activity. if (mCamera != null) // Call stopPreview() to stop updating the preview surface. mCamera.stopPreview(); } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // If your preview can change or rotate, take care of those events here. // Make sure to stop the preview before resizing or reformatting it. if (mHolder.getSurface() == null){ // preview surface does not exist return; // stop preview before making changes try { mCamera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview // make any resize, rotate or reformatting changes here // start preview with new settings try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ // Log.d(TAG, "Error starting camera preview: " + e.getMessage()); }

OPTION 2: Step 2 –more on orientation of App Note: A camera preview does not have to be in landscape mode. Starting in Android 2.2 (API Level 8), you can use the setDisplayOrientation() method to set the rotation of the preview image. In order to change preview orientation as the user re-orients the phone, within the surfaceChanged() method of your preview class, first stop the preview with Camera.stopPreview() change the orientation and then start the preview again with Camera.startPreview().

OPTION 2: Step 2- attaching Preview Interface to Layout in App Edit the appropriate XML layout file. Here showing the XML file---add a FrameLayout to contain the Preview. Also add a button to later trigger image capture <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:id="@+id/camera_preview" android:layout_height="fill_parent" android:layout_weight="1” /> <Button android:id="@+id/button_capture" android:text="Capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center” /> </LinearLayout>

OPTION 2: Step 3- layout of preview interface On most devices, the default orientation of the camera preview is landscape. This previous code layout specifies a horizontal (landscape) layout Change Manifest file so app is in landscape. <activity android:label="@string/app_name" android:name=".MyCameraInAppActivity" android:screenOrientation="landscape" >

OPTION 2: Step 3- add preview into Frame Layout created In the activity for your camera view, add your preview class to the FrameLayout element shown in the example above public class CameraActivity extends Activity {     private Camera mCamera;     private CameraPreview mPreview;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         // Create an instance of Camera         mCamera = getCameraInstance();         // Create our Preview view and set it as the content of our activity.         mPreview = new CameraPreview(this, mCamera);         FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);         preview.addView(mPreview);     }

OPTION 2: Step 4&5 – code to do capture ---trigger and handling set up listeners for your user interface controls to respond to a user action by taking a picture. Camera.takePicture() = takes picture, 3 parameters which receive data from the camera. To get data in a JPEG format, you must implement an Camera.PictureCallback interface to receive the image data and write it to a file.

OPTION 2: Step 4&5 – event handling when button hit –this is callback after picture taken ---store to file Add code to main Application Activity class This is private variable of this class private PictureCallback mPicture = new PictureCallback() {     @Override     public void onPictureTaken(byte[] data, Camera camera) {         File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);         if (pictureFile == null){             Log.d(TAG, "Error creating media file, check storage permissions: " +                 e.getMessage());             return;         }         try {             FileOutputStream fos = new FileOutputStream(pictureFile);             fos.write(data);             fos.close();         } catch (FileNotFoundException e) {             Log.d(TAG, "File not found: " + e.getMessage());         } catch (IOException e) {             Log.d(TAG, "Error accessing file: " + e.getMessage());         }     } };

OPTION2- Step 4&5 registering event handler to a button Trigger capturing an image by calling the Camera.takePicture() method. The following example code shows how to call this method from a button View.OnClickListener. // Add a listener to the Capture button Button captureButton = (Button) findViewById(id.button_capture);     captureButton.setOnClickListener(         new View.OnClickListener() {         @Override         public void onClick(View v) {             // get an image from the camera             mCamera.takePicture(null, null, mPicture);//see previous page code //callback is mPicture !!!!         }     } );

OPTION2 – Step 6 – Release the camera Caution: Remember to release the Camera object by calling the Camera.release() when your application is done using it! For information about how to release the camera, see Releasing the camera.

OPTION 2: Steps 1) Create simple interface with a button that will trigger taking the picture

App Interface --- button Add button

The Main Application (Activity) class import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class cameralab2 extends Activity { /** Called when the activity is first created. */ final int CAMERALAB1=1; final int CAMERALAB2=2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //loads interface stored in main.xml of layout directory setContentView(R.layout.main); Button cameraButton = (Button) findViewById(R.id.camera); //setups button to have even so that it creates an Activity //of instance photoshoot.class and start it as an Activity cameraButton.setOnClickListener( new OnClickListener(){ public void onClick(View v ){ Intent i = new Intent(cameralab2.this, photoshoot.class); startActivityForResult(i,CAMERALAB2); } }); // MORE ON OTHER SLIDES

The Main Application (Activity) class Here setting up the ImageView and setting bitmap to image associated with Intent passed to onActivityResult @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode== CAMERALAB1 && resultCode == Activity.RESULT_OK){ Log.v("Result","Result:"+data.toURI()); Bundle extras = data.getExtras(); Bitmap b = (Bitmap) extras.get("data"); ((ImageView)findViewById(R.id.picture)).setImageBitmap(b); } else if (requestCode== CAMERALAB2 && resultCode == Activity.RESULT_OK){ String imageurl = extras.getString("url"); Uri imgUri = Uri.parse(imageurl); ((ImageView)findViewById(R.id.picture)).setImageURI(imgUri);

PhotoShoot class First the imports. import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.os.Debug; import android.provider.MediaStore.Images; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.Toast; Activity and implements SurfaceHolder.Callback public class photoshoot extends Activity implements SurfaceHolder.Callback { private static final String TAG = "photoshoot"; Camera mCamera; boolean mPreviewRunning = false; byte[] tempdata; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; public void onCreate(Bundle icicle) super.onCreate(icicle); Debug.startMethodTracing("mytrace"); Log.e(TAG, "onCreate"); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.camera); ImageView iv = new ImageView(this); iv.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.icon)); this.addContentView(iv, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mSurfaceView = (SurfaceView)findViewById(R.id.surface); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); iv.setOnClickListener(new OnClickListener(){ public void onClick(View view){ mCamera.takePicture(mShutterCallback, mPictureCallback,mjpeg); } }); Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback(){ @Override public void onShutter() { // TODO Auto-generated method stub }; Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) Camera.PictureCallback mjpeg = new Camera.PictureCallback() { tempdata=data; //showDialog(LOADLOCALIMAGE); done(); Log.e(TAG, "PICTURE JPEG CALLBACK: data.length = " + data.length); void done() Bitmap bm = BitmapFactory.decodeByteArray(tempdata,0,tempdata.length); String url = Images.Media.insertImage(getContentResolver(), bm, null, null); bm.recycle(); Bundle bundle = new Bundle(); if(url!=null) bundle.putString("url", url); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); else Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show(); // Debug.stopMethodTracing(); finish(); public void surfaceCreated(SurfaceHolder holder) Log.e(TAG, "surfaceCreated"); mCamera = Camera.open(); //mCamera.startPreview(); public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) Log.e(TAG, "surfaceChanged"); try // XXX stopPreview() will crash if preview is not running if (mPreviewRunning) { mCamera.stopPreview(); Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(w, h); /* p.set("rotation", 90); p.setPictureSize(480,640); */ mCamera.setParameters(p); mCamera.setPreviewDisplay(holder); mCamera.startPreview(); mPreviewRunning = true; catch(Exception e) Log.d("",e.toString()); public void surfaceDestroyed(SurfaceHolder holder) Log.e(TAG, "surfaceDestroyed"); mPreviewRunning = false; mCamera.release(); mCamera=null;

PhotoShoot class First the imports. “Hello World Project import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.os.Debug; import android.provider.MediaStore.Images; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.Toast; public class photoshoot extends Activity implements SurfaceHolder.Callback { private static final String TAG = "photoshoot"; Camera mCamera; boolean mPreviewRunning = false; byte[] tempdata; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; public void onCreate(Bundle icicle) super.onCreate(icicle); Debug.startMethodTracing("mytrace"); Log.e(TAG, "onCreate"); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREE N, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.camera); ImageView iv = new ImageView(this); iv.setBackgroundDrawable(this.getResources().getDrawable(R.drawabl e.icon)); this.addContentView(iv, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mSurfaceView = (SurfaceView)findViewById(R.id.surface); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS ); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LAN DSCAPE); iv.setOnClickListener(new OnClickListener(){ public void onClick(View view){ mCamera.takePicture(mShutterCallback, mPictureCallback,mjpeg); } }); Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback(){ @Override public void onShutter() { // TODO Auto-generated method stub }; Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) Camera.PictureCallback mjpeg = new Camera.PictureCallback() { tempdata=data; //showDialog(LOADLOCALIMAGE); done(); Log.e(TAG, "PICTURE JPEG CALLBACK: data.length = " + data.length); void done() Bitmap bm = BitmapFactory.decodeByteArray(tempdata,0,tempdata.length); String url = Images.Media.insertImage(getContentResolver(), bm, null, null); bm.recycle(); Bundle bundle = new Bundle(); if(url!=null) bundle.putString("url", url); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); else Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show(); // Debug.stopMethodTracing(); finish(); public void surfaceCreated(SurfaceHolder holder) Log.e(TAG, "surfaceCreated"); mCamera = Camera.open(); //mCamera.startPreview(); public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) Log.e(TAG, "surfaceChanged"); try // XXX stopPreview() will crash if preview is not running if (mPreviewRunning) { mCamera.stopPreview(); Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(w, h); /* p.set("rotation", 90); p.setPictureSize(480,640); */ mCamera.setParameters(p); mCamera.setPreviewDisplay(holder); mCamera.startPreview(); mPreviewRunning = true; catch(Exception e) Log.d("",e.toString()); public void surfaceDestroyed(SurfaceHolder holder) Log.e(TAG, "surfaceDestroyed"); mPreviewRunning = false; mCamera.release(); mCamera=null; “Hello World Project import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.os.Debug; import android.provider.MediaStore.Images; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.Toast; public class photoshoot extends Activity implements SurfaceHolder.Callback { private static final String TAG = "photoshoot"; Camera mCamera; boolean mPreviewRunning = false; byte[] tempdata; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; public void onCreate(Bundle icicle) super.onCreate(icicle); Debug.startMethodTracing("mytrace"); Log.e(TAG, "onCreate"); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREE N, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.camera); ImageView iv = new ImageView(this); iv.setBackgroundDrawable(this.getResources().getDrawable(R.drawabl e.icon)); this.addContentView(iv, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mSurfaceView = (SurfaceView)findViewById(R.id.surface); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS ); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LAN DSCAPE); iv.setOnClickListener(new OnClickListener(){ public void onClick(View view){ mCamera.takePicture(mShutterCallback, mPictureCallback,mjpeg); } }); Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback(){ @Override public void onShutter() { // TODO Auto-generated method stub }; Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) Camera.PictureCallback mjpeg = new Camera.PictureCallback() { tempdata=data; //showDialog(LOADLOCALIMAGE); done(); Log.e(TAG, "PICTURE JPEG CALLBACK: data.length = " + data.length); void done() Bitmap bm = BitmapFactory.decodeByteArray(tempdata,0,tempdata.length); String url = Images.Media.insertImage(getContentResolver(), bm, null, null); bm.recycle(); Bundle bundle = new Bundle(); if(url!=null) bundle.putString("url", url); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); else Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show(); // Debug.stopMethodTracing(); finish(); public void surfaceCreated(SurfaceHolder holder) Log.e(TAG, "surfaceCreated"); mCamera = Camera.open(); //mCamera.startPreview(); public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) Log.e(TAG, "surfaceChanged"); try // XXX stopPreview() will crash if preview is not running if (mPreviewRunning) { mCamera.stopPreview(); Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(w, h); /* p.set("rotation", 90); p.setPictureSize(480,640); */ mCamera.setParameters(p); mCamera.setPreviewDisplay(holder); mCamera.startPreview(); mPreviewRunning = true; catch(Exception e) Log.d("",e.toString()); public void surfaceDestroyed(SurfaceHolder holder) Log.e(TAG, "surfaceDestroyed"); mPreviewRunning = false; mCamera.release(); mCamera=null;

Android’s Camera class android.hardware.Camera Must ask to use the camera in Manifest file Example  <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />

Must ask permission to use Android Camera or its features Examples (first one is the one we use)  <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />

Android Steps in Using Camera to take picture (some optional) Obtain an instance of Camera from open(int). Get existing (default) settings with getParameters(). If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters). If desired, call setDisplayOrientation(int). Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first. Call stopPreview() to stop updating the preview surface. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).