android wifi直播直播相机视频流

时间:2015-02-17 19:48:18

标签: android video stream p2p wifi-direct

我在两个Android设备之间建立了一个wifi直接p2p连接,将实时摄像头从设备A流式传输到设备B,分辨率为720x480。它工作正常,但即使在近距离(<1米)也非常不稳定。有时它是〜15fps,然后几秒钟它会下降到~3fps(只是一个猜测)。基本功能是PreviewCallback的OnPreviewFrame中的Runnable线程,它使用YuvImage()将预览帧压缩为JPEG并将其写入OutputStream。

我的问题是:有更有效的方法吗?我不需要惊人的帧率(mabye 20 ......?)。它必须更加一致。

        private PreviewCallback previewCb_ = new PreviewCallback() {

    public void onPreviewFrame(byte[] data, Camera c) {

        frame = data;
        imageFormat = c.getParameters().getPreviewFormat();

        if (!socket.isClosed()) {

            mHandler.post(new Runnable() {
                public void run() {
                    if (stream != null){
                        try
                        {
                            //Log.d(ChooseFunction.TAG, "writing to stream");
                            buffer.reset();
                            synchronized(frame){
                                new YuvImage(frame, imageFormat, CameraView.IMG_WIDTH, CameraView.IMG_HEIGHT, null).compressToJpeg(area, 100, buffer);
                            }
                            buffer.flush();

                            // write the content header
                            stream.write(("--" + boundary + "\r\n" +
                                    "Content-type: image/jpg\r\n" +
                                    "Content-Length: " + buffer.size() +
                                    "\r\n\r\n").getBytes());



                            buffer.writeTo(stream);
                            stream.write("\r\n\r\n".getBytes());
                            stream.flush();
                        }
                        catch (IOException e)
                        {
                            Log.d(ChooseFunction.TAG, e.getMessage());
                        }
                    }
                }
            });
        }
    }
};

1 个答案:

答案 0 :(得分:0)

我使用 OpenCV 进行此转换,以提高将相机预览数据转换为位图的效果。

//Use openCV manager or openCV native libs directly in your project
        Log.i(TAG, "Trying to load OpenCV library");
    if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mOpenCVCallBack)) {
        Log.e(TAG, "Cannot connect to OpenCV Manager");
    }

private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                //Init mat object
                mYuv = new Mat(480 + 480 / 2, 640, CvType.CV_8UC1);
                mRgb = new Mat();
                Log.i(TAG, "OpenCV loaded successfully");
                OpenCVLoaded = true;
            }
            break;
            default: {
                super.onManagerConnected(status);
            }
            break;
        }
    }
};

if (OpenCVLoaded) {
                //put camera preview data in mat
                mYuv.put(0, 0, instantPhotoData);
                Imgproc.cvtColor(mYuv, mRgb, Imgproc.COLOR_YUV420sp2RGB, 4);

                // convert to bitmap:
                final Bitmap rawBitmap = Bitmap.createBitmap(mRgb.cols(), mRgb.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(mRgb, rawBitmap);
                previewImageView.setImageBitmap(rawBitmap);

                    }
                });
            }