Android:“Camera.takePicture failed”例外 [英] Android: "Camera.takePicture failed" Exception
问题描述
我遇到了一个奇怪的问题。我在HTC EVO上做我的测试。我写了一个演示相机应用程序针对2.2,几乎一切正常工作。问题是,在采取三或四张图片后,应用程序崩溃,并给我以下消息:
D / QualcommCamera硬件):takePicture(479)
D / QualcommCameraHardware(64):val_ril_status = 0,val_wimax_status = 0,val_hotspot_status = 0,val_low_temp_limit = 10.000000,val_batt_temp = 29.799999,val_low_temp_limit = 15,val_batt_cap = 96
D / QualcommCamera硬件(64):FLASHLIGHT是ENABLED
D / QualcommCamera硬件(64):stopPreviewInternal E:1
D / QualcommCamera硬件(64):cancelAutoFocusInternal E
D / QualcommCamera硬件0
我/ QualcommCamera硬件(64):deinitPreview E
D / QualcommCamera硬件(64):launch_watchdog_thread:
D / QualcommCamera硬件(64):watchdog_thread_id = 369048
I / QualcommCamera硬件):register_buf:camfd = 35,reg = 1 buffer = 0x4153f000
I / QualcommCameraHardware(64):register_buf:camfd = 35,reg = 1 buffer = 0x415bf000
I / QualcommCameraHardware camfd = 35,reg = 1 buffer = 0x4163f000
I / QualcommCameraHardware(64):register_buf:camfd = 35,reg = 1 buffer = 0x416bf000
I / QualcommCameraHardware(64):register_buf:camfd = reg = 1 buffer = 0x4331d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x4351d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x4371d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x4391d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x43b1d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x43d1d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x43f1d000
I / QualcommCameraHardware(64):register_buf:camfd = 38,reg = 1 buffer = 0x4411d000
I / QualcommCameraHardware(64):deinitPreview X
D / QualcommCameraHardware(64):stopPreviewInternal X:0
/ QualcommCameraHardware(64):initRaw E:raw size = 3264x2448
D / QualcommCameraHardware(64):initRaw:raw ration = 0.750000,display size = 768x432
D / QualcommCameraHardware 768,thumbnail_height = 576,thumbnail_buffer_size = 663552
D / QualcommCamera硬件(64):native_access_parm:fd 24,类型1,长度32
D / mm-camera-ov8810_u(64):andy cam_mode_sel 0
D / QualcommCameraHardware(64):initRaw:初始化mRawHeap。
E / MemoryHeapBase(64):错误打开/ dev / pmem_camera:没有这样的文件或目录
E / QualcommCameraHardware(64):无法构造pmem池的主堆/ dev / pmem_camera
E / QualcommCameraHardware(64):initRaw X与pmem_camera失败,尝试与pmem_adsp
D / QualcommCamera硬件(64):frame_thread X
D / QualcommCamera硬件(64):watchdog_thread_id = 369048
D / QualcommCamera硬件(64):release_watchdog_thread:frame_thread_released = 1
E / MemoryHeapBase(64):mmap(fd = 38,size = 11988992)失败(参数无效)
E / QualcommCameraHardware堆的pmem池/ dev / pmem_adsp
E / QualcommCamera硬件(64):initRaw X:错误初始化mRawHeap
E / QualcommCamera硬件(64):initRaw失败。不拍照。
D / AndroidRuntime(2650):关闭VM
W / dalvikvm(2650):threadid = 1:线程以未捕获异常退出(组= 0x400259f8)
E / AndroidRuntime致命异常:main
E / AndroidRuntime(2650):java.lang.RuntimeException:takePicture failed
E / AndroidRuntime(2650):在android.hardware.Camera.native_takePicture E / AndroidRuntime(2650):在android.hardware.Camera.takePicture(Camera.java:535)
E / AndroidRuntime(2650):在android.hardware.Camera.takePicture $ b E / AndroidRuntime(2650):at spikes.cameraSpike03.MainActivity.takePicture(MainActivity.java:90)
E / AndroidRuntime(2650):at spikes.cameraSpike03.MainActivity.access $ 3(MainActivity.java:87 )
E / AndroidRuntime(2650):at android.view.View.performClick(View。)
E / AndroidRuntime(2650):at spikes.cameraSpike03.MainActivity $ 3.onClick(MainActivity.java:80) java:2408)
E / AndroidRuntime(2650):在android.view.View $ PerformClick.run(View.java:8817)
E / AndroidRuntime(2650):在android.os.Handler。 handleCallback(Handler.java:587)
E / AndroidRuntime(2650):在android.os.Handler.dispatchMessage(Handler.java:92)
E / AndroidRuntime(2650):在android.os。 Looper.loop(Looper.java:144)
E / AndroidRuntime(2650):在android.app.ActivityThread.main(ActivityThread.java:4937)
E / AndroidRuntime(2650):在java。 lang.reflect.Method.invokeNative(Native方法)
E / AndroidRuntime(2650):在java.lang.reflect.Method.invoke(Method.java:521)
E / AndroidRuntime(2650):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
E / AndroidRuntime(2650):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626 )
E / AndroidRuntime(2650):在dalvik.system.NativeStart.main(本地方法)
W / ActivityManager(107):强制完成活动spikes.cameraSpike03 / .MainActivity
D / QualcommCameraHardware(64):void * watchdog(void *),exit,frame_thread_released = 1
D / QualcommCameraHardware(64):void * watchdog(void *),frame_thread_released = 1,cnt = 0
QualcommCameraHardware(64):void release_watchdog_thread():pthread_join在看门狗上成功。
这可能是内存管理问题吗?
以下是我使用的所有代码。
[AndroidManifest.xml]
<?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =spikes.cameraSpike03
android:versionCode =1
android:versionName =1.0>
< application android:icon =@ drawable / iconandroid:label =@ string / app_nameandroid:debuggable =true
< activity android:name =。MainActivityandroid:label =@ string / app_nameandroid:screenOrientation =portrait>
< intent-filter>
< action android:name =android.intent.action.MAIN/>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< uses-sdk android:minSdkVersion =8/>
< uses-permission android:name =android.permission.CAMERA/>
< uses-feature android:name =android.hardware.camera/>
< uses-feature android:name =android.hardware.camera.autofocus/>
< uses-feature android:name =android.hardware.camera.flash/>
< / manifest>
[main.xml]
<?xml version =1.0encoding =utf-8?&
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/androidandroid:orientation =verticalandroid:layout_width =fill_parentandroid:layout_height =fill_parent> ;
< SurfaceView android:id =@ + id / svCameraViewandroid:layout_width =fill_parentandroid:layout_height =fill_parent/>
< LinearLayout android:layout_width =fill_parentandroid:layout_height =wrap_contentandroid:orientation =horizontalandroid:background =#3000android:layout_alignParentBottom =trueandroid:layout_centerHorizontal = android:gravity =center_horizontal>
< Button android:id =@ + id / btnCaptureandroid:layout_width =wrap_contentandroid:layout_height =wrap_contentandroid:text =capture/>
< CheckBox android:id =@ + id / chkAutofocusandroid:layout_width =wrap_contentandroid:layout_height =wrap_contentandroid:text =Autofocus/>
< / LinearLayout>
< / RelativeLayout>
[MainActivity.java]
package spikes.cameraSpike03;
import java.util.List;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import Android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends Activity实现SurfaceHolder.Callback {
private static final String LOG_TAG = MainActivity.class.getName();
private static final String LOG_LINE =---------------------------------;
私人相机_camera;
private boolean _previewIsRunning = false;
private SurfaceView _svCameraView;
private SurfaceHolder _surfaceHolder;
private Button _btnCapture;
private复选框_chkAutofocus;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow()。setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
_svCameraView =(SurfaceView)findViewById(R.id.svCameraView);
_surfaceHolder = _svCameraView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
_chkAutofocus =(CheckBox)findViewById(R.id.chkAutofocus);
_btnCapture =(Button)findViewById(R.id.btnCapture);
_btnCapture.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(_camera!= null){
// Decide是否使用自动对焦
if(_chkAutofocus.isChecked()){
Log.d(LOG_TAG,LOG_LINE +准备使用自动对焦拍摄照片...);
_camera.autoFocus(new AutoFocusCallback(){
@Override
public void onAutoFocus(boolean success,Camera camera){
Log.d(LOG_TAG,LOG_LINE +_camera.autoFocus。 onAutoFocus(...));
takePicture();
Log.d(LOG_TAG,LOG_LINE +_camera.autoFocus.onAutoFocus完成);
}
});
}
else {
Log.d(LOG_TAG,LOG_LINE +准备拍摄没有自动对焦的图片... 。);
takePicture();
}
}
}
});
}
private void takePicture(){
Log.d(LOG_TAG,LOG_LINE +takePicture()entered。
_camera.takePicture(_shutterCallback,null,_jpegCallback);
_previewIsRunning = false;
Log.d(LOG_TAG,LOG_LINE +takePicture()finished。);
}
private ShutterCallback _shutterCallback = new ShutterCallback(){
@Override
public void onShutter(){
Log.d(LOG_TAG,LOG_LINE + _shutterCallback.onShutter()called。);
}
};
private PictureCallback _jpegCallback = new PictureCallback(){
@Override
public void onPictureTaken(byte [] data,Camera camera){
Log.d(LOG_TAG, LOG_LINE +_jpegCallback.onPictureTaken()called。);
_camera.startPreview();
_previewIsRunning = true;
}
};
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){
if(_previewIsRunning){
Log.d(LOG_TAG, LOG_LINE +关于停止预览...);
_camera.stopPreview();
Log.d(LOG_TAG,LOG_LINE +停止预览);
}
try {
Log.d(LOG_TAG,LOG_LINE +关于设置相机参数...);
Camera.Parameters parameters = _camera.getParameters();
//获取最佳预览大小,以便在设置参数时不会出现异常
List< Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
size optimumPreviewSize = CameraUtil.getOptimalPreviewSize(supportedPreviewSizes,width,height);
parameter.setPreviewSize(optimalPreviewSize.width,optimalPreviewSize.height);
parameter.setFocusMode(Parameters.FOCUS_MODE_AUTO);
parameter.setFlashMode(Parameters.FLASH_MODE_AUTO);
_camera.setDisplayOrientation(90);
_camera.setParameters(parameters);
_camera.setPreviewDisplay(holder);
Log.d(LOG_TAG,LOG_LINE +完成相机参数设置);
}
catch(Exception ex){
ex.printStackTrace();
Log.e(LOG_TAG,ex.toString());
}
Log.d(LOG_TAG,LOG_LINE +即将开始预览...);
_camera.startPreview();
_previewIsRunning = true;
Log.d(LOG_TAG,LOG_LINE +开始预览);
}
@Override
public void surfaceCreated(SurfaceHolder holder){
_camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
Log.d(LOG_TAG,LOG_LINE +拆除相机,因为表面被破坏... );
_camera.stopPreview();
_previewIsRunning = false;
_camera.release();
Log.d(LOG_TAG,LOG_LINE +由于表面被破坏,已完成拆卸相机。
}
@Override
public void onConfigurationChanged(Configuration newConfig){
Log.d(LOG_TAG,LOG_LINE +关于将请求方向设置为SCREEN_ORIENTATION_PORTRAIT ... );
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Log.d(LOG_TAG,LOG_LINE +已成功将请求方向设置为SCREEN_ORIENTATION_PORTRAIT。);
}
}
[CameraUtil.java]
package spikes.cameraSpike03;
import java.util.List;
import android.hardware.Camera.Size;
public class CameraUtil {
private CameraUtil(){}
/ **
*返回包含最佳预览大小的Size对象为当前硬件。
*此代码基于以下位置找到:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
*
* @param supportedSizes
*表示此硬件支持的所有已知预览大小的Size对象列表。
*
* @param w
*表面宽度。
*
* @param h
*曲面高度。
*
* @return
*返回包含当前硬件的最佳预览大小的尺寸的Size对象。
* /
public static Size getOptimalPreviewSize(List< Size> supportedSizes,int w,int h){
final double ASPECT_TOLERANCE = 0.05;
double targetRatio =(double)w / h;
if(supportedSizes == null)return null;
size optimumSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
//尝试查找大小匹配的长宽比和大小
for(Size size:supportedSizes){
double ratio =(double)size.width / size.height;
if(Math.abs(ratio - targetRatio)> ASPECT_TOLERANCE)continue;
if(Math.abs(size.height - targetHeight)< minDiff){
optimumSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
//找不到匹配的宽高比,忽略需求
if(optimumSize == null){
minDiff = Double.MAX_VALUE;
for(Size size:supportedSizes){
if(Math.abs(size.height - targetHeight)< minDiff){
optimumSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimumSize;
}
}
任何建议都将非常感激。
谢谢。
解决方案我也有类似的问题。后来我发现startPreview很重要。
_camera.startPreview()
在takePicutre检出点之前非常重要5和6在此链接中。
http://developer.android.com/reference/android/hardware/Camera.html
I'm experiencing an odd problem. I'm doing my testing on an HTC EVO. I have written a demo camera application targeting 2.2 and almost everything works correctly. The problem is that after taking three or four pictures, the application crashes and gives me the following messages:
D/QualcommCameraHardware( 64): takePicture(479) D/QualcommCameraHardware( 64): val_ril_status = 0,val_wimax_status = 0,val_hotspot_status = 0,val_low_temp_limit = 10.000000,val_batt_temp = 29.799999,val_low_temp_limit = 15,val_batt_cap = 96 D/QualcommCameraHardware( 64): FLASHLIGHT is ENABLED D/QualcommCameraHardware( 64): stopPreviewInternal E: 1 D/QualcommCameraHardware( 64): cancelAutoFocusInternal E D/QualcommCameraHardware( 64): cancelAutoFocusInternal X: 0 I/QualcommCameraHardware( 64): deinitPreview E D/QualcommCameraHardware( 64): launch_watchdog_thread: D/QualcommCameraHardware( 64): watchdog_thread_id = 369048 I/QualcommCameraHardware( 64): register_buf: camfd = 35, reg = 1 buffer = 0x4153f000 I/QualcommCameraHardware( 64): register_buf: camfd = 35, reg = 1 buffer = 0x415bf000 I/QualcommCameraHardware( 64): register_buf: camfd = 35, reg = 1 buffer = 0x4163f000 I/QualcommCameraHardware( 64): register_buf: camfd = 35, reg = 1 buffer = 0x416bf000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x4331d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x4351d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x4371d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x4391d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x43b1d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x43d1d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x43f1d000 I/QualcommCameraHardware( 64): register_buf: camfd = 38, reg = 1 buffer = 0x4411d000 I/QualcommCameraHardware( 64): deinitPreview X D/QualcommCameraHardware( 64): stopPreviewInternal X: 0 D/QualcommCameraHardware( 64): initRaw E: raw size=3264x2448 D/QualcommCameraHardware( 64): initRaw: raw ration = 0.750000, display size=768x432 D/QualcommCameraHardware( 64): initRaw: thumbnail_width=768, thumbnail_height=576, thumbnail_buffer_size=663552 D/QualcommCameraHardware( 64): native_access_parm: fd 24, type 1, length 32 D/mm-camera-ov8810_u( 64): andy cam_mode_sel 0 D/QualcommCameraHardware( 64): initRaw: initializing mRawHeap. E/MemoryHeapBase( 64): error opening /dev/pmem_camera: No such file or directory E/QualcommCameraHardware( 64): failed to construct master heap for pmem pool /dev/pmem_camera E/QualcommCameraHardware( 64): initRaw X failed with pmem_camera, trying with pmem_adsp D/QualcommCameraHardware( 64): frame_thread X D/QualcommCameraHardware( 64): watchdog_thread_id = 369048 D/QualcommCameraHardware( 64): release_watchdog_thread: frame_thread_released = 1 E/MemoryHeapBase( 64): mmap(fd=38, size=11988992) failed (Invalid argument) E/QualcommCameraHardware( 64): failed to construct master heap for pmem pool /dev/pmem_adsp E/QualcommCameraHardware( 64): initRaw X: error initializing mRawHeap E/QualcommCameraHardware( 64): initRaw failed. Not taking picture. D/AndroidRuntime( 2650): Shutting down VM W/dalvikvm( 2650): threadid=1: thread exiting with uncaught exception (group=0x400259f8) E/AndroidRuntime( 2650): FATAL EXCEPTION: main E/AndroidRuntime( 2650): java.lang.RuntimeException: takePicture failed E/AndroidRuntime( 2650): at android.hardware.Camera.native_takePicture(Native Method) E/AndroidRuntime( 2650): at android.hardware.Camera.takePicture(Camera.java:535) E/AndroidRuntime( 2650): at android.hardware.Camera.takePicture(Camera.java:503) E/AndroidRuntime( 2650): at spikes.cameraSpike03.MainActivity.takePicture(MainActivity.java:90) E/AndroidRuntime( 2650): at spikes.cameraSpike03.MainActivity.access$3(MainActivity.java:87) E/AndroidRuntime( 2650): at spikes.cameraSpike03.MainActivity$3.onClick(MainActivity.java:80) E/AndroidRuntime( 2650): at android.view.View.performClick(View.java:2408) E/AndroidRuntime( 2650): at android.view.View$PerformClick.run(View.java:8817) E/AndroidRuntime( 2650): at android.os.Handler.handleCallback(Handler.java:587) E/AndroidRuntime( 2650): at android.os.Handler.dispatchMessage(Handler.java:92) E/AndroidRuntime( 2650): at android.os.Looper.loop(Looper.java:144) E/AndroidRuntime( 2650): at android.app.ActivityThread.main(ActivityThread.java:4937) E/AndroidRuntime( 2650): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 2650): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime( 2650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) E/AndroidRuntime( 2650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) E/AndroidRuntime( 2650): at dalvik.system.NativeStart.main(Native Method) W/ActivityManager( 107): Force finishing activity spikes.cameraSpike03/.MainActivity D/QualcommCameraHardware( 64): void* watchdog(void*), frame_thread_released = 1, cnt = 0 D/QualcommCameraHardware( 64): void* watchdog(void*), exit, frame_thread_released=1 D/QualcommCameraHardware( 64): void release_watchdog_thread(): pthread_join succeeded on watchdog.
Could this be a memory management problem?
Below is all the code I'm using.
[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="spikes.cameraSpike03" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="android.hardware.camera.flash" /> </manifest>
[main.xml]
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <SurfaceView android:id="@+id/svCameraView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#3000" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center_horizontal"> <Button android:id="@+id/btnCapture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="capture" /> <CheckBox android:id="@+id/chkAutofocus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Autofocus" /> </LinearLayout> </RelativeLayout>
[MainActivity.java]
package spikes.cameraSpike03; import java.util.List; import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.Parameters; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.hardware.Camera.Size; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; public class MainActivity extends Activity implements SurfaceHolder.Callback { private static final String LOG_TAG = MainActivity.class.getName(); private static final String LOG_LINE = "---------------------------------"; private Camera _camera; private boolean _previewIsRunning = false; private SurfaceView _svCameraView; private SurfaceHolder _surfaceHolder; private Button _btnCapture; private CheckBox _chkAutofocus; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); _svCameraView = (SurfaceView)findViewById(R.id.svCameraView); _surfaceHolder = _svCameraView.getHolder(); _surfaceHolder.addCallback(this); _surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); _chkAutofocus = (CheckBox)findViewById(R.id.chkAutofocus); _btnCapture = (Button)findViewById(R.id.btnCapture); _btnCapture.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(_camera != null){ //Decide whether or not to use autofocus if(_chkAutofocus.isChecked()){ Log.d(LOG_TAG, LOG_LINE + "Preparing to take the picture using autofocus..."); _camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { Log.d(LOG_TAG, LOG_LINE + "_camera.autoFocus.onAutoFocus(...) entered."); takePicture(); Log.d(LOG_TAG, LOG_LINE + "_camera.autoFocus.onAutoFocus(...) finished."); } }); } else{ Log.d(LOG_TAG, LOG_LINE + "Preparing to take the picture without autofocus..."); takePicture(); } } } }); } private void takePicture(){ Log.d(LOG_TAG, LOG_LINE + "takePicture() entered."); _camera.takePicture(_shutterCallback, null, _jpegCallback); _previewIsRunning = false; Log.d(LOG_TAG, LOG_LINE + "takePicture() finished."); } private ShutterCallback _shutterCallback = new ShutterCallback() { @Override public void onShutter() { Log.d(LOG_TAG, LOG_LINE + "_shutterCallback.onShutter() called."); } }; private PictureCallback _jpegCallback = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { Log.d(LOG_TAG, LOG_LINE + "_jpegCallback.onPictureTaken() called."); _camera.startPreview(); _previewIsRunning = true; } }; @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if(_previewIsRunning){ Log.d(LOG_TAG, LOG_LINE + "About to stop preview..."); _camera.stopPreview(); Log.d(LOG_TAG, LOG_LINE + "Stopped preview."); } try{ Log.d(LOG_TAG, LOG_LINE + "About to set up camera parameters..."); Camera.Parameters parameters = _camera.getParameters(); //Get the optimal preview size so we don't get an exception when setting the parameters List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes(); Size optimalPreviewSize = CameraUtil.getOptimalPreviewSize(supportedPreviewSizes, width, height); parameters.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height); parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO); parameters.setFlashMode(Parameters.FLASH_MODE_AUTO); _camera.setDisplayOrientation(90); _camera.setParameters(parameters); _camera.setPreviewDisplay(holder); Log.d(LOG_TAG, LOG_LINE + "Finished setting up camera parameters."); } catch(Exception ex){ ex.printStackTrace(); Log.e(LOG_TAG, ex.toString()); } Log.d(LOG_TAG, LOG_LINE + "About to start preview..."); _camera.startPreview(); _previewIsRunning = true; Log.d(LOG_TAG, LOG_LINE + "Started preview."); } @Override public void surfaceCreated(SurfaceHolder holder) { _camera = Camera.open(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.d(LOG_TAG, LOG_LINE + "Tearing down camera because surface was destroyed..."); _camera.stopPreview(); _previewIsRunning = false; _camera.release(); Log.d(LOG_TAG, LOG_LINE + "Finished tearing down camera because surface was destroyed."); } @Override public void onConfigurationChanged(Configuration newConfig){ Log.d(LOG_TAG, LOG_LINE + "About to set request orientation to SCREEN_ORIENTATION_PORTRAIT..."); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Log.d(LOG_TAG, LOG_LINE + "Successfully set request orientation to SCREEN_ORIENTATION_PORTRAIT."); } }
[CameraUtil.java]
package spikes.cameraSpike03; import java.util.List; import android.hardware.Camera.Size; public class CameraUtil { private CameraUtil(){} /** * Returns a Size object containing the dimensions for an optimal preview size for the current hardware. * This code is based on that found at: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html * * @param supportedSizes * A list of Size objects representing all the known preview sizes supported by this hardware. * * @param w * The surface width. * * @param h * The surface height. * * @return * Returns a Size object containing the dimensions for an optimal preview size for the current hardware. */ public static Size getOptimalPreviewSize(List<Size> supportedSizes, int w, int h) { final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) w / h; if (supportedSizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; // Try to find an size match aspect ratio and size for (Size size : supportedSizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : supportedSizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } }
Any suggestions would be greatly appreciated.
Thank you.
解决方案I also had the similar issue. later i found startPreview is very important.
_camera.startPreview()
is very important before the takePicutre checkout the point 5 and 6 in this link.http://developer.android.com/reference/android/hardware/Camera.html
这篇关于Android:“Camera.takePicture failed”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!