我正在制作一个使用Camera2 API的应用程序(目标API级别高于23)。它使用异步任务,其中套接字在不断运行,并获取命令以从python服务器拍摄图像。
拍摄2到4张图像(取决于设备)并通过套接字将这些图像发送到服务器后,Android应用程序进入空闲状态,并出现此日志错误E/RequestThread-0: Hit timeout for jpeg callback!
我还附上了代码段和错误日志,以获取更多信息。
我在堆栈溢出和github问题上看到了一些答案,但是没有解决。
public void readyCamera() {
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
try {
String pickedCamera = getcamera(manager);
if (activityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.openCamera(pickedCamera,cameraStateCallback,null);
imageReader = ImageReader.newInstance(1920,1088,ImageFormat.JPEG,3 /* images buffered */);
imageReader.setOnImageAvailableListener(onImageAvailableListener,null);
Log.d(TAG,"imageReader created");
} catch (CameraaccessException e) {
Log.e(TAG,e.getMessage());
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
}
private void processImage(Image image) {
final ByteBuffer buffer;
final byte[] bytes;
boolean success = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSSSSS",Locale.getDefault());
String currentDateandTime = (sdf.format(new Date()) + "_count-" +
QopiusApp.shootCommandCounter);
// QopiusApp.connection_message = "shooting starts " +
currentDateandTime;
final File file = new File(Environment.getExternalStorageDirectory() +
"/Pictures/image" + currentDateandTime + ".jpg");
// File file = new File(Environment.getExternalStorageDirectory() +
"/Pictures/image.jpg");
FileOutputStream output = null;
if (image.getFormat() == ImageFormat.JPEG) {
buffer = image.getPlanes()[0].getBuffer();
bytes = new byte[image.getPlanes()[0].getBuffer().remaining()]; //
makes byte array large enough to hold image
buffer.get(bytes); // copies image from buffer to byte array
try {
output = new FileOutputStream(file);
output.write(bytes); // write the byte array to file
success = true;
// bytes
if (QopiusApp.command.equalsIgnoreCase("shoot")) {
new AppExecutors().diskIO().execute(new Runnable() {
// Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// new Thread(new SendImage(bytes)).start();
if (socket != null) {
BufferedInputStream reader = null;
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(
socket.getOutputStream() );
reader = new BufferedInputStream( new
FileInputStream( file ) );
// byte[] buffer = new byte[ 409600 ];
int bytesRead;
while ( (bytesRead = reader.read(bytes))
!= -1 ) {
out.write( bytes,bytesRead );
socket.getOutputStream().write("EndBytes".getBytes());
}
//
socket.getOutputStream().write("EndBytes".getBytes());
reader.close();
out.flush();
// out.close();
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
i.printStackTrace();
} finally {
try {
out.flush();
// out.close();
reader.close();
//socket.close();
} catch (IOException i) {
System.out.println(i);
}
}
}
}
});
// thread.start();
// new Thread(new SendImage(bytes)).start();
QopiusApp.shootCommandCounter++;
// output = new FileOutputStream(file);
// output.write(bytes); // write the byte array to
file
// success = true;
// QopiusApp.connection_message = "shooting starts " +
currentDateandTime;
Log.e("MyPhotoCounter","shoot " + QopiusApp.shootCommandCounter + " end");
} else if (QopiusApp.command.equalsIgnoreCase("close")) {
QopiusApp.shootCommandCounter = 0;
Toast.makeText(this,"Camera stopped!!!!",Toast.LENGTH_LONG).show();
Log.e("MyPhotoCounter","Close command received " + QopiusApp.shootCommandCounter + " ending");
stopCamera();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
image.close(); // close this to free up buffer for otherimages
Log.e("Image","Image capture sucessfull111111111111 ---------
--------->");
}
}
}
我希望相机拍摄的图像数量与从服务器获取的图像数量相同(例如10张图像),然后将这些图像发送到服务器。
但是我的应用程序在拍摄2到4张图像后进入了空闲状态。
对于三分之二的图像,效果很好。
2019-11-05 11:38:55.355 14944-15051/com.qopius.camera_v2 E/Response:
SocketResponse{command='shoot',shootCount=3}
2019-11-05 11:38:55.479 14944-15268/com.qopius.camera_v2 I/RequestThread-0: Received jpeg.
2019-11-05 11:38:55.480 14944-15268/com.qopius.camera_v2 I/RequestThread-0: Producing jpeg buffer...
2019-11-05 11:38:55.482 14944-14944/com.qopius.camera_v2 D/myLog: onImageAvailable
2019-11-05 11:38:55.490 14944-14944/com.qopius.camera_v2 E/MyPhotoCounter: shoot 2 end
2019-11-05 11:38:55.490 14944-14944/com.qopius.camera_v2 E/Image: Image capture sucessfull111111111111 ------------------>
2019-11-05 11:38:55.490 14944-14944/com.qopius.camera_v2 E/MyPhotoCounter: 2
此后,发生以下错误:
2019-11-05 11:38:56.387 14944-15051/com.qopius.camera_v2 E/Socket status ======>: camera open
2019-11-05 11:38:56.388 14944-15051/com.qopius.camera_v2 W/SENT: [SocketResponse{command='shoot',shootCount=3}] 5
2019-11-05 11:38:56.388 14944-15051/com.qopius.camera_v2 D/Socket: While loop running ...............
2019-11-05 11:38:56.389 14944-15051/com.qopius.camera_v2 E/Socket: 82
2019-11-05 11:38:56.389 14944-15051/com.qopius.camera_v2 E/myLog: onDisConnectedJSON document was not fully consumed.
2019-11-05 11:38:56.390 14944-15051/com.qopius.camera_v2 E/Sever: server disconnectedcom.google.gson.JsonIOException: JSON document was not fully consumed.
2019-11-05 11:38:56.390 14944-15051/com.qopius.camera_v2 E/Async: Socket running!!!
2019-11-05 11:38:56.391 14944-14944/com.qopius.camera_v2 E/myLog: SocketResponse{command='shoot',shootCount=3}
2019-11-05 11:38:56.396 14944-15473/com.qopius.camera_v2 D/Socket: While loop running ...............
2019-11-05 11:38:56.397 14944-15473/com.qopius.camera_v2 E/Socket: 0
2019-11-05 11:38:56.548 14944-15100/com.qopius.camera_v2 E/RequestThread-0: Hit timeout for jpeg callback!
2019-11-05 11:38:56.552 14944-15100/com.qopius.camera_v2 E/CameraDeviceState: Cannot receive result while in state: 0
2019-11-05 11:38:56.553 14944-15100/com.qopius.camera_v2 I/CameraDeviceState: Legacy camera service transitioning to state IDLE
2019-11-05 11:38:56.553 14944-15100/com.qopius.camera_v2 E/CameraDeviceState: Cannot call idle while in state: 0
2019-11-05 11:38:59.757 14944-15318/com.qopius.camera_v2 E/RequestThread-0: Hit timeout for jpeg callback!
2019-11-05 11:38:59.772 14944-15318/com.qopius.camera_v2 I/CameraDeviceState: Legacy camera service transitioning to state IDLE
2019-11-05 11:38:59.772 14944-15318/com.qopius.camera_v2 E/CameraDeviceState: Cannot call idle while in state: 0