【问题标题】:camera crash unexpectedly in galaxy nexus银河系中的相机意外崩溃
【发布时间】:2012-09-24 06:50:05
【问题描述】:

在使用 Galaxy nexus 进行测试时,我的相机应用程序出现了一个奇怪的问题。它在尝试启动相机活动时简单地崩溃了。但它几乎适用于所有其他设备。 这些是我的功能.....

@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();
  }
 }
}

谁能帮忙 我?

【问题讨论】:

  • 把 Exception/Crash logcat 也放上去。
  • @AdilSoomro 抱歉..不幸的是我现在没有它..我也没有 nexus 手机..
  • 可能是因为您正在surfaceChanged 中启动和停止相机。最好在 onResume() 中启动Preview 并在 onPause() 中停止Preview。它适用于我在 Galaxy Nexus 上的应用程序。
  • 还有一件事是,我在我的清单文件中添加了“targetSDKVersion=15”..我的清单中的活动声明中还有一个声明“android:configChanges”为“android:configChanges= "keyboardHidden|orientation|screenLayout""..这里有什么问题吗?
  • 不,这不会影响。对于 ScreenLayout,我不确定,但对于其他选项(键盘隐藏|方向),因为我也在清单中使用了这些选项......

标签: android camera


【解决方案1】:

终于我得到了答案..Nexus 有前端和后端摄像头。所以问题是用于打开摄像头的代码(需要为前端摄像头设置摄像头 ID),我们还需要设置适当的预览大小..这是修改后的代码..

@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;
            }

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多