gpt4 book ai didi

android - 相机无法在 HTC 设备上打开

转载 作者:行者123 更新时间:2023-11-29 21:20:45 25 4
gpt4 key购买 nike

我已经根据提供的代码示例构建了一个相机 here并使用 AOSP GingerBread 的 Camera.java 和 ShutterButton.java 对其进行了增强,我拿走的代码主要用于自动对焦和 ShutterButton。相机已在 Samsung Galaxy Nexus 上进行了测试。和 Samsung Galaxy S4LG Optimus运行 4.0.x 的手机可以正常工作。但是,在 appthwack.com 上进行测试后,结果为 here .这是我的 Camera.open 代码,它不起作用:

    try{   
Camera.open();
}
catch(Exception ex)
{
Log.e(TAG,"Could not open camera",ex);//running on HTC prints could not open camera
}
mPreview=new CameraPreview(this,mCamera);//this is which the NullPointerException is thrown

上面的代码在 Activity 的 onResume 中调用,这意味着 Camera 对象仅在屏幕对用户可见时才被使用,我在 onPause() 中释放 Camera,如下所示:

    sensorManager.unregisterListener(this);
isPreviewShowing=false;
if(mCamera!=null)
killCamera();
container.removeAllViews();

private void killCamera()
{
if(mCamera!=null)
{
mCamera.cancelAutoFocus();
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
}

我已将打开相机的代码更改为:

 Camera.CameraInfo info=new Camera.CameraInfo();
int cameraCount=mCamera.getNumberOfCameras();
int cameraIndex=0;
for(cameraIndex=0;cameraIndex<cameraCount;cameraIndex++)
{
Camera.getCameraInfo(cameraIndex, info);
if(info.facing==CameraInfo.CAMERA_FACING_BACK)
{
try
{
mCamera=Camera.open(cameraIndex);
}
catch(Exception ex)
{
Log.e(TAG, "Could not open camera",ex);
}
}
}

这也没有用,我也尝试将以下代码添加到 CameraPreview类有:

 @SuppressWarnings("deprecation")
public CameraPreview(Context context,Camera camera) {
super(context);
this.ctx=context;
mCamera=camera;
mHolder=this.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
if(mCamera==null)
{
mCamera=null;
Camera.CameraInfo info=new Camera.CameraInfo();
int cameraCount=mCamera.getNumberOfCameras();
int cameraIndex=0;
for(cameraIndex=0;cameraIndex<cameraCount;cameraIndex++)
{
Camera.getCameraInfo(cameraIndex, info);
if(info.facing==CameraInfo.CAMERA_FACING_BACK)
{
try
{
mCamera=Camera.open(cameraIndex);
}
catch(Exception ex)
{
Log.e(TAG, "Could not open camera",ex);
}
}
}

}
try
{
if(mCamera==null)
{
Log.d(TAG,"Camera is null here but why???");
}
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
catch(IOException e)
{
Log.e("CameraPreview","Error getting camera preview",e);
}
}

这也不起作用。此外,对于任何询问我权限的人,我已将所有必要的权限声明为:

  <uses-permission android:name="android.permission.CAMERA" />


<uses-feature android:name="android.hardware.camera"
android:required="true"
/>
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="true"
/>
<uses-feature android:name="android.hardware.sensor.accelerometer"
android:required="true"
/>

开始使用全新的相机会有点困难,我想(如果可能的话)保持相机的现有结构不变,所以你能帮我解决这个问题吗?整个 logcat文件与上面发布的测试结果一起可用。

编辑:

使用全局 SurfaceHolder 对象而不是 holder on holder provider by surfaceCreated 解决了 HTC Desire HD 上的问题,但不是 HTC One S 上的问题。它在此处抛出错误:

 java.lang.NullPointerException at 
com.blutechnologies.scancard.CameraPreview.surfaceCreated(CameraPreview.java:95) at
android.view.SurfaceView.updateWindow(SurfaceView.java:543) at
android.view.SurfaceView.access$000(SurfaceView.java:81) at
android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169) at
android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590) at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1793) at
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2700) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:156) at
android.app.ActivityThread.main(ActivityThread.java:5045) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at
dalvik.system.NativeStart.main(Native Method)

前面是:

 I  ActivityManager START {cmp=com.blutechnologies.scancard/.CameraActivity} from pid 21085
I QualcommCamera Qint android::get_camera_info(int, camera_info*): E
V QCameraHAL void android::HAL_getCameraInfo(int, android::CameraInfo*): E
V QCameraHAL void android::HAL_getCameraInfo(int, android::CameraInfo*): X

我猜这意味着相机很忙,也就是它正在别处使用,是不是因为之前的设备崩溃,如果我把相机关闭代码放在surfaceDestroyed中,它会自行纠正吗? ,这样做似乎非常危险,我将这段代码添加到我的 surfaceDestroyed 中:

  private void killCamera()
{
if(mCamera!=null)
{
mCamera.cancelAutoFocus();
mCamera.release();
mCamera=null;
}
}

相机现在是空白的,根本不显示预览,它在设置预览大小时卡住了。一定是我没有做的事情可以解决这个错误。

最佳答案

这是获取相机的例子:

 private Camera getCameraInstance() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
Camera.getCameraInfo( camIdx, cameraInfo );
if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK ) {
try {
cam = Camera.open( camIdx );
} catch (RuntimeException e) {
Log.e("Camera", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}

return cam;
}

同时检查 list 中的权限

   <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

关于android - 相机无法在 HTC 设备上打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20771267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com