/
camera Make new project camera Make new project

camera Make new project - PowerPoint Presentation

evans
evans . @evans
Follow
66 views
Uploaded On 2023-09-21

camera Make new project - PPT Presentation

CameraFun Give permission to use camera writeexternal storage Make two buttons id t akePictureButton id s howLastPicButton Add SurfaceView to layout Surface view is something we can draw ID: 1019099

mcamera mediarecorder width camera mediarecorder mcamera camera width height file surface null orientation log surfaceholder path environment sizetoset mvideoview

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "camera Make new project" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. camera

2. Make new project CameraFunGive permission to use camerawrite_external _storageMake two buttonsid takePictureButtonid showLastPicButtonAdd SurfaceView to layoutSurface view is something we can draw onGraphics on a SurfaceView is discussed in the video GraphicWithCanvasThis will show the picture previewSet layout height = fill_parent (or try 10dip)Set layout width = fill_parentSet layout weight = 1Id = @+id/surface_cameraSurfaceView changes when the phone’s orientation changes.

3. SurfaceViewTo handle the surface and changes in the surface, we need our activity to implement SurfaceHolder.Callback.public class CameraFunActivity extends Activity implements SurfaceHolder.Callback {See Graphics with Canvas notes for how to make surfaceView a class, not the activityOur CameraFunActivity class needs some objectsCamera mCamera; // to hold the camera object//!! When importing package camera, make sure to get android.hardware.cameraprivate SurfaceView mSurfaceView; // to hold the surface viewprivate SurfaceHolder mSurfaceHolder; // to hold the interface for the surfaceviewboolean previewRunning = false;We’ll add some more laterIn onCreate, addmSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); // get surface objectmSurfaceHolder = mSurfaceView.getHolder(); // get surface holdermSurfaceHolder.addCallback(this); // this activity is the callback// note, instead this, we could make a new class that implements SurfaceHolder.CallbackmSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Implementing SurfaceHolder.CallBack requires three functions to be implementedsurfaceCreatedsurfaceChangedCalled when orientation is changed and after surfaceCreatedsurfaceDestroyed

4. surfaceCreated and surfaceDestroyedpublic void surfaceCreated(SurfaceHolder holder) {Log.e("CameraFun", "surfaceCreated");mCamera = Camera.open(); // this will not compile if the wrong camera package was imported (see previous slide)}public void surfaceDestroyed(SurfaceHolder holder) { Log.e("CamerFun", "surfaceDestroyed"); mCamera.stopPreview(); mCamera.release(); previewRunning = false;}

5. surfaceChangedpublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {// called when surface created and when orientation is changedLog.d("CameraFun", "surfaceChanged");if (previewRunning) mCamera.stopPreview();Log.e("CameraFun","view width"+width+" height"+height);Camera.Parameters p = mCamera.getParameters(); // set the preview sizep.setPreviewSize(width, height);mCamera.setParameters(p);try {mCamera.setPreviewDisplay(holder);} catch (IOException e) {e.printStackTrace();}mCamera.startPreview();mPreviewRunning = true;}

6. runIt crashes on most devicesCheck logNote that error when setting preview sizeNote that preview size is odd.Check web for more info on Camera.Parameters and Camera.Parameters.setPreviewSizesee getSupportedPreviewSizes

7. Improved surfaceChangedIn surfaceChanged, before setParameters, addLog.e("MyCameraFun","width"+width+" h"+height);List<Camera.Size> sizes = mCamera.getParameters().getSupportedPreviewSizes();Camera.Size sizeToSet = null;for (Camera.Size t: sizes){String str = "view";str += t.width;str += "by" + t.height;Log.e("CameraFun", str);if (t.width<= width && t.height<= height && sizeToSet == null) sizeToSet = t;}Log.e("CameraFun","w="+sizeToSet.width+" h="+sizeToSet.height);changep.setPreviewSize(width, height);top.setPreviewSize(sizeToSet.width, sizeToSet.height);// note that this sizetoSet might be null. In which case none of the supported sizes works. This condition should be handledrun

8. Rotation/orientationNote that the orientation is messed upPerhaps the image is correct in only one orientation.Two fixesFix the orientation to be in portrait or landscape and set display orientation accordinglyIn onCreate, addsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);In surfaceChanged, addIn version 2.2 and above:mCamera.setDisplayOrientation(180); // or 90 or whatever value worksIn 2.1Camera.Parameters parameters = mCamera.getParameters();parameters.set("orientation", "portrait");mCamera.setParameters(parameters);Get the rotation and then set camera orientation accordinglyIn surfaceChanged, addDisplay display = getWindowManager().getDefaultDisplay();switch (display.getRotation()) {case Surface.ROTATION_0:Log.e("DEBUG INFO","rotation = 0");mCamera.setDisplayOrientation(180); // whatever value worksbreak;case Surface.ROTATION_90:Etc. etc.

9. Take pictureIn onCreate add button click listenerWhen clicked callmCamera.takePicture(null, null, pictureCallback);The first null could be set to play a sound. But we will just use the defaultpictureCallback is a Camera.PictureCallback object that we must make

10. PictureCallbackCamera.PictureCallback pictureCallback = new Camera.PictureCallback() {public void onPictureTaken(byte[] imageData, Camera c) {if (imageData == null) {Log.e("DEBUG INFO","image is null");} else {File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File fileToSave = new File(path,"myPic.jpeg");Log.e("CameraFun","saving pic to: "+fileToSave);Bitmap bitmap = BitmapFactory.decodeByteArray(imageData,0,imageData.length);FileOutputStream fos = new FileOutputStream(fileToSave);bitmap.compress(CompressFormat.JPEG, 50, fos); // quality is 50, but could be between 0 and 100fos.close(); }}mCamera.startPreview();};Notes: there are many bitmap functions. Check documentationNeed to something to handle throwRunGet pic from storage and view on your laptop

11. Show pictureMake new activity (see previous tutorial on making another activity)In layout addAdd ImageViewIn onCreate, add// get file with pictureFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); File fileToView = new File(path,"myPic.jpeg");Log.e("Still image source filename:", fileToView.getPath());// load file as bitmapBitmap bm = BitmapFactory.decodeFile(fileToView.getPath()); // show bitmapandroid.widget.ImageView imageView = (android.widget.ImageView)findViewById(R.id.imageView);imageView.setImageBitmap(bm);

12. Record videoAdd android.permission.RECORD_AUDIOAdd member variable: MediaRecorder mediaRecorder;Add button for stopping the recordWe’ll use the button for taking a picture to start recordingIn the start recording button, replace mCamera.takePicture(null, null, pictureCallback);WithFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); File fileToSave = new File(path,"myMov.mpeg");mCamera.unlock();mediaRecorder = new MediaRecorder();mediaRecorder.setCamera(mCamera);mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);mediaRecorder.setMaxDuration(-1);mediaRecorder.setOutputFile(fileToSave.getPath()); // your filemediaRecorder.setVideoFrameRate(10);//videoFramesPerSecond);mediaRecorder.setVideoSize(320, 240); // or some other sizemediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());mediaRecorder.setMaxFileSize(100000000); // size in bytesmediaRecorder.prepare();mediaRecorder.start();

13. In stop video buttonmediaRecorder.stop();mCamera.lock();

14. Playing Videoin picviewer.xml (layout), delete image viewAdd VideoViewin ImageViewComment out everything with showing the pictureAddFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); File fileToLoad = new File(path,"myMov.mpeg");VideoView mVideoView = (VideoView) findViewById(R.id.videoView);mVideoView.setVideoPath(fileToLoad.getPath()); mVideoView.setMediaController(new MediaController(this)); mVideoView.requestFocus(); mVideoView.start(); //mVideoView.stop() //mVideoView.suspend();