12

I have a strange issue in my camera app when tested with galaxy nexus..It simply crash when try to start the camera activity..But it works fine with almost all other devices.. These are my functions.....

@Override
public void surfaceCreated(SurfaceHolder holder) {
 // TODO Auto-generated method stub

     try {
        camera = Camera.open();
        camera.setPreviewDisplay(holder);

    Camera.Parameters parameters = camera.getParameters();
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            setDisplayOrientation(camera, 90);
        else
            parameters.set("orientation", "portrait");
        parameters.setPictureFormat(PixelFormat.JPEG);
        camera.setParameters(parameters);



    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 


}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
  int height) {
 // TODO Auto-generated method stub
 if(previewing){
  camera.stopPreview();
  previewing = false;
 }

 if (camera != null){
  try {


   camera.setPreviewDisplay(surfaceHolder);
   camera.startPreview();
   previewing = true;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

Can anyone help me ?

hacker
  • 8,919
  • 12
  • 62
  • 108
  • 4
    Put the Exception/Crash logcat too. – Adil Soomro Sep 24 '12 at 08:45
  • @AdilSoomro sorry..unfortunately i dont have it now..i dont have the nexus phone also.. – hacker Sep 24 '12 at 08:48
  • probably becoz you are starting & stopping camera in surfaceChanged. Its better to startPreview in onResume() and stopPreview in onPause().Its working for my application over galaxy nexus. – Praween k Sep 24 '12 at 08:54
  • One more thing is that, i am adding "targetSDKVersion=15" in my manifest file..Also there is a statement "android:configChanges" in my activity declaration in manifest as "android:configChanges="keyboardHidden|orientation|screenLayout""..Is there any issue here ? – hacker Sep 24 '12 at 09:10
  • No this won't effect. For ScreenLayout, I'm not sure but for other options(keyboardHidden|orientation) as I have used these in my manifest too... – Praween k Sep 24 '12 at 09:16
  • see this post http://stackoverflow.com/questions/11630530/how-to-handle-screen-orientation-changes-when-there-is-an-asyntask-running-with/11631017#11631017 – hacker Sep 24 '12 at 09:18
  • @bugfinder: well! in that scene I would suggest you to download a galaxy nexus emulator and debug the app. :) – Adil Soomro Sep 24 '12 at 09:22
  • sorry to be ignoramus, how to download galaxy nexus emulator ? – hacker Sep 24 '12 at 09:30
  • i tried with nexus like emulator..but no crashes.. – hacker Oct 10 '12 at 08:17
  • @bugfinder I have Galaxy Nexus phone. If you'll create project, that I could quickly import to Eclipse, I can try to reproduce the issue and debug it. – vArDo Oct 10 '12 at 21:43
  • @vArDo pls get me your email id ? – hacker Oct 11 '12 at 06:21
  • @vArDo i can mail u the project.. – hacker Oct 11 '12 at 07:08
  • @bugfinder kjkjkjkj123123@gmail.com – vArDo Oct 11 '12 at 07:42
  • @hacker: hi, I'm facing issues regarding preview orientation and crash in ICS.I would be really grateful if you can email me your demo application at `mehuljoisar@gmail.com` – Mehul Joisar May 31 '13 at 13:38

1 Answers1

11

finally i got the answer..Nexus has both front and back end cameras.So the problem was the code which used to open the camera(need to set the camera id for the front end camera) and also we need to set the appropriate preview size..Here is the modified code..

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
  int height) {



    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, width, height);
    parameters.setPreviewSize(optimalSize.width, optimalSize.height);

    camera.setParameters(parameters);
    camera.startPreview();
     startPreview();

}


   @Override
public void surfaceCreated(SurfaceHolder holder) {


     try {

         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
              Camera.CameraInfo info=new Camera.CameraInfo();

              for (int i=0; i < Camera.getNumberOfCameras(); i++) {
                Camera.getCameraInfo(i, info);

                if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                  camera=Camera.open(i);
                  defaultCameraId = i;
                }
              }
            }

            if (camera == null) {
              camera=Camera.open();
            }




            try {
                camera.setPreviewDisplay(surfaceHolder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Camera.Parameters parameters = camera.getParameters();
            android.hardware.Camera.CameraInfo info =
                    new android.hardware.Camera.CameraInfo();
            android.hardware.Camera.getCameraInfo(defaultCameraId, info);
            int rotation = this.getWindowManager().getDefaultDisplay()
                    .getRotation();
            if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            {

                 int degrees = 0;
                 switch (rotation) {
                     case Surface.ROTATION_0: 
                         degrees = 0; break;
                     case Surface.ROTATION_90: 
                         degrees = 90; break;
                     case Surface.ROTATION_180: 
                         degrees = 180; break;
                     case Surface.ROTATION_270: 
                         degrees = 270; break;
                 }
                 int result;
                 if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                     result = (info.orientation + degrees) % 360;
                     result = (360 - result) % 360;  
                 } else {  // back-facing
                     result = (info.orientation - degrees + 360) % 360;
                 }
                 camera.setDisplayOrientation(result);

            }
            else
            {
                parameters.set("orientation", "portrait");
            }


            camera.setParameters(parameters);



    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 


}


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
                final double ASPECT_TOLERANCE = 0.1;
                double targetRatio = (double) w / h;
                if (sizes == null) return null;

                Size optimalSize = null;
                double minDiff = Double.MAX_VALUE;

                int targetHeight = h;


                for (Size size : sizes) {
                    double ratio = (double) size.width / size.height;
                    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }


                if (optimalSize == null) {
                    minDiff = Double.MAX_VALUE;
                    for (Size size : sizes) {
                        if (Math.abs(size.height - targetHeight) < minDiff) {
                            optimalSize = size;
                            minDiff = Math.abs(size.height - targetHeight);
                        }
                    }
                }
                return optimalSize;
            }
hacker
  • 8,919
  • 12
  • 62
  • 108