我一直在开发一个使用相机功能的应用程序,并且我已经在使用最新 SDK 打包的 android 模拟器上完成了大部分测试。我不确定是过度使用还是什么,但通常可以使用的应用程序的相机部分现在有时会冻结。我刚刚在我的应用程序中添加了一个新库,但它不在相机活动中。我想知道是否有人对为什么我可能会得到类似以下 logcat 的东西有任何想法。
06-27 22:25:44.406: E/Trace(787): error opening trace file: No such file or directory(2)
06-27 22:28:38.003: E/Camera(787): Error 100
06-27 22:28:51.472: E/Camera(787): Error 100
06-27 22:29:09.712: E/Camera(787): Error 100
06-27 22:29:17.652: E/Camera(787): Error 100
06-27 22:29:59.582: E/Camera(787): Error 100
06-27 22:30:27.352: E/Camera(787): Error 100
06-27 22:31:10.763: E/AndroidRuntime(787): FATAL EXCEPTION: main
06-27 22:31:10.763: E/AndroidRuntime(787): java.lang.RuntimeException: takePicture failed
06-27 22:31:10.763: E/AndroidRuntime(787): at android.hardware.Camera.native_takePicture(Native Method)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.hardware.Camera.takePicture(Camera.java:1095)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.hardware.Camera.takePicture(Camera.java:1040)
06-27 22:31:10.763: E/AndroidRuntime(787): at com.example.myapp.camera.CameraPreview.takeSnapshot(CameraPreview.java:279)
06-27 22:31:10.763: E/AndroidRuntime(787): at com.example.myapp.camera.TakePhotoActivity$1.onClick(TakePhotoActivity.java:37)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.view.View.performClick(View.java:4204)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.view.View$PerformClick.run(View.java:17355)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.os.Handler.handleCallback(Handler.java:725)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.os.Looper.loop(Looper.java:137)
06-27 22:31:10.763: E/AndroidRuntime(787): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 22:31:10.763: E/AndroidRuntime(787): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 22:31:10.763: E/AndroidRuntime(787): at java.lang.reflect.Method.invoke(Method.java:511)
06-27 22:31:10.763: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 22:31:10.763: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 22:31:10.763: E/AndroidRuntime(787): at dalvik.system.NativeStart.main(Native Method)
所有不同的相机错误 100 都是我往返于不使用相机的不同活动的原因。当它们显示时,我的 SurfaceView 继承类是空白的。最后的错误是我预览并尝试拍照时引起的。
这是我的 CameraPreview 的大部分代码。
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
private static final String TAG = CameraPreview.class.getSimpleName();
private boolean cameraConfigured = false;
private Context mContext;
private SurfaceHolder mHolder;
public boolean inPreview = false;
public Camera mCamera;
@SuppressWarnings("deprecation")
public CameraPreview(Context context, AttributeSet attr, int defStyle)
{
super(context, attr, defStyle);
Log.d(TAG, "constructor (context)");
mContext = context;
mHolder = getHolder();
//adding this to SurfaceHolder.Callback
//to notify class of surface changes
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mHolder.setKeepScreenOn(true);
}
public CameraPreview(Context context, AttributeSet attr)
{
this(context, attr, 0);
}
public CameraPreview(Context context)
{
this(context, null, 0);
}
private Camera getCameraInstance()
{
Log.d(TAG, "getCameraInstance()");
Camera c = null;
try {
c = Camera.open();
Log.d(TAG, "camera opened");
} catch (Exception e) {
Log.d(TAG, "getCameraInstance()", e);
}
if(c == null)
{
Log.d(TAG, "back facing camera not found");
//ONLY ALLOW FRONT CAMERA FOR NEXUS 7 TESTING
Camera.CameraInfo cInfo = new Camera.CameraInfo();
int cams = Camera.getNumberOfCameras();
for (int i = 0; i < cams; i++)
{
Camera.getCameraInfo(i, cInfo);
if(cInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
try {
c = Camera.open(i);
} catch (Exception e) {
Log.e(TAG, "failed to open front camera", e);
}
}
}
}
return c;
}
@SuppressWarnings("unused")
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
Log.d(TAG, "onLayout()");
if(changed)
{
final int width = right - left;
final int height = bottom - top;
int previewWidth = width;
int previewHeight = height;
Display display = ((WindowManager)mContext.getSystemService
(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch(display.getRotation())
{
case Surface.ROTATION_0:
previewWidth = height;
previewHeight = width;
mCamera.setDisplayOrientation(90);
break;
case Surface.ROTATION_90:
previewWidth = width;
previewHeight = height;
mCamera.setDisplayOrientation(0);
break;
case Surface.ROTATION_180:
previewWidth = height;
previewHeight = width;
mCamera.setDisplayOrientation(270);
break;
case Surface.ROTATION_270:
previewWidth = width;
previewHeight = height;
mCamera.setDisplayOrientation(180);
break;
}
}
}
private void initPreview(int width, int height)
{
Log.d(TAG, "initPreview()");
if(mCamera != null)
{
if(!cameraConfigured)
{
Camera.Parameters param = mCamera.getParameters();
Camera.Size previewSize = getBestPreviewSize(width, height, param);
Camera.Size pictureSize = getBestPictureSize(param);
if(previewSize != null && pictureSize != null)
{
param.setPreviewSize(previewSize.width, previewSize.height);
param.setPictureSize(pictureSize.width, pictureSize.height);
param.setPictureFormat(ImageFormat.JPEG);
mCamera.setParameters(param);
requestLayout();
cameraConfigured = true;
}
}
}
}
private Camera.Size getBestPreviewSize(int width, int height,
Camera.Parameters param)
{
Log.d(TAG, "getBestPreviewSize()");
Camera.Size ret = null;
final double ASPECT_TOLERANCE = 0.1;
double targetAspect = (double)width / (double)height;
ArrayList<Camera.Size> matchedPreviewSizes = new ArrayList<Camera.Size>();
List<Camera.Size> previewSizes = param.getSupportedPreviewSizes();
for(Camera.Size size : previewSizes)
{
double previewAspect = (double) size.width / (double) size.height;
if(size.width <= width && size.height <= height
&& Math.abs(targetAspect - previewAspect) > ASPECT_TOLERANCE)
{
matchedPreviewSizes.add(size);
}
}
if(!matchedPreviewSizes.isEmpty())
{
ret = Collections.max(matchedPreviewSizes, sizeComparator);
}
else
{
ret = Collections.max(previewSizes, sizeComparator);
}
return ret;
}
private Camera.Size getBestPictureSize(Camera.Parameters param)
{
Log.d(TAG, "getBestPictureSize()");
Camera.Size ret = null;
for(Camera.Size newSize : param.getSupportedPictureSizes())
{
if(ret == null)
{
ret = newSize;
}
else
{
int retArea = ret.width * ret.height;
int newArea = newSize.width * newSize.height;
if(newArea > retArea)
{
ret = newSize;
}
}
}
Log.d(TAG, Integer.toString(ret.width)+"x"+Integer.toString(ret.height));
return ret;
}
//call by onPause
public void clearCamera()
{
Log.d(TAG, "clearCamera()");
if(inPreview)
{
mCamera.stopPreview();
}
try {
mCamera.setPreviewDisplay(null);
mCamera.release();
mCamera = null;
inPreview = false;
} catch (Exception e) {
Log.d(TAG, "clearCamera() " + e.getMessage());
}
}
public void setCamera()
{
Log.d(TAG, "setCamera()");
mCamera = getCameraInstance();
}
public void startPreview()
{
Log.d(TAG, "startPreview()");
if(cameraConfigured && mCamera != null)
{
try {
mCamera.setPreviewDisplay(mHolder);
} catch (Exception e) {
Log.e(TAG, "start preview", e);
}
mCamera.startPreview();
inPreview = true;
}
}
public void takeSnapshot()
{
if(inPreview)
{
Log.d(TAG, "takeSnapshot()");
mCamera.takePicture(null, null, photoCallback);
inPreview = false;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Log.d(TAG, "surfaceChanged()");
initPreview(width, height);
startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.d(TAG, "surfaceCreated()");
//no-op wait for surface changed
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
Log.d(TAG, "surfaceDestroyed()");
//no-op take care of in activity
}