2015-08-03 85 views
1

我们可以通过指定预览大小开放的Android相机,如:如何在android相机中交换预览尺寸的宽度和高度?

mParameter.setPreviewSize(640, 480); 
mCamera.setParameter(mParameter); 

以上的比例为1.3333333333333333

有什么办法来交换的宽度和高度?也就是说这样的效果:

mParameter.setPreviewSize(480, 640); 
mCamera.setParameter(mParameter); 

的比例为0.75

+1

尽管当然欢迎您随时调用'setPreviewSize()',否则会期望许多设备崩溃,并说这不是支持的预览大小。 – CommonsWare

+0

@CommonsWare是的。它会在很多设备上崩溃。那么有什么办法可以实现与第二个效果相同的效果? –

+0

您可以尝试在预览表面使用'TextureView'并将矩阵应用于'TextureView'来旋转预览帧。 – CommonsWare

回答

0

所有视频预览尺寸并非在所有设备都支持。你只能以编程方式进行。 如果大小是不适用的,然后扔媒体记录停止-19错误

int width = 320; 
      int height = 240; 
      try { 
       //get the available sizes of the video 
       List<Size> tmpList = getSupportedVideoSizes(); 

       final List<Size> sizeList = new Vector<Size>(); 

       // compare the apsect ratio of the candidate sizes against the 
       // real ratio 
       Double aspectRatio = (Double.valueOf(getWindowManager() 
         .getDefaultDisplay().getHeight())/getWindowManager() 
         .getDefaultDisplay().getWidth()); 
       for (int i = tmpList.size() - 1; i > 0; i--) { 
        Double tmpRatio = Double.valueOf(tmpList.get(i).height) 
          /tmpList.get(i).width; 
        if (EnableLog.LOG_TAG) { 
         Log.e("Width & height", tmpList.get(i).width + " x " 
           + tmpList.get(i).height); 
        } 
        if (Math.abs(aspectRatio - tmpRatio) < .15) { 
         width = tmpList.get(i).width; 
         height = tmpList.get(i).height; 
         sizeList.add(tmpList.get(i)); 
        } 
       } 
       if (EnableLog.LOG_TAG) { 
        Log.e("tmpList", tmpList + "*"); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


      // set the size of video. 
      // If the size is not applicable then throw the media recorder stop 
      // -19 error 
      mrec.setVideoSize(width, height); 

/** 
    * This method will return the size of the video 
    * 
    * if the getSupportedVideoSizes() is return null 
    * 
    * then it is possible to get the size of video from 
    * getSupportedPreviewSizes() 
    * 
    * @return list of size 
    */ 
    public List<Size> getSupportedVideoSizes() { 
      if (params.getSupportedVideoSizes() != null) { 
       return params.getSupportedVideoSizes(); 
      } else { 
       // Video sizes may be null, which indicates that all the supported 
       // preview sizes are supported for video recording. 
       return params.getSupportedPreviewSizes(); 
      } 
     } 

试试这个代码,也许会有所帮助。

编辑:

根据您的意见,你要那么你需要改变的循环来设定最佳预览大小。我会告诉你的例子

变化for循环

for (int i = tmpList.size() - 1; i > 0; i--) { 
} 

for (int i = 0 ; i < tmpList.size()-1; i++) { 
} 

这会给你更好的预览大小。

+0

不,我不是找到匹配最佳预览尺寸的方式,而是在交换宽度和高度后实现效果。 –

+0

此代码将设置您需要更改for循环以获得最佳预览大小的最低预览大小。我会更新我的问题。检查我的编辑。 – Amsheer

相关问题