Presentation is loading. Please wait.

Presentation is loading. Please wait.

Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.

Similar presentations


Presentation on theme: "Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface."— Presentation transcript:

1 camera

2 Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface view is something we can draw on – This will show the picture preview – Set layout height = 10dip – Set layout width = fill_parent – Set layout weight = 1 – Id = @+id/surface_camera SurfaceView changes when the phone’s orinetation changes.

3 To handle the surface and changes in the surface, we need our activity to implement SurfaceHolder.Callback. – public class MyCamera extends Activity implements SurfaceHolder.Callback { Our MyCamera class needs some objects – Camera mCamera; // to hold the camera object – private SurfaceView mSurfaceView; // to hold the surface view – private SurfaceHolder mSurfaceHolder; // to hold the interface for the surfacevieww – We’ll add some more later After super.onCreate – setContentView(R.layout.main); – mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); // get surface object – mSurfaceHolder = mSurfaceView.getHolder();// get surface holder – mSurfaceHolder.addCallback(this);// this activity is the callback – mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Implementing SurfaceHolder.CallBack requires three functions to be implemented – surfaceCreated – surfaceChanged Called when orientation is changed – surfaceDestroyed

4 public void surfaceCreated(SurfaceHolder holder) { Log.e(“My camera app”, "surfaceCreated"); mCamera = Camera.open(); }

5 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(“My camera app”, "surfaceChanged"); But mCamera.stopPreview() requires that startPreview was called mCamera.stopPreview(); mCamera.startPreview(); …. Set up preview ….

6 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(“My camera app”, "surfaceChanged"); If (previewRunning) mCamera.stopPreview(); mCamera.startPreview(); previewRunning = true; …. Set up preview …. boolean previewRunning = false;

7 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(“My camera app”, "surfaceChanged"); if (previewRunning) { mCamera.stopPreview(); } Log.d(“My camera app,“view width”+w+” height”+h); Camera.Parameters p = mCamera.getParameters(); // set the preview size p.setPreviewSize(w, h); mCamera.setParameters(p); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); mPreviewRunning = true; }

8 public void surfaceDestroyed(SurfaceHolder holder) { Log.e(TAG, "surfaceDestroyed"); mCamera.stopPreview(); mCamera.release(); previewRunning = false; }

9 run It crashes on nexus 1 Check log Note that error when setting preview size Note that preview size is odd. Check web for more info on Camera.Parameters and Camera.Parameters.setPreviewSize – see getSupportedPreviewSizes

10 Log.d(TAG,"width"+w+" h"+h); List sizes = mCamera.getParameters().getSupportedPreviewSizes(); Camera.Size sizeToSet = null; for (Camera.Size t: sizes) { String str = "view"; str += t.width; str += "by" + t.height; Log.d(TAG, str); } Log.d(TAG,"w="+sizeToSet.width+" h="+sizeToSet.height); Before setParameters, do the following if (t.width<=w && t.height<=h && found==false) { sizeToSet = t; found = true; } Boolean found = new Boolean(false); add

11 Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(sizeToSet.width, sizeToSet.height); mCamera.setParameters(p); Change parameter setting to:

12 Take picture In onCreate add button click listener – When clicked call mCamera.stopPreview (); // I think mCamera.takePicture(null, pictureCallback, null); – The first null could be set to play a sound. But we will just use the default – pictureCallback is a Camera.PictureCallback object that we must make

13 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { if (imageData != null) { Log.d("Still", "Image data received from camera"); String pathForAppFiles = getFilesDir().getAbsolutePath(); pathForAppFiles = pathForAppFiles + "/my_pic.jpeg”; Log.d("Still image filename:", pathForAppFiles); We will use Bitmap, which has lots of bitmap related functions To write to a jpeg file, we use Bitmap.compress(file format, quality, outputStream); Bitmap bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length); To create a Bitmap we use BitmapFactory. decodeByteArray(byte[] data, int offset, int length) Where data is imageData (the fucntion argument,; offset = 0; and length = imageData.length FileOutputStream fos = openFileOutput(STILL_IMAGE_FILE, MODE_WORLD_READABLE); bitmap.compress(CompressFormat.JPEG, 50, fos); os.close(); mCamera.startPreview(); Finally, start the preview again:

14 Show picture Make new activity (see previous tutorial on making another activity) In layout add – Add ImageView In onCreate String pathForAppFiles = getFilesDir().getAbsolutePath(); pathForAppFiles = pathForAppFiles + "/" + "myPic.jpeg"; Log.d("Still image source filename:", pathForAppFiles); Get file name Get bitmap from file Bitmap bm = BitmapFactory.decodeFile(pathForAppFiles); Show bitmap in ImageView ImageView imageView = (ImageView)this.findViewById(R.id.ImageView); imageView.setImageBitmap(bm);


Download ppt "Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface."

Similar presentations


Ads by Google