我遇到Android Camera API问题。我正在调试,因为
camera=Camera.open()
总是返回null,而权限是在android.Manifest中配置的,后置摄像头可用。
所以我检查了Android的可用性和摄像头数量:
Context context = getContext();
boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
int count = getNumberOfCameras();
camera = getCameraInstance();
Boolean b
(检查设备是否有摄像头)returns true
,如预期的那样。
但与此同时,count
(获取可用摄像头的数量)returns 0
。
我的java课程:
package com.example.wouter.zytholog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import static android.hardware.Camera.*;
/**
* Created by wouter on 3/04/2018.
*/
public class CameraFragment extends Fragment implements SurfaceHolder.Callback {
static Camera camera = null;
PictureCallback mCallback;
Button mCaptureBtn;
SurfaceHolder mHolder;
SurfaceView mView;
final int CAMERA_REQUEST_CODE = 1;
public static CameraFragment newInstance(){
CameraFragment fragment = new CameraFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
mView = view.findViewById(R.id.surfaceView);
mHolder = mView.getHolder();
if(ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(getActivity(), new String[] {android.Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
}
else{
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
mCaptureBtn = view.findViewById(R.id.capture);
mCaptureBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
capture();
}
});
mCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
Intent i = new Intent(getActivity(), showBeerActivity.class);
i.putExtra("capture", bytes);
startActivity(i);
return;
}
};
Context context = getContext();
boolean b = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
int count = getNumberOfCameras();
camera = getCameraInstance();
return view;
}
private void capture() {
camera.takePicture(null,null,mCallback);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
private Camera getCameraInstance() {
Camera c = null;
try{
c= open();
}
catch(Exception e){
}
return c; //returns null if camera is not available
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case CAMERA_REQUEST_CODE:{
if(grantResults.length >0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}else{
Toast.makeText(getContext(), "Please give us permission to the camera", Toast.LENGTH_LONG).show();
}
break;
}
}
}
}
Android.Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wouter.zytholog">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LogoScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".LoginOrRegisterActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".showBeerActivity"></activity>
</application>
</manifest>
答案 0 :(得分:0)
private CameraManager mManager;
private String[] mCameraIds;
mManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
mCameraIds = mManager.getCameraIdList();
答案 1 :(得分:-1)
getnumberofcameras是静态方法尝试使用它作为
count =Camera.getNumberOfCameras ();