使用Android的Camera2 API进行视频直播

时间:2015-10-05 11:37:22

标签: android broadcast camera2

我试图创建一个通过互联网播放视频的应用,目前我正在使用已弃用的Camera API,将Camera.PreviewCallback添加到Camera对象,然后发送onPreviewFrame()中的字节数组来自Camera.PreviewCallback的方法。

但现在我想测试新的Camera2 API,我正在Camera2BasicTutorial观看,我认为我需要制作一个CameraCaptureSession.CaptureCallback对象来获取图像字节数组,就像教程所说的那样:

CameraCaptureSession.CaptureCallback CaptureCallback
                = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                unlockFocus();


            }
        };

然后将其添加到CameraCaptureSession:

mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);

问题在于我不知道如何从CaptureCallback中的onCaptureCompleted()中的任何参数检索每个图像字节数组。

任何帮助?

2 个答案:

答案 0 :(得分:0)

你是对的 - 你无法从onCaptureCompleted()方法获取图像数据。该回调仅返回有关您自己的簿记曝光的元数据。实际图片信息会发送到您在曝光的Surface中指明的任何CaptureRequest

答案 1 :(得分:0)

至少我意识到如何做我想做的事情,从Camera2BasicTutorial,我对Camera2BasicFragment类进行了以下更改:

  1. 修改captureStillPicture()方法以删除我确定需要满足广播需求的内容,也不允许此方法停止重复模式:

    private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
    
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());
    
        CameraCaptureSession.CaptureCallback CaptureCallback
                = new CameraCaptureSession.CaptureCallback() {
    
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
            }
        };
    
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    }
    
  2. 在createCameraPreviewSession()方法中,禁用automaticall flash:

    // When the session is ready, we start displaying the preview.
                        mCaptureSession = cameraCaptureSession;
                        try {
                            // Auto focus should be continuous for camera preview.
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                            // Flash is automatically enabled when necessary.
    // mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
    // CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
    
                            // Finally, we start displaying the camera preview.
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
    
  3. 我创建了一个boolean var来检测当前是否有正在处理的图像,以便不对摄像头捕获的所有帧进行排队;和另一个布尔值来跟踪是否有通过互联网发送的帧:

    private boolean mWorking = false;
    private boolean mNetworkWorking = false;
    
  4. 修改CaptureCallback对象以在每个帧中运行captureStillPicture()方法(仅当目前没有帧处理时)。

                case STATE_PREVIEW: {
    
                if (!mWorking){
                    Log.d(TAG, "capturing..");
                    mWorking = true;
    
                    mBackgroundHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            captureStillPicture();
                        }
                    });
    
                } else {
                    Log.d(TAG, "thread working, doing nothing");
                }
    
    
                break;
    
  5. 最后,阅读框架并发送;我实现了修改OnImageAvailableListener对象:

    private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
        = new ImageReader.OnImageAvailableListener() {
    
    @Override
    public void onImageAvailable(final ImageReader reader) {
    
        // Process the image.
        Image image = reader.acquireNextImage();
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        final byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        image.close();
    
        if (!mNetworkWorking){
            Thread thread = new Thread(){
                @Override
                public void run(){
    
                    mNetworkWorking = true;
    
                    HttpResponse response = null;
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(mBroadcastUrl);
                    post.setEntity(new ByteArrayEntity(bytes));
    
                    try {
                        response = client.execute(post);
                    } catch (ClientProtocolException e) {
                        if (BuildConfig.LOCAL_LOG)
                            Log.w(TAG, "ClientProtocolException: "+e.getMessage());
                    } catch (IOException e) {
                        if (BuildConfig.LOCAL_LOG)
                            Log.w(TAG, "IOException: "+e.getMessage());
                    }
    
                    mNetworkWorking = false;
    
                }
            };
    
            thread.setName("networkThread");
            thread.setPriority(Thread.MAX_PRIORITY);
            thread.start();
        }
    
        mWorking = false;
    
    }
    
    };
    
  6. 这就是全部。