查看: 275|回复: 0

[手机开发] Android Camera实现毫秒级拍照实例

发表于 2017-11-10 08:39:00
太阳http代理AD

?我们知道自定义Camera需要以下几步

  1. 打开相机,即实例化Camera对象,Camera camera = Camera.open();
  2. 设置Camera的相关参数,Camera.Parameters parameters = camera.getParameters();
  3. 打开预览,camera.setPreviewDisplay(surfaceholder); camera.startPreview();
  4. 获取图片,这里只是从预览中获取因此使用,camera.setPreviewCallback(new Camera.PreviewCallback(){……..});
  5. 停止预览,释放相机,camera.stopPreview();camera.release();

1、打开相机

  1. public static Camera openFacingBackCamera() {
  2. Camera cam = null;
  3. Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
  4. for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
  5. Camera.getCameraInfo(camIdx, cameraInfo);
  6. if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  7. try {
  8. //打开相应的摄像头,目前是前置摄像头
  9. cam = Camera.open(camIdx);
  10. } catch (RuntimeException e) {
  11. e.printStackTrace();
  12. if (cam!=null){
  13. cam.stopPreview();
  14. cam.release();
  15. }
  16. cam = null;
  17. }
  18. }
  19. }
  20. return cam;
  21. }
复制代码

2、设置相关参数和预览

  1. //定义SurfaceView
  2. private SurfaceView localSurface;
  3. // 为surfaceHolder添加一个回调监听器
  4. localSurface.getHolder().addCallback(new SurfaceHolder.Callback() {
  5. @Override
  6. public void surfaceCreated(SurfaceHolder holder) {
  7. }
  8. @Override
  9. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  10. camera = CameraUtil.openFacingBackCamera();
  11. //设置相机预览旋转角度
  12. camera.setDisplayOrientation(180);
  13. if (camera != null) {
  14. Camera.Parameters parameters = camera.getParameters();// 获得相机参数
  15. List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
  16. List<Camera.Size> sizes_pic = parameters.getSupportedPictureSizes();
  17. Camera.Size cs = sizes.get(0);
  18. int mWidth = cs.width;
  19. int mHeight = cs.height;
  20. parameters.setPreviewSize(mWidth, mHeight);
  21. parameters.setPictureSize(sizes_pic.get(0).width, sizes_pic.get(0).height);
  22. parameters.setJpegQuality(85);//
  23. parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
  24. parameters.setPictureFormat(ImageFormat.JPEG);
  25. camera.setParameters(parameters);
  26. try {
  27. camera.setPreviewDisplay(localSurface.getHolder());
  28. camera.startPreview(); // 开始预览
  29. camera.autoFocus(null); // 自动对焦
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. camera.stopPreview();
  33. camera.release();
  34. camera = null;
  35. }
  36. }
  37. }
  38. @Override
  39. public void surfaceDestroyed(SurfaceHolder holder) {
  40. if (camera!=null){
  41. camera.stopPreview();
  42. camera.release();
  43. camera = null;
  44. }
  45. }
  46. } );
  47. // 设置该SurfaceView自己不维护缓冲
  48. localSurface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
复制代码

3、获取图片

  1. private void getViewImage() {
  2. //设置监听
  3. camera.setPreviewCallback(new Camera.PreviewCallback(){
  4. @Override
  5. public void onPreviewFrame(byte[] data, Camera camera) {
  6. Camera.Size size = camera.getParameters().getPreviewSize();
  7. try{
  8. YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
  9. if(image!=null){
  10. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  11. image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);
  12. Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
  13. //因为图片会放生旋转,因此要对图片进行旋转到和手机在一个方向上
  14. rotateBitmap(bmp);
  15. stream.close();
  16. }
  17. }catch(Exception ex){
  18. Log.e("Sys","Error:"+ex.getMessage());
  19. }
  20. }
  21. });
  22. }
复制代码

旋转图片的方法

  1. public void rotateMyBitmap(Bitmap bmp){
  2. Matrix matrix = new Matrix();
  3. matrix.postScale(-1, 1); // 镜像水平翻转(如果左右颠倒)
  4. matrix.postRotate(180);
  5. Bitmap nbmp = Bitmap.createBitmap(bmp, 0,0, bmp.getWidth(), bmp.getHeight(), matrix, true);
  6. imageView.setImageBitmap(nbmp);
  7. };
复制代码

自定义时间拍照

  1. private Handler mHandler = new Handler() {
  2. public void handleMessage(Message msg) {
  3. switch (msg.what) {
  4. case 101:
  5. getViewImage();
  6. Log.e(TAG, "handleMessage: 拍照" );
  7. mHandler.sendEmptyMessageDelayed(102, 100);
  8. break;
  9. case 102:
  10. camera.setPreviewCallback(null);
  11. break ;
  12. }
  13. }
  14. };
复制代码

然后点击按钮调用

  1. mHandler.sendEmptyMessageDelayed(101, 100);
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



太阳http代理AD
回复

使用道具 举报