Android静默拍照 Android实现静默拍照功能
骑驴望星空 人气:01.申请权限(6.0以后要动态申请)
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.创建相机工具类 CameraPreview:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraPreview(Context context, Camera camera) { super(context); //初始化Camera对象 mCamera = camera; //得到SurfaceHolder对象 mHolder = getHolder(); //添加回调,得到Surface的三个声明周期方法 mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder holder) { try { //设置预览方向 mCamera.setDisplayOrientation(90); //把这个预览效果展示在SurfaceView上面 mCamera.setPreviewDisplay(holder); //开启预览效果 mCamera.startPreview(); } catch (IOException e) { // Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (holder.getSurface() == null) { return; } //停止预览效果 mCamera.stopPreview(); //重新设置预览效果 try { mCamera.setPreviewDisplay(mHolder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }
3.创建xml文件 (要想用户无感拍照,可把视图宽高调为0.1dp或者隐藏在别的页面之下):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/camera_frame" android:layout_width="match_parent" android:layout_height="500dp"> </FrameLayout> </LinearLayout>
4.主类实现静默拍照:
public class CameraActivity extends BaseActivity{ private FrameLayout cameraFrame; private Camera mCamera; private TextView cameraTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); cameraFrame = (FrameLayout) findViewById(R.id.camera_frame); cameraTv = (TextView) findViewById(R.id.camera_tv); int numberOfCameras = Camera.getNumberOfCameras();// 获取摄像头个数 //遍历摄像头信息 for (int cameraId = 0; cameraId < numberOfCameras; cameraId++) { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//前置摄像头 mCamera = Camera.open(cameraId);//打开摄像头 } } CameraPreview mPreview = new CameraPreview(this, mCamera); cameraFrame.addView(mPreview); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); // 设置1秒后自动拍照,可调节 //得到照相机的参数 Camera.Parameters parameters = mCamera.getParameters(); //图片的格式 parameters.setPictureFormat(ImageFormat.JPEG); //预览的大小是多少 parameters.setPreviewSize(800, 400); //设置对焦模式,自动对焦 parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); //对焦成功后,自动拍照 mCamera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { //获取照片 mCamera.takePicture(null, null, mPictureCallback); } } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }); } @Override protected void onStart() { super.onStart(); } //获取照片中的接口回调 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { FileOutputStream fos = null; String mFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tt005.png"; //文件 File tempFile = new File(mFilePath); try { // fos = new FileOutputStream(tempFile); fos.write(data); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //实现连续拍多张的效果 // mCamera.startPreview(); // if (fos != null) { // try { // fos.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } } } }; }
加载全部内容