照相机

来源:互联网 发布:ios免费漫画软件推荐 编辑:程序博客网 时间:2024/03/28 20:08

1.      必须有预览照片

2.      三个权限


布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <Button        android:onClick="click"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="拍照" />     <FrameLayout         android:id="@+id/camera_preview"         android:layout_width="100dip"         android:layout_height="100dip"         />    <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/iv"        /></LinearLayout>




MainActivity:

public class MainActivity extends Activity {private ImageView iv;private Camera mCamera;private CameraPreview mPreview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv); // 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(R.id.camera_preview);        preview.addView(mPreview);}public void click(View view){mCamera.autoFocus(new AutoFocusCallback() {@Overridepublic void onAutoFocus(boolean success, Camera camera) {mCamera.takePicture(null, null, new PictureCallback() {//第一个参数播放的声音@Overridepublic void onPictureTaken(byte[] data, Camera camera) {try {File file = new File(Environment.getExternalStorageDirectory(),SystemClock.uptimeMillis()+".jpg");FileOutputStream fos = new FileOutputStream(file);fos.write(data);fos.close();Toast.makeText(getApplicationContext(), "成功", 0).show();mCamera.startPreview();} catch (Exception e) {e.printStackTrace();}}});}});        }/** 获取一个照相机实例 */public static Camera getCameraInstance(){    Camera c = null;    try {        c = Camera.open(); // attempt to get a Camera instance   参数为0,1 前后置    }    catch (Exception e){        // Camera is not available (in use or does not exist)    }    return c; // returns null if camera is unavailable}@Overrideprotected void onDestroy() {mCamera.stopPreview();mCamera.release();mCamera = null;super.onDestroy();}}




预览:基本固定代码

/** A basic Camera preview class */public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {    private static final String TAG = "CameraPreview";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);        // 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());        }    }    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());        }    }}



0 0