我已经在两个android设备之间建立了wifi直接p2p连接,以720x480的分辨率将实时摄像头从设备a流式传输到设备B。它工作正常,但即使在近距离(<1米)也会有很大的波动。有时它是~15fps,然后在几秒钟内它会下降到~3fps(只是一个猜测)。基本功能是PreviewCallback的OnPreviewFrame中的一个可运行线程,该线程使用YuvImage()将预览帧压缩为JPEG并将其写入OutputStream。
我的问题是:有没有更有效的方法来做到这一点?我不需要惊人的帧速率(可能是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 + "rn" +
"Content-type: image/jpgrn" +
"Content-Length: " + buffer.size() +
"rnrn").getBytes());
buffer.writeTo(stream);
stream.write("rnrn".getBytes());
stream.flush();
}
catch (IOException e)
{
Log.d(ChooseFunction.TAG, e.getMessage());
}
}
}
});
}
}
};
我使用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);
}
});
}