SurfaceView+Camera+OpenCv自定义相机保存图片

至于如何在项目中如何添加OpenCv的SDK在此不再赘述:我引入的OpenCv-SDK版本为OpenCV-3.1.0-android-sdk

在Activity中实现CameraBridgeViewBase.CvCameraViewListener2接口并重写其方法,在

Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)方法中可以使用OpenCv的保存图片的方法,如下:
Imgcodecs.imwrite("/storage/emulated/0/" + "OpenCvimwrite.jpg", inputFrame.rgba());
方法参数为:1.图片名称(包括路径)2.Mat类型的数据
注:此方法保存的图片分辨率和预览分辨率相同(即Camera的PreviewSize属性)
另外此图片可能比较模糊,通过查看 JavaCameraView类中的Boolean initializeCamera(int width,int height)方法发现有以下两行代码:
   
[html]  view plain  copy
  1. int size = mFrameWidth * mFrameHeight;  
  2. size  = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;  
将size改为
 
 
[html]  view plain  copy
  1. size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat());  
照片的效果会稍微好一点。
下面切入主题,添加保存照片的方法:
在JavaCameraView类中添加方法:
[html]  view plain  copy
  1. public void tackPhoto(final String name) {  
  2.         if (mCamera != null) {  
  3.             mCamera.takePicture(null, null, new Camera.PictureCallback() {  
  4.                 @Override  
  5.                 public void onPictureTaken(byte[] data, Camera camera) {  
  6.                     Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);  
  7.                     saveMyBitmap(bmp, name);  
  8.                 }  
  9.             });  
  10.         }  
[html]  view plain  copy
  1.  public void saveMyBitmap(Bitmap mBitmap, String photoName) {  
  2.         if (isMediaMounted()) {  
  3.             String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath().toString();  
  4.             createFileDir(SDCardPath + File.separator + "OpenCv");  
  5.             String FilePath = SDCardPath + File.separator + "OpenCv" + File.separator + photoName + ".jpg";  
  6.             File file = new File(FilePath);  
  7. //        "/storage/emulated/0/OpenCv" + "OpenCv.jpg",  
  8.             BufferedOutputStream bos = null;  
  9.             try {  
  10.                 FileOutputStream fos = new FileOutputStream(file);  
  11.                 bos = new BufferedOutputStream(fos);  
  12.                 mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);  
  13.                 bos.flush();  
  14.             } catch (Exception e) {  
  15.                 e.printStackTrace();  
  16.             } finally {  
  17.                 try {  
  18.                     if (mBitmap != null) {  
  19.                         mBitmap.recycle();  
  20.                     }  
  21.                     bos.close();  
  22.                 } catch (IOException e) {  
  23.                     e.printStackTrace();  
  24.                 }  
  25.             }  
  26.         } else {  
  27.             Log.e(TAG, "saveMyBitmap: 没有挂载内存卡,无法保存图片");  
  28.         }  
  29.     }  

[html]  view plain  copy
  1. /**  
  2.     * SD卡是否挂载  
  3.     *  
  4.     * @return  
  5.     */  
  6.    public static boolean isMediaMounted() {  
  7.        String status = Environment.getExternalStorageState();  
  8.        if (status.equals(Environment.MEDIA_MOUNTED)) {  
  9.            return true;  
  10.        } else {  
  11.            return false;  
  12.        }  
  13.    }  

这样在Activity中就可以调用 JavaCameraView的 tackPhoto()方法拍照了。

猜你喜欢

转载自blog.csdn.net/u010112268/article/details/80454258