以下是一些可能解决Android相机问题的代码示例:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION);
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private void setCameraPreviewSize() {
List sizes = mCamera.getParameters().getSupportedPreviewSizes();
Camera.Size bestSize = sizes.get(0);
for (int i = 1; i < sizes.size(); i++) {
if ((sizes.get(i).width * sizes.get(i).height) > (bestSize.width * bestSize.height)) {
bestSize = sizes.get(i);
}
}
mCamera.getParameters().setPreviewSize(bestSize.width, bestSize.height);
}
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
...
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.setPreviewCallback(new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// process preview data
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
...
}