Using android camera

Solo disponible en BuenasTareas
  • Páginas : 7 (1745 palabras )
  • Descarga(s) : 0
  • Publicado : 16 de enero de 2011
Leer documento completo
Vista previa del texto
Utilizing the Camera with Android As with most other functionality on Android devices, there are multiple ways to utilize the camera. Capturing a picture with an Intent The easiest way but perhaps least flexible is to create an Intent to launch the built-in camera Activity and return the resulting picture to your Activity. Unfortunately, this isn't without it's problems. (For a variety of reasons,Android doesn't return a full size image using this method.) The version below may only work correctly on 2.0 devices and higher. For earlier devices, Android may not be able to write the image to a file based on an Intent.
/* * Make sure your emulator has an "SD Card" size specified or just test on a device */ package com.mobvcasting.pictureintent; import;; import; import import import import import import import import import import import import; android.content.Intent;;;; android.os.Bundle; android.os.Environment; android.util.Log; android.view.View; android.view.View.OnClickListener; android.widget.Button;android.widget.ImageView;

public class PictureIntent extends Activity { Button aButton; String imageFilePath; ImageView ourImageView; public final int CAMERA_RESULT = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageFilePath =Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp_image.jpg"; Log.v("IMAGE FILE",imageFilePath); aButton = (Button) this.findViewById(; aButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { File imageFile = new File(imageFilePath); Uri imageFileUri = Uri.fromFile(imageFile);

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); startActivityForResult(i, CAMERA_RESULT); }}); ourImageView = (ImageView) this.findViewById(; } protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); //Bundle extras = intent.getExtras(); switch(requestCode) { case CAMERA_RESULT:

if (resultCode == RESULT_OK) {Log.v("RESULTS","HERE"); /* Tiny Image Returned Bitmap bmp = (Bitmap) extras.get("data"); ourImageView.setImageBitmap(bmp); */ Bitmap bmp = BitmapFactory.decodeFile(imageFilePath); ourImageView.setImageBitmap(bmp); Log.v("RESULTS","Image Width: " + bmp.getWidth()); Log.v("RESULTS","Image Height: " + bmp.getHeight()); } break; } } }

This requires that we request some permissions in ourAndroidManifest.xml file:

Capturing a picture Another way to capture an image with Android is to create our own camera application. This is more difficult but should enable the grabbing of a full size image. It also allows for complex interactions with the camera preview image. The first issue we run into is that we need to create a SurfaceView for the camera to draw the preview image onto. This previewimage will act as our view finder so that the user can see what they are about to take a picture of. Here is a barebones SurfaceView that we can get started with:
package com.mobvcasting.snapshot; import import import import android.content.Context; android.util.AttributeSet; android.view.SurfaceHolder; android.view.SurfaceView;

public class CameraView extends SurfaceView implementsSurfaceHolder.Callback { SurfaceHolder mHolder; public CameraView(Context context, AttributeSet attrs) { super(context,attrs); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public CameraView(Context context) { super(context); //...