51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Android UVCCamera 源码分析(四)

经过前几章的学习,我们大概了解了整个UVCCamera初始化、开始预览的过程。那么接着我们将来看看UVCCamera是如何实现拍照功能的。本章内容相对比较简单,均是Java层的实现。我们直接来看代码:

|-----------------|---------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 | @Override public void captureStill(final String path,OnCaptureListener listener) { super.captureStill(path,listener); } |

UVCCameraHandler提供了简单易用的拍照方法------captureStill,继而调用了它的基类的方法:

|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 | public void captureStill(final String path, AbstractUVCCameraHandler.OnCaptureListener listener) { AbstractUVCCameraHandler.mCaptureListener = listener; checkReleased(); sendMessage(obtainMessage(MSG_CAPTURE_STILL, path)); isCaptureStill = true; } |

然后我们再跟到已经比较熟悉的消息处理方法中

|------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 | @Override public void handleMessage(final Message msg) { final CameraThread thread = mWeakThread.get(); if (thread == null) return; switch (msg.what) { ... case MSG_CAPTURE_STILL: thread.handleStillPicture((String) msg.obj); break; ... } } |

|-----------------|------------------------------------------------------------------------------------| | 1 2 3 4 | public void handleStillPicture(String picPath) { this.picPath = picPath; } |

AbstractUVCCameraHandler.CameraThreadhandleStillPicture方法很简单,仅仅是把传进来的文件路径赋值给了一个成员变量。这里可能会有同学纳闷,调用来调用去最后就给一个path赋了个值,这是怎么做到把图像存到这个路径指向的文件中的。

其实道理很简单,我们之前文章中分析过开启预览的过程,在startPreview的时候会设置一个callback用来获取每一帧的数据,那么拍照其实也就是从中截取一帧而已:

|---------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private final IFrameCallback mIFrameCallback = new IFrameCallback() { @Override public void onFrame(final ByteBuffer frame) { int len = frame.capacity(); final byte[] yuv = new byte[len]; frame.get(yuv); // nv21 yuv data callback if (mPreviewListener != null) { mPreviewListener.onPreviewResult(yuv); } // 捕获图片 if (isCaptureStill && !TextUtils.isEmpty(picPath)) { isCaptureStill = false; new Thread(new Runnable() { @Override public void run() { saveYuv2Jpeg(picPath, yuv); } }).start(); isCaptureStill = false; } ... } }; |

可以看到在onFrame回调里会判断是否有设置picPath,如果设置则将从相机获取的yuv数据转成JPEG保存到文件中:

|---------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | private void saveYuv2Jpeg(String path, byte[] data) { YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); boolean result = yuvImage.compressToJpeg(new Rect(0, 0, mWidth, mHeight), 100, bos); if (result) { byte[] buffer = bos.toByteArray(); Bitmap bmp = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); File file = new File(path); FileOutputStream fos = null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); try { fos.flush(); fos.close(); // bmp.recycle(); if (mCaptureListener != null) { mCaptureListener.onCaptureResult(bmp, path); } } catch (IOException e) { e.printStackTrace(); } } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } |

到这里整个拍照流程就走完了,主体逻辑还是比较清晰的,但这仅仅是最基础的拍照功能。此外还有诸如获取相机所支持的图片尺寸、自动对焦等逻辑UVCCamera也都是支持的,我们将在后续文章里慢慢分析。

相关内容 {#相关内容}

参考链接:https://www.jianshu.com/p/e7e370011775


赞(1)
未经允许不得转载:工具盒子 » Android UVCCamera 源码分析(四)