1 / 46

Android – Camera –an Example

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?.

cricket
Télécharger la présentation

Android – Camera –an Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  3. 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" />

  4. 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

  5. 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 • 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.

  6. 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

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

  8. 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

  9. 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-sdkandroid: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>

  10. 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;

  11. 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); }

  12. 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(intrequestCode, intresultCode, 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 TextView t = (TextView)findViewById(R.id.textView_Results); t.setText("Camera Cancelled"); } else { // Image capture failed, advise user TextView t = (TextView)findViewById(R.id.textView_Results); 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 } } }

  13. 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) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4"); } else { return null; } return mediaFile; } }

  14. OPTION 1: Lets run it

  15. OPTION 1: Running results

  16. 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

  17. 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);}

  18. Option 2: getting small image stored directly in Intent returned from Camera App • Alter your onActivityResults method protected void onActivityResult(intrequestCode, intresultCode, 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 Onlyfor smallIcon type Images

  19. Option 2: have your application create code to control camera

  20. 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

  21. 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.

  22. 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.

  23. 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.

  24. 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

  25. 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.

  26. 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.

  27. 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.

  28. 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;

  29. 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()); } } }

  30. 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().

  31. 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_width="fill_parent" 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>

  32. 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" >

  33. 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);    }

  34. 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.

  35. 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());        }    }};

  36. 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 buttonButton 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 !!!!        }    });

  37. 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.

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

  39. App Interface --- button • Add button

  40. 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

  41. 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(intrequestCode, intresultCode, 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){ Log.v("Result","Result:"+data.toURI()); Bundle extras = data.getExtras(); String imageurl = extras.getString("url"); Uri imgUri = Uri.parse(imageurl); ((ImageView)findViewById(R.id.picture)).setImageURI(imgUri); } } }

  42. PhotoShoot class • Activity and implements SurfaceHolder.Callback public class photoshoot extends Activity implements SurfaceHolder.Callback { private static final String TAG = "photoshoot"; Camera mCamera; booleanmPreviewRunning = false; byte[] tempdata; private SurfaceViewmSurfaceView; private SurfaceHoldermSurfaceHolder; 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.ShutterCallbackmShutterCallback = new Camera.ShutterCallback(){ @Override public void onShutter() { // TODO Auto-generated method stub } }; Camera.PictureCallbackmPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) { } } }; Camera.PictureCallbackmjpeg = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) { 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"); mCamera.stopPreview(); mPreviewRunning = false; mCamera.release(); mCamera=null; } } • 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;

  43. 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; 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() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) { 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"); mCamera.stopPreview(); 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; booleanmPreviewRunning = false; byte[] tempdata; private SurfaceViewmSurfaceView; private SurfaceHoldermSurfaceHolder; 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.ShutterCallbackmShutterCallback = new Camera.ShutterCallback(){ @Override public void onShutter() { // TODO Auto-generated method stub } }; Camera.PictureCallbackmPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) { } } }; Camera.PictureCallbackmjpeg = new Camera.PictureCallback() { public void onPictureTaken(byte[] data, Camera c) { if(data !=null) { 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"); mCamera.stopPreview(); mPreviewRunning = false; mCamera.release(); mCamera=null; } }

  44. 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" />

  45. 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" />

  46. 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()).

More Related