我很难修复这些东西。设置是,我实现了Camera.PreviewCallback对象的onPreviewFrame方法,以获得要发送到连接设备的逐帧字节数据。
Camera.PreviewCallback previewCallback= new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
Log.e(TAG, "onPreviewFrame: BYTE SIZE: "+bytes.toString() );
framesCallback.getCurrentData(bytes);
}
};
回调是 framesCallback.getCurrentData(bytes); 。我在CameraActivity中实现了回调,这里是代码:
/**
* INITIALIZE THE REALTIME CALLBACK FOR GETTING RAW DATA OF SURFACE(IMAGE) TO BE SEND AS PAYLOAD
*/
private void initFramesToBeSendCallback() {
surfaceHolder.setFramesToBeRetrieveListener(new CustomSurfaceHolder.OnCaptureFramesToBeSendCallback() {
@Override
public void getCurrentData(byte[] bytes) {
if (isRecording) {
Log.e(TAG, "initFramesToBeSendCallback RECORDING, will send data!");
is= new ByteArrayInputStream(bytes);
sendFramesOfImages(is);
} else {
Log.e(TAG, "initFramesToBeSendCallback NOT RECORDING");
}
}
});
}
由于它更新并发送每个帧字节,我使用方法:
is= new ByteArrayInputStream(bytes);
sendFramesOfImages(is);
读取每个字节并将其保存在InputStream中。和方法 sendFramesOfImages(是); ,发送实时帧数据。这是Google Nearby Connections发送流数据的有效负载,参数是我创建的输入流:
private void sendFramesOfImages(InputStream is) {
Nearby.Connections.sendPayload(client, SERVICE_ID, Payload.fromStream(is))
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.e(TAG, "onResult: STREAM DATA SUCCESS");
}
}
});
}
单击触发发送数据的Button。它显示错误日志
02-28 04:22:53.015 12814-12898/shaveyourneck.syn.app.com.shaveyourneck W/NearbyConnections: Unable to deliver status for Payload -5402869508027937544
java.io.IOException: write failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.write(IoBridge.java:542)
at java.io.FileOutputStream.write(FileOutputStream.java:186)
at java.io.FileOutputStream.write(FileOutputStream.java:191)
at com.google.android.gms.internal.zzcom.zza(Unknown Source)
at com.google.android.gms.internal.zzcom.zza(Unknown Source)
at com.google.android.gms.internal.zzcon.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
at libcore.io.Posix.writeBytes(Native Method)
at libcore.io.Posix.write(Posix.java:223)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
at libcore.io.IoBridge.write(IoBridge.java:537)
at java.io.FileOutputStream.write(FileOutputStream.java:186)
at java.io.FileOutputStream.write(FileOutputStream.java:191)
at com.google.android.gms.internal.zzcom.zza(Unknown Source)
at com.google.android.gms.internal.zzcom.zza(Unknown Source)
at com.google.android.gms.internal.zzcon.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
我无法发送所需的数据。请帮忙吗?任何帮助将不胜感激。
答案 0 :(得分:0)
您的sendPayload调用中存在错误。它应该是sendPayload(GoogleApiClient, remoteEndpointId, Payload)
,但您将作为remoteEndpointId传递给SERVICE_ID。
过去,您正在为每个数据帧创建一个新流。我建议制作一个流来连续泵送数据。它会更有效率。
ParcelFileDescriptor[] payloadPipe = ParcelFileDescriptor.createPipe();
// Send the remote side one half of the payload pipe.
Nearby.getConnectionsClient(context).sendPayload(endpointId, Payload.fromStream(payloadPipe[0]));
// Use the other half of the payload pipe to pump data through.
surfaceHolder.setFramesToBeRetrieveListener(new CustomSurfaceHolder.OnCaptureFramesToBeSendCallback() {
@Override
public void getCurrentData(byte[] bytes) {
if (isRecording) {
payloadPipe[1].write(bytes);
}
}
});