Reputation: 105
Hi.
I got this error:
Fatal Exception: java.lang.RuntimeException setParameters failed
The error log shows that all camera.setParameters(parameters)
causes the crash.
Here's my code:
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);
}
}
Upvotes: 0
Views: 1200
Reputation: 1170
Instead of using static preview sizes try using supported preview sizes which are introduced in 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();
This will be more safer way to use this approach because all devices won't support manual sizes
Upvotes: 4
Reputation: 1161
Its throwing an error probably because of the device you are running/testing on.I had the same issue while working on QRScanner.
Try changing parameters.setPreviewFrameRate(60); and parameters.setPreviewSize(1920 , 1080); as per the device you are using. Happy Coding
Upvotes: 0