中的Nexus 7平板电脑的开放式摄像机 [英] Open Camera in Nexus 7 tablet
问题描述
我已经开发了Android的一个摄像头应用程序。它使用表面视图捕获图像。下面是code,我用来打开相机
I have developed a Camera application in android. It captures image using a surface view. Below is the code that I used to open camera
try {
// attempt to get a Front Camera instance
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out
.println("fail to connect to Front Camera");
}
if (c == null) {
try {
// attempt to get a Back Camera instance
c = Camera.open(1);
} catch (Exception e) {
// TODO: handle exception
System.out
.println("fail to connect to Camera with id = 1");
}
}
if (c == null) {
try {
// attempt to get a Back Camera instance
c = Camera.open(0);
} catch (Exception e) {
// TODO: handle exception
System.out
.println("fail to connect to Camera with id = 0");
}
}
if (c == null) {
try {
// attempt to get a Back Camera instance
c = Camera.open();
} catch (Exception e) {
// TODO: handle exception
System.out
.println("fail to connect to Back Camera");
return c;
}
其中c是相机的对象。
Where c is an object of Camera.
它工作正常,在其他手机不同的的Nexus 7平板电脑。 ,但仍然对象 C = Camera.open()抛出异常在所有情况下> C
是空
。
It works fine in other phones except Nexus 7 Tablet. In Nexus 7, the code throws exception in all cases except the last one i.e. c = Camera.open();
, but still the object c
is null
.
下面是堆栈跟踪
11-22 12:36:57.559 W/System.err(7621): java.lang.NullPointerException
11-22 12:36:57.559 W/System.err(7621): at
com.MyPackage.OpenCamera.getFrontCameraInstance(OpenCamera.java:238)
11-22 12:36:57.559 W/System.err(7621): at
com.MyPackage.OpenCamera.onCreate(OpenCamera.java:123)
11-22 12:36:57.559 W/System.err(7621): at
android.app.Activity.performCreate(Activity.java:5104)
11-22 12:36:57.559 W/System.err(7621): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-22 12:36:57.559 W/System.err(7621): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-22 12:36:57.559 W/System.err(7621): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-22 12:36:57.559 W/System.err(7621): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread
$H.handleMessage(ActivityThread.java:1234)
11-22 12:36:57.559 W/System.err(7621): at
android.os.Handler.dispatchMessage(Handler.java:99)
11-22 12:36:57.559 W/System.err(7621): at android.os.Looper.loop
(Looper.java:137)
11-22 12:36:57.559 W/System.err(7621): at android.app.ActivityThread.main
(ActivityThread.java:5039)
11-22 12:36:57.559 W/System.err(7621): at
java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invoke
(Method.java:511)
11-22 12:36:57.559 W/System.err(7621): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:793)
11-22 12:36:57.559 W/System.err(7621): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-22 12:36:57.559 W/System.err(7621): at dalvik.system.NativeStart.main
(Native Method)
11-22 12:36:57.559 I/System.out(7621): Error in setting Parameter
11-22 12:36:57.609 I/ActivityManager(480): Displayed
11-22 12:36:57.679 W/System.err(7621): at
android.location.Geocoder.getFromLocation(Geocoder.java:136)
11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.
$MyTimmer$1.run(OpenCamera.java:336)
11-22 12:36:57.679 W/System.err(7621): java.lang.NullPointerException
11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.
$MyTimmer$1.run(OpenCamera.java:344)
所以我不能用它来捕捉图像。 任何解决方案???
So I can't use it to capture image. Any solution???
谢谢...
推荐答案
在使用下列code对了Nexus 7: INT cameraId = CameraInfo.CAMERA_FACING_FRONT; 该cameraId为1,但是,正如你所看到的,使用1作为参数导致错误。 我发现,使用0而不是1的作品,但相机必须被释放每次使用后让我继续前进,调用Camera.open(cameraId).release(); 我知道这是丑陋的,但它似乎与Nexus 7,以解决这一问题。
When using the following code on the Nexus 7: int cameraId = CameraInfo.CAMERA_FACING_FRONT; The cameraId is 1, however, as you have seen, using 1 as the parameter results in an error. I have found that using 0 instead of 1 works but the camera must be released after each use so I go ahead and call Camera.open(cameraId).release(); I realize this is ugly but it seems to solve the issue with the Nexus 7.
int cameraId = -1;
boolean errorFound = false;
boolean hasFeatCamera = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
if (hasFeatCamera) {
try{
cameraId = CameraInfo.CAMERA_FACING_BACK;
Camera.open(cameraId).release();
}catch(Exception e){
Camera.open(0).release();
}
} else if(CameraInfo.CAMERA_FACING_FRONT>-1){
try{
cameraId = CameraInfo.CAMERA_FACING_FRONT;
Camera.open(cameraId).release();
}catch(Exception e){
errorFound = true;
}
if(errorFound){
try{
Camera.open(0).release(); // Put in for Nexus 7 as CameraInfo.CAMERA_FACING_FRONT= 1 but it only loads if the id is actually 0
cameraId = 0;
}catch(Exception e){
cameraId = -1;
}
}
}
if(cameraId<0){
Toast.makeText(this, "No camera found.",
Toast.LENGTH_LONG).show();
}
这篇关于中的Nexus 7平板电脑的开放式摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!