您好
我收到了这个错误:
致命异常:java.lang.RuntimeException setParameters失败
错误日志显示所有camera.setParameters(parameters)
导致崩溃。
这是我的代码:
android.hardware.Camera camera;
public class CameraActivity extends Activity implements SurfaceHolder.Callback , GestureDetector.OnGestureListener{
android.hardware.Camera camera;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Fabric.with(this, new Crashlytics());
Fabric.with(this, new Answers());
setContentView(R.layout.camera_activity);
} else {
}
ButterKnife.inject(this);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cameraImage();
}
});
callback = new android.hardware.Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, android.hardware.Camera camera) {
FileOutputStream outputStream=null;
File file_image = getDirc();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmddhhmms");
String date = simpleDateFormat.format(new Date());
String photo_file="PI_"+date+".jpg";
String file_name = file_image.getAbsolutePath()+"/"+photo_file;
File picfile=new File(file_name);
try {
outputStream=new FileOutputStream(picfile);
outputStream.write(bytes);
outputStream.close();
}catch (FileNotFoundException e){}
catch (IOException ex){}
finally {
}
refreshCamera();
refreshGallery(picfile);
try {
camera.stopPreview();
}catch (Exception e){}
try{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){}
}
};
}
private void refreshGallery(File file){
Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
}
public void refreshCamera(){
if (surfaceHolder.getSurface() == null){
return;
}
}
private File getDirc(){
File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
return new File(dics ,"Camera");
}
public void cameraImage(){
camera.takePicture(null , null ,callback);
MediaPlayer mediaPlayer = MediaPlayer.create(this , R.raw.sound);
mediaPlayer.start();
}
public void surfaceCreated(SurfaceHolder surfaceHolder ) {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
refreshCamera();
setDisplayOrientation();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera=null;
}
public void frontCamera(View view) {
camera.stopPreview();
camera.release();
camera=null;
if (cameraId == 0){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else{
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
}
private void setDisplayOrientation() {
// Now set the display orientation:
mDisplayRotation = Util.getDisplayRotation(CameraActivity.this);
mDisplayOrientation = Util.getDisplayOrientation(mDisplayRotation, 0);
camera.setDisplayOrientation(mDisplayOrientation);
if (mFaceView != null) {
mFaceView.setDisplayOrientation(mDisplayOrientation);
}
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE)
{
// Left to Right swipe action
if (x2 > x1)
{
}
// Right to left swipe action
else
{
camera.stopPreview();
camera.release();
camera=null;
//this one
if(moi == 0){
moi = 1;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_SEPIA);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_SEPIA);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}
}
}
else if(moi == 1){
moi = 2;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_AQUA);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_AQUA);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
else if(moi == 2){
moi = 3;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_BLACKBOARD);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_BLACKBOARD);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
else if(moi == 3){
moi = 4;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
else if(moi == 4){
moi = 5;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_NEGATIVE);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
else if(moi == 5){
moi = 6;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_POSTERIZE);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_POSTERIZE);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
else if(moi == 6){
moi = 7;
if (cameraId == 1){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);
parameters.setPreviewFrameRate(60);
parameters.setPreviewSize(1920 , 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
parameters.setPreviewFrameRate(60);
parameters.setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);
parameters.setPreviewSize(1920, 1080);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
}}
}
}
break;
}
return super.onTouchEvent(event);
}
}
答案 0 :(得分:3)
请尝试使用API level 5
中引入的支持的预览尺寸,而不是使用静态预览尺寸Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
// Choose desired preview size
Camera.Size customSize = //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
camera.setParameters(parameters);
camera.startPreview();
使用此方法会更安全,因为所有设备都不支持手动尺寸
答案 1 :(得分:0)
它可能因为您正在运行/测试的设备而引发错误。我在使用QRScanner时遇到了同样的问题。
尝试更改parameters.setPreviewFrameRate(60);和parameters.setPreviewSize(1920,1080);根据您使用的设备。 快乐编码