zoukankan      html  css  js  c++  java
  • 8.Media and Camera/Media Camera

        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.autofocus" />
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="mirror.android.cameratest.CameraTest" >
    
        <!-- 4.Palcing preview in a layout -->
        <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:gravity="center"/>
    
    </LinearLayout>
    package mirror.android.cameratest;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.hardware.Camera.CameraInfo;
    import android.hardware.Camera.Parameters;
    import android.hardware.Camera.PictureCallback;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.FrameLayout;
    
    public class CameraTest extends Activity {
    
        private Camera camera;
        private CameraPreview mPreview;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_camera_test);
            
            if(checkCameraHardware(getApplication())){
                camera = getCameraInstance();
                Parameters p = camera.getParameters();
                //Log.d("Camera--->getParameters",p.toString());//--->android.hardware.Camera$Parameters@41757e0
                
                // get camera information of the device
                int cameraCount = Camera.getNumberOfCameras();
                CameraInfo cameraInfo = new CameraInfo();
                for(int i = 0; i < cameraCount ; i++){
                    Camera.getCameraInfo(i, cameraInfo);
                    //get information from cameraInfo object
                    //such as cameraInfo.orientation
                }
                
                //Create our Preview view and set it as the content of our activity.
                mPreview = new CameraPreview(getApplication(), camera);
                FrameLayout preview = (FrameLayout)findViewById(R.id.camera_preview);
                preview.addView(mPreview);
            }
            
            Button captureButton = (Button)findViewById(R.id.button_capture);
            captureButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    camera.takePicture(null, null, mPicture);
                }
            });
        }
        
        
        //1.check if the 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;
        }
        
        //2.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)
                e.printStackTrace();
            }
            return c; // returns null if camera is unavailable
        }
        
        //5. implements a PictureCallback to save image
        private PictureCallback mPicture = new PictureCallback() {
            @Override
            public void onPictureTaken(final byte[] data, Camera camera) {
                
                camera.stopPreview();
                new Thread(
                    new Runnable(){
                        @Override
                        public void run() {
                            File pictureFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                            try{
                                FileOutputStream fos = new FileOutputStream(pictureFile);
                                fos.write(data);
                                fos.close();
                            }catch (FileNotFoundException e) {
                                Log.d("Camera Error","File not found: " + e.getMessage());
                            }catch (IOException e) {
                                Log.d("Camera Error","Error accessing file: " + e.getMessage());
                            }
                        }
                    }
                ).start();
                camera.startPreview();
            }
        };
    
        //6. realese the camera obkect
        @Override
        protected void onDestroy() {
            if(camera != null){
                camera.stopPreview();
                camera.release();
                camera = null;
            }
            super.onDestroy();
        }
    }
    package mirror.android.cameratest;
    
    import java.io.IOException;
    
    import android.content.Context;
    import android.hardware.Camera;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    //3. Creating a preview class
    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
    
        private SurfaceHolder mHolder;
        private Camera mCamera;
        
        public CameraPreview(Context context, Camera camera) {
            super(context);
            
            mCamera = camera;
            mHolder = getHolder();
            
            //Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed.
            mHolder.addCallback(this);
        }
    
        @Override
        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("Camera Error", "Error setting camera preview: " + e.getMessage());
            }
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // 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
            mCamera.stopPreview();
            
            // set preview size and make any resize, rotate or
            // reformatting changes here
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
            } catch (IOException e) {
                Log.d("Camera Error", "Error setting camera preview: " + e.getMessage());
            }
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // empty. Take care of releasing the Camera preview in your activity.
        }
    }

    1. Manifest Declarations

      <uses-permission android:name="android.permission.CAMERA" />      permission to use a device camera

      <uses-feature android:name="android.hardware.camera" />      declare use of camera features

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  saves images to the external storage (SD Card),

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />        tags images with GPS location information

    2. Detecting camera hardware

      Check for the existence of cameras and request access.

    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;
        }
    }

      get an instance of Camera by open() method

    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
    }

     

    3. Checking camera features

      get further information about its capabilities using the getParameters() method and checking the returned Parameters object for supported

        capabilities.

      use getcameraInfo() to determine if a camera is on the front or back of the device, and the orientation of the image.

    4. Creating a preview class

      A camera preview class is a SurfaceView that can display the live image data coming from a camera

      implements SurfaceHolder.Calback to captrue the callback events for creating and destroying the view

    /** A basic Camera preview class */
    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;
    
        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);
    
        }
    
        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());
            }
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            // empty. Take care of releasing the Camera preview in your activity.
        }
    
        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
            }
    
            // set preview size and 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());
            }
        }
    }

    5. Placing preview in a layput

    <?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>
    activity android:name=".CameraActivity"
              android:label="@string/app_name"
    
              android:screenOrientation="landscape">
              <!-- configure this activity to use landscape orientation -->
    
              <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    6. Capturing pictures

      In order to retrieve a picture, use the takePicture() method.

      This method takes three parameters which receive data from the camera.

        <1>

        <2>

        <3>

      In order to receive data in a JPEG format, you must implement an PictureCallback interface to receive the image data and write it to a file.

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

    7. Releasing the camera

      release the camera object when your application stops using it, and as soon as your application is paused

    protected void onPause() {
        super.onPause();
        if (mCamera != null){
            mCamera.release();  // release the camera for other applications
            mCamera = null;
        }
    }

    8. Saving Media Files

      pictures and videos should be saved to a device's external storage directory (SD Card) to conserve system space and to allow users

        to access these files without their device

      <1>Enviroment.getExternalStoragePublicDirectory(Evironment.DIRECTORY_PICTURE)

         This method returns the standard, shared and recommended location for saving pictures and videos.

         This directory is shared (public), so other applications can easily discover, read, change and delete files saved in this location.

         If your application is uninstalled by the user, media files saved to this location will not be removed.

      <2>Context.getExternalFileDir(Environment.DIRECTORY_PICTURE)

         standard location for saving pictures and videos which are associated with your application

         If your application is uninstalled, any files saved in this location are removed.

    9. Camera Features

      9.1 Checking feature availabity

        Check the availabilty of camera features by getting an instance of a camera’s parameters object, and checking the relevant methods. 

    // get Camera parameters
    Camera.Parameters params = mCamera.getParameters();
    
    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      // Autofocus mode is supported
    }

      9.2 Using camera features

    // get Camera parameters
    Camera.Parameters params = mCamera.getParameters();
    // set the focus mode
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    // set Camera parameters
    mCamera.setParameters(params);

        9.2.1 Metering and focus areas

    http://my.oschina.net/madmatrix/blog/204333

          

  • 相关阅读:
    King's Quest
    JavaScript“并非”一切皆对象
    javascript中的style只能取到在HTML中定义的css属性
    jquery中的$(this)和this
    WEB安全字体(Web Safe Fonts)-网页设计用什么字体兼容性好?
    css各种水平垂直居中
    css绘制各种形状
    css3椭圆运动
    通过时间戳控制类
    js中的面向对象程序设计
  • 原文地址:https://www.cnblogs.com/iMirror/p/4102460.html
Copyright © 2011-2022 走看看