问题背景:要让Camera循环聚焦,聚焦完成后进行拍照,在拍照的数据里截取出一定区域的数据。在initCamera里设置聚焦模式:
List allFocus = myParam.getSupportedFocusModes();
for(String ff:allFocus){
Log.i(tag, ff + "...FOCUS...");
}
if(allFocus.contains(Camera.Parameters.FLASH_MODE_AUTO)){
myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
Focus_Mode = 1;
}
else if(allFocus.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)){
myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); // FOCUS_MODE_CONTINUOUS_PICTURE FOCUS_MODE_AUTO
Focus_Mode = 2;
}
myCamera.setParameters(myParam);
class GetPictureThread implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(!Thread.currentThread().isInterrupted()){
if(myCamera != null && isPreview){
if(Focus_Mode == 1 && (!isFocusing)){
myCamera.autoFocus(mAutoFocusCallback);
}
else if(Focus_Mode == 2){
myCamera.takePicture(myShutterCallback, null, myJpegCallback);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
}
12-07 18:05:33.227: D/dalvikvm(13589): threadid=11: exiting
12-07 18:05:33.227: W/dalvikvm(13589): threadid=11: thread exiting with uncaught exception (group=0x417669a8)
12-07 18:05:33.230: E/AndroidRuntime(13589): FATAL EXCEPTION: Thread-1177
12-07 18:05:33.230: E/AndroidRuntime(13589): java.lang.RuntimeException: autoFocus failed
12-07 18:05:33.230: E/AndroidRuntime(13589): at android.hardware.Camera.native_autoFocus(Native Method)
12-07 18:05:33.230: E/AndroidRuntime(13589): at android.hardware.Camera.autoFocus(Camera.java:1120)
12-07 18:05:33.230: E/AndroidRuntime(13589): at org.yanzi.rectphoto_wuzhou.RectPhoto$GetPictureThread.run(RectPhoto.java:428)
12-07 18:05:33.230: E/AndroidRuntime(13589): at java.lang.Thread.run(Thread.java:838)
12-07 18:05:33.240: I/Camera(13589): handleMessage: 2
F:\1.log (12 hits)
Line 12829: 12-08 10:14:19.477 D/CameraClient( 142): autoFocus (pid 10314)
Line 12831: 12-08 10:14:19.477 D/MtkCam/CamDevice( 142): (599)(Default:0)[CamDevice::autoFocus] +
Line 12834: 12-08 10:14:19.477 D/MtkCam/CamAdapter( 142): (599)(Default)[autoFocus] +
Line 12861: 12-08 10:14:19.477 D/aaa_hal ( 142): [autoFocus()]
Line 12877: 12-08 10:14:19.477 D/MtkCam/CamAdapter( 142): (599)(Default)[autoFocus] -
Line 20489: 12-08 10:14:20.677 D/CameraClient( 142): autoFocus (pid 10314)
Line 20491: 12-08 10:14:20.677 D/MtkCam/CamDevice( 142): (142)(Default:0)[CamDevice::autoFocus] +
Line 20503: 12-08 10:14:20.678 E/MtkCam/CamDevice( 142): (142)(Default:0)[CamDevice::autoFocus] preview is not enabled (autoFocus){#552:mediatek/hardware/camera/device/CamDevice/CamDevice.cpp}
Line 20503: 12-08 10:14:20.678 E/MtkCam/CamDevice( 142): (142)(Default:0)[CamDevice::autoFocus] preview is not enabled (autoFocus){#552:mediatek/hardware/camera/device/CamDevice/CamDevice.cpp}
Line 20512: 12-08 10:14:20.679 E/AndroidRuntime(10314): java.lang.RuntimeException: autoFocus failed
Line 20514: 12-08 10:14:20.679 E/AndroidRuntime(10314): at android.hardware.Camera.native_autoFocus(Native Method)
Line 20516: 12-08 10:14:20.679 E/AndroidRuntime(10314): at android.hardware.Camera.autoFocus(Camera.java:1120)
可以看到,上面提到preview is not enabled,竟然说preview没有开启。可我明明preview已经开启了,而且我在扫描线程里设置了判断if(myCamera != null && isPreview)。参考国外这位大大的帖子http://www.hitziger.net/blog/android-camera-autofocus-failed/ 上面提到auto focus失败原因是surfaceholder还没有被创建,换句话camera还没有开启预览就进行自动聚焦了。但其实我的扫描线程启动已经加了延迟,确保camera预览已经开启,姑且信了吧。把线程开启的地方加到了surfacechanged,因为我的initcamera是在surfacechanged里面,initcamera的最后就是startPreview。但依然出错。我加了个延迟在surfacechanged依旧报错。后来参考又一个人的帖子,在activity的onResume方法里进行mySurfaceHolder.addCallback(this);确保surfaceview已经创建了再添加回调。这样干确实严谨一点,但依旧报错。事实上这样做不是必须的,因为我把GetPictureThread去掉之后,一切ok。我加个button,拍照的时候自动聚焦,如果聚焦成功再触发takePicture也是ok的。
问题出在哪呢?最后才恍然大悟,问题在拍照的jpegCallback上,代码如下:
PictureCallback myJpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Log.i(tag, "myJpegCallback:onPictureTaken...");
long t1 = System.currentTimeMillis();
if(null != data){
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
myCamera.stopPreview();
isPreview = false;
}
Matrix matrix = new Matrix();
matrix.postRotate((float)90.0);
Bitmap rotaBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false);
//Bitmap sizeBitmap = Bitmap.createScaledBitmap(rotaBitmap, 540, 800, true);
Bitmap rectBitmap = Bitmap.createBitmap(rotaBitmap, square.left, square.top, square.width(), square.height());
if(null != rectBitmap)
{
saveThread.setSaveBitmap(rectBitmap);
}
//ImageUtil.saveJpeg(rotaBitmap);
myCamera.startPreview();
isPreview = true;
long t2 = System.currentTimeMillis();
Log.i(tag, "本次保存耗时:" + (t2 - t1) + "毫秒");
}
};
注意在拍照时,camera首先停止预览保存完照片后再次开启预览。尽管我加了isPreview这个标志,但这个标志位是不起啥作用的。推测,stoppreviw和startpreview的时候,camera在底层是异步处理的。也就是说程序执行到startpreview,isPreview为真了,但这时camera还没有完全开启预览,而扫描线程再次触发auto focus就会报上面的错误。后来我对这个myJpegCallback测了下时间,完全同样的代码,在geek手机上是900多毫秒左右,在G700上是1800毫秒左右,将近2秒了,所以扫描周期一定得大于这个时间。后来将扫描周期设为3秒,两个手机都ok了。但G700上偶发的也还是会报错,这是因为内存占用太多,手机速度变慢,myJpegCallback回调的周期超过了3秒,重启下手机就好了。在AutoFocusCallback里设置标志isFocusing也是必须的。
final AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
// TODO Auto-generated method stub
isFocusing = true;
if(success){
Log.i(tag, "聚焦成功...");
myCamera.takePicture(myShutterCallback, null, myJpegCallback);
}
else{
Log.i(tag, "聚焦失败...");
}
isFocusing = false;
}
};
参考http://stackoverflow.com/questions/5878042/android-camera-autofocus-on-demand 自从API9,就有了连续聚焦。参考官方对聚焦模式的说明,http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FOCUS_MODE_CONTINUOUS_VIDEO 聚焦最强的是FOCUS_MODE_CONTINUOUS_PICTURE 它是和AutoFocus兼容的,其次是