我的程序假设每250毫秒byte[]
以ArrayList
的形式存储scheduleWithFixedDelay
图像。
我在两个不同的设备上测试相同的代码: 第一个是运行Android 4.4.2的Nexus 7,第二个是运行Android 4.4.3的Nexus 10。
Nexus 7完美地运行class RunnableCamera implements Runnable {
ScheduledExecutorService serviceCam = Executors.newSingleThreadScheduledExecutor();
@Override
public void run() {
startCamera CAM = new startCamera();
serviceCam.scheduleWithFixedDelay(new Runnable()
{
@Override
public void run()
{
camera.takePicture(null, null, mPicture);
}
}, 0, 250, TimeUnit.MILLISECONDS);
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
camera.startPreview();
imagesArrayList.add(data);
Log.d("image", "byte[] added to ArrayList");
}
};
public class startCamera implements Callback {
//private Camera camera;
private SurfaceHolder holder = null;
public startCamera() {
videoView = (VideoView) findViewById(R.id.vwCamera);
holder = videoView.getHolder();
holder.addCallback(this);
Log.d("bro", "startCamera() constructor");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
Log.i("bro", "surfaceCreated");
camera = Camera.open(0);
camera.setDisplayOrientation(90);
if (camera != null) {
camera.setPreviewDisplay(holder);
} else {
Log.i("bro", "camera = null");
}
} catch (Exception e) {
Log.v(TAG, "Could not start the preview-Display123");
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i("bro", "surfaceChanged");
//Sets correct preview resolution
if (holder.getSurface() == null){
Log.i("bro", "holder.getSurface() == null");
return;
}
Camera.Parameters parameters = camera.getParameters();
Log.i("bro", "camera.getParameters();");
sizes = parameters.getSupportedPreviewSizes();
Log.i("bro", "parameters.getSupportedPreviewSizes();");
Camera.Size optimalSize = getBestPreviewSize(width, height);
try {
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
camera.setParameters(parameters);
} catch (NullPointerException a) {
}
Log.i("bro", "startPreview()");
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("bro", "surfaceDestroyed");
if (camera != null) {
camera.stopPreview();
camera.release();
Log.i("bro", "CAMERA STOPPED");
}
}
private Camera.Size getBestPreviewSize(int width, int height) {
Camera.Size result = null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return result;
}
}
}
功能,但Nexus 10却没有。 Nexus 10运行一次然后停止。
Thread thread1 = new Thread(new RunnableCamera());
thread1.start();
在我的onCreate方法中,我启动Runnable类,如:
{{1}}
如果我将时间延迟更改为600毫秒,Nexus 10可以完美地运行它。我想知道这是第一代Nexus 10的设备问题,Android 4.4.3问题还是我的代码有问题。有更好的方法吗?