Android相机预览反向横向配置

时间:2013-12-26 08:33:09

标签: android camera android-camera

我想在反向横向模式下设置相机预览,当我通过清单方向设置尝试时,预览显示为镜像视图。我有一个覆盖相机预览,这就是为什么我想要反向横向模式,我使用的是android 4.0。

   class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
   SurfaceHolder mHolder;  // <2>
   public Camera camera; // <3>

     Preview(Context context) {
super(context);

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();  // <4>
mHolder.addCallback(this);  // <5>
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
  }


  @Override
  public void surfaceCreated(SurfaceHolder holder)
  {
      try {
            if (camera != null) {
                camera.setPreviewDisplay(holder);
            }
        } catch (IOException exception) {
            Log.e("fd", "IOException caused by setPreviewDisplay()", exception);
        }


       }  // Called when the holder is destroyed

     @Override
   public void surfaceDestroyed(SurfaceHolder holder) {  // <14>
   //camera.setParameters(Camera.Parameters.FLASH_MODE_OFF);
  Parameters params = camera.getParameters();
  params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
  camera.setParameters(params);
  camera.stopPreview();
  camera.release();
   //camera = null;
    }

 // Called when holder has changed

     @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
  camera = Camera.open();
  Parameters params = camera.getParameters();
  List<Size> sizes = params.getSupportedPictureSizes();
  Camera.Size size = sizes.get(0);
  params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
  params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
  params.setSceneMode(Camera.Parameters.SCENE_MODE_LANDSCAPE);
  params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
  params.setJpegThumbnailQuality(100);
  params.setExposureCompensation(0);
  params.setJpegQuality(100);

  // params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
  /*
   * 
   *  Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = // .... select one of previewSizes here

parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.startPreview();
   */

   //int w=0,h=0;
   for(int i=0;i<sizes.size();i++)
   {
       if(sizes.get(i).width > size.width)
           size = sizes.get(i);
   }
   params.setPictureSize(size.width, size.height);
   //Toast.makeText(getContext(),                 size.width+"and"+size.height,Toast.LENGTH_SHORT).show();

  camera.setParameters(params);

 /* if (this.getResources().getConfiguration().orientation !=        Configuration.ORIENTATION_LANDSCAPE)
  {
  params.set("orientation", "portrait");
  camera.setDisplayOrientation(90);
  }*/
  camera.startPreview();
  try
  {
  camera.setPreviewDisplay(holder);
  }
  catch (IOException exception)
  {
  camera.release();
  camera = null;
  }

  }

}

1 个答案:

答案 0 :(得分:0)

此答案的所有学分都转到Jason Robinson

您可以直接拨打camera.setDisplayOrientation(270);

,而不是使用setCameraDisplayOrientation()