Android:支持的相机预览大小纵横比错误

Android:支持的相机预览大小纵横比错误,android,android-camera,surfaceview,Android,Android Camera,Surfaceview,解决方案:我实际上并没有设置相机预览,而是缩放它附带的默认预览。您需要将参数分配回相机,因为\u camera.getParameters.setPreviewSize()不起作用 我正在努力让Android摄像头在我的手机上预览(最好是全屏) 然而,我遇到了一种情况,我觉得硬件和软件都在与我作对:/ 我得到了一个支持摄像头的屏幕尺寸列表。对于一半的手机(我的12台设备中约有6台),当我将预览设置为与屏幕上最接近的预览匹配时,这一切都很好 但是,在另一半上,预览是拉伸和倾斜的。有许多帖子已经存在

解决方案:我实际上并没有设置相机预览,而是缩放它附带的默认预览。您需要将参数分配回相机,因为
\u camera.getParameters.setPreviewSize()
不起作用

我正在努力让Android摄像头在我的手机上预览(最好是全屏)

然而,我遇到了一种情况,我觉得硬件和软件都在与我作对:/

我得到了一个支持摄像头的屏幕尺寸列表。对于一半的手机(我的12台设备中约有6台),当我将预览设置为与屏幕上最接近的预览匹配时,这一切都很好

但是,在另一半上,预览是拉伸和倾斜的。有许多帖子已经存在并声称解决了这个问题,但没有

我发现的原因是:我从硬件本身获得的支持的摄像头屏幕尺寸列表是错误的。为什么?因为当手动将相机设置为其支持的最高尺寸之一时,纵横比仍处于关闭状态:

_camera.getParameters().setPreviewSize(720,1280);
getHolder().setFixedSize(720, 1280);
这款手机的屏幕尺寸为768 x 1280。支持的最接近屏幕大小为720 x 1280。将其设置为该值仍会导致图像失真。下一个支持的屏幕大小分辨率是1280 x 960。对我来说,这很有趣,因为它说它支持两种不同的纵横比,但其中一些“支持的”纵横比是倾斜的。i、 它说它支持4:3和16:9,但只有4:3看起来正常

getHolder().setFixedSize(720, 960); //fixes issue even though this is NOT a supported screen size
当我使用不受支持的屏幕尺寸720 x 960,强制曲面保持架匹配4:3的纵横比时,相机预览显示正常

我的问题是,在支持的纵横比不同于它所说的纵横比的相机中,我如何知道相机的纵横比是多少

更新

深入了解我的手机细节:

这款手机配有摄像头,可将图像保存为3264 x 2448。高宽比为4:3。我要看看是否可以从手机中获取这些数据,并使用这些数据设置所需的纵横比

更新2

使用

它给我640x480。再加上一点数学,它的纵横比是4:3。然后我使用它并适当地缩放我的相机

它很粗糙,但它完成了任务

//Because the camera LIES about supporting both 16:9 and 4:3 we must find the actual one from the picture size
void determineCameraAspectRatio()
{
    int cameraWidth = _camera.getParameters().getPictureSize().width;
    int cameraHeight = _camera.getParameters().getPictureSize().height;

    if( cameraWidth*3 == cameraHeight*4)
    {
        System.out.println("4:3");
    }

    if( cameraWidth*9 == cameraHeight*16)
    {
        System.out.println("16:9");
    }
}
并将修复应用于相机:

   if(_is4By3AspectRatio)
            {
                 height  = (Helper.screenWidthPX(_context) * 4)/3;
             }else//16:9
             {
                height = (Helper.screenWidthPX(_context) * 16)/9;
              }
                _camera.getParameters().setPreviewSize(Helper.screenWidthPX(_context),height);
                getHolder().setFixedSize(Helper.screenWidthPX(_context),height);
    }
更新

这适用于许多手机,但在某些手机上甚至是错误的。Nexus 6报告背面摄像头纵横比为4:3,其最高支持比为1440 x 1920,但将其设置为该值会导致图像倾斜。我完全不知道为什么。所有这些都没有意义。它说它希望图片的纵横比为4:3,但只能以16:9的比例(正面和背面)正确显示。真奇怪

这是surfaceFlinger上的系统转储

SurfaceFlinger
Build configuration: [sf] [libui] [libgui]
Sync configuration: [using: EGL_ANDROID_native_fence_sync EGL_KHR_wait_sync]
DispSync configuration: app phase 0 ns, sf phase 0 ns, present offset 0 ns (refresh 16666667 ns)
Visible layers (count = 10)
+ LayerDim 0xb8919c28 (DimLayer)
  Region transparentRegion (this=0xb8919d88, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb8919c30, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=        0, pos=(0,0), size=(  16,  16), crop=(   0,   0,  -1,  -1), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88ebac8
      format= 0, activeBuffer=[   0x   0:   0,  0], queued-frames=0, mRefreshPending=0
            mTexName=4 mCurrentTexture=-1
            mCurrentCrop=[49152,0,415,-1] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[1x1], default-format=1, transform-hint=00, FIFO(0)={}
+ LayerDim 0xb88659d0 (DimLayer)
  Region transparentRegion (this=0xb8865b30, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb88659d8, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=        0, pos=(0,0), size=(  16,  16), crop=(   0,   0,  -1,  -1), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88ebac8
      format= 0, activeBuffer=[   0x   0:   0,  0], queued-frames=0, mRefreshPending=0
            mTexName=5 mCurrentTexture=-1
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[1x1], default-format=1, transform-hint=00, FIFO(0)={}
+ LayerDim 0xb886d000 (DimLayer)
  Region transparentRegion (this=0xb886d160, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb886d008, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=        0, pos=(0,0), size=(  16,  16), crop=(   0,   0,  -1,  -1), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88ebac8
      format= 0, activeBuffer=[   0x   0:   0,  0], queued-frames=0, mRefreshPending=0
            mTexName=6 mCurrentTexture=-1
            mCurrentCrop=[-16843010,-16843010,-16843010,-16843010] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[1x1], default-format=1, transform-hint=00, FIFO(0)={}
+ Layer 0xb88f9580 (com.android.phasebeam.PhaseBeamWallpaper)
  Region transparentRegion (this=0xb88f96e0, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb88f9588, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=    21000, pos=(0,0), size=( 768,1280), crop=(   0,   0, 768,1280), isOpaque=1, invalidate=0, alpha=0xff, flags=0x00000003, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88591d0
      format= 2, activeBuffer=[ 768x1280:1024,  3], queued-frames=0, mRefreshPending=0
            mTexName=8 mCurrentTexture=1
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x1280], default-format=2, transform-hint=00, FIFO(0)={}
             [00:0xb8911cc8] state=FREE    , 0xb8868488 [ 768x1280:1024,  3]
            >[01:0xb88feb78] state=ACQUIRED, 0xb88fdc40 [ 768x1280:1024,  3]
             [02:0xb88ff1c0] state=FREE    , 0xb88fedf0 [ 768x1280:1024,  3]
+ Layer 0xb887db18 (SurfaceView)
  Region transparentRegion (this=0xb887dc78, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb887db20, count=1)
    [  0,   0, 768, 1024]
      layerStack=   0, z=    21025, pos=(0,0), size=( 768,1024), crop=(   0,   0, 768,1024), isOpaque=1, invalidate=0, alpha=0xff, flags=0x00000002, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb89124b8
      format= 4, activeBuffer=[ 640x 480: 640, 11], queued-frames=0, mRefreshPending=0
            mTexName=61 mCurrentTexture=3
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0x4
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x1024], default-format=4, transform-hint=00, FIFO(0)={}
             [00:0xb8923620] state=DEQUEUED, 0xb89000e0 [ 640x 480: 640, 11]
             [01:0xb88784c8] state=DEQUEUED, 0xb8878160 [ 640x 480: 640, 11]
             [02:0xb8878550] state=DEQUEUED, 0xb88785f0 [ 640x 480: 640, 11]
            >[03:0xb8877120] state=ACQUIRED, 0xb8878638 [ 640x 480: 640, 11]
             [04:0xb8913c48] state=DEQUEUED, 0xb886c8a0 [ 640x 480: 640, 11]
             [05:0xb88ff0f8] state=DEQUEUED, 0xb8879b08 [ 640x 480: 640, 11]
             [06:0xb8900930] state=DEQUEUED, 0xb88c3308 [ 640x 480: 640, 11]
+ Layer 0xb8862be8 (co.pumpup.app/co.pumpup.app.Activities.CameraEditorActivity)
  Region transparentRegion (this=0xb8862d48, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb8862bf0, count=1)
    [  0,   0, 768, 1280]
      layerStack=   0, z=    21030, pos=(0,0), size=( 768,1280), crop=(   0,   0, 768,1280), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000000, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb89124b8
      format= 1, activeBuffer=[ 768x1280: 768,  1], queued-frames=0, mRefreshPending=0
            mTexName=60 mCurrentTexture=2
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x1280], default-format=1, transform-hint=00, FIFO(0)={}
             [00:0xb88ffff8] state=FREE    , 0xb88ed258 [ 768x1280: 768,  1]
             [01:0xb88fe908] state=FREE    , 0xb88ff3d0 [ 768x1280: 768,  1]
            >[02:0xb8872348] state=ACQUIRED, 0xb88a6110 [ 768x1280: 768,  1]
+ Layer 0xb88befc0 (FocusedStackFrame)
  Region transparentRegion (this=0xb88bf120, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb88befc8, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=    21031, pos=(0,0), size=(   1,   1), crop=(   0,   0,  -1,  -1), isOpaque=0, invalidate=0, alpha=0x4d, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88ebac8
      format= 1, activeBuffer=[   0x   0:   0,  0], queued-frames=0, mRefreshPending=0
            mTexName=3 mCurrentTexture=-1
            mCurrentCrop=[524288,327680,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[1x1], default-format=1, transform-hint=00, FIFO(0)={}
+ LayerDim 0xb886f9c8 (DimLayer)
  Region transparentRegion (this=0xb886fb28, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb886f9d0, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=    22019, pos=(-192,-320), size=(1152,1920), crop=(   0,   0,  -1,  -1), isOpaque=0, invalidate=0, alpha=0x00, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88ebac8
      format= 0, activeBuffer=[   0x   0:   0,  0], queued-frames=0, mRefreshPending=0
            mTexName=7 mCurrentTexture=-1
            mCurrentCrop=[-67372037,-67372037,1919905635,-842203036] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[1152x1920], default-format=1, transform-hint=00, FIFO(0)={}
+ Layer 0xb8900d20 (StatusBar)
  Region transparentRegion (this=0xb8900e80, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb8900d28, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=   151000, pos=(0,0), size=( 768,  50), crop=(   0,   0, 768,  50), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88fea70
      format= 1, activeBuffer=[ 768x  50: 768,  1], queued-frames=0, mRefreshPending=0
            mTexName=10 mCurrentTexture=2
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x50], default-format=1, transform-hint=00, FIFO(0)={}
             [00:0xb887a088] state=FREE    , 0xb8873348 [ 768x  50: 768,  1]
             [01:0xb88fe2f8] state=FREE    , 0xb89004b8 [ 768x  50: 768,  1]
            >[02:0xb8879fd8] state=ACQUIRED, 0xb88773c8 [ 768x  50: 768,  1]
+ Layer 0xb8861748 (NavigationBar)
  Region transparentRegion (this=0xb88618a8, count=1)
    [  0,   0,   0,   0]
  Region visibleRegion (this=0xb8861750, count=1)
    [  0,   0,   0,   0]
      layerStack=   0, z=   201000, pos=(0,1184), size=( 768,  96), crop=(   0,   0, 768,  96), isOpaque=0, invalidate=0, alpha=0xff, flags=0x00000001, tr=[1.00, 0.00][0.00, 1.00]
      client=0xb88fea70
      format= 1, activeBuffer=[ 768x  96: 768,  1], queued-frames=0, mRefreshPending=0
            mTexName=11 mCurrentTexture=2
            mCurrentCrop=[0,0,0,0] mCurrentTransform=0
            mAbandoned=0
            -BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x96], default-format=1, transform-hint=00, FIFO(0)={}
             [00:0xb88fff08] state=FREE    , 0xb88fdf68 [ 768x  96: 768,  1]
             [01:0xb88c2920] state=FREE    , 0xb8862b88 [ 768x  96: 768,  1]
            >[02:0xb8913d00] state=ACQUIRED, 0xb88c5138 [ 768x  96: 768,  1]
Displays (1 entries)
+ DisplayDevice: Built-in Screen
   type=0, hwcId=0, layerStack=0, ( 768x1280), ANativeWindow=0xb885cd68, orient= 0 (type=00000000), flips=30060, isSecure=1, secureVis=0, powerMode=2, activeConfig=0, numLayers=2
   v:[0,0,768,1280], f:[0,0,768,1280], s:[0,0,768,1280],transform:[[1.000,0.000,-0.000][0.000,1.000,-0.000][0.000,0.000,1.000]]
mAbandoned=0
-BufferQueue mMaxAcquiredBufferCount=1, mDequeueBufferCannotBlock=0, default-size=[768x1280], default-format=1, transform-hint=00, FIFO(0)={}
 [00:0xb8913f80] state=DEQUEUED, 0xb88642e8 [ 768x1280: 768,  1]
>[01:0xb885fd80] state=ACQUIRED, 0xb8911608 [ 768x1280: 768,  1]
SurfaceFlinger global state:
EGL implementation : 1.4
EGL_QUALCOMM_shared_image EGL_KHR_image EGL_KHR_image_base EGL_QCOM_create_image EGL_KHR_lock_surface EGL_KHR_lock_surface2 EGL_KHR_fence_sync EGL_KHR_cl_eventEGL_KHR_cl_event2EGL_IMG_context_priority EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_gl_texture_3D_image EGL_KHR_gl_renderbuffer_image EGL_EXT_create_context_robustness EGL_ANDROID_blob_cache EGL_ANDROID_framebuffer_target EGL_KHR_create_context EGL_KHR_wait_sync EGL_KHR_gl_colorspace EGL_ANDROID_image_crop EGL_ANDROID_recordable EGL_ANDROID_native_fence_sync EGL_ANDROID_image_native_buffer EGL_KHR_gl_colorspace EGL_IMG_image_plane_attribs 
GLES: Qualcomm, Adreno (TM) 320, OpenGL ES 3.0 V@95.0 AU@  (GIT@Ia6306ec328)
GL_AMD_compressed_ATC_texture GL_AMD_performance_monitor GL_AMD_program_binary_Z400 GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_robustness GL_EXT_texture_format_BGRA8888 GL_EXT_texture_type_2_10_10_10_REV GL_NV_fence GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_depth24 GL_OES_EGL_image GL_OES_EGL_sync GL_OES_EGL_image_external GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_fragment_precision_high GL_OES_get_program_binary GL_OES_packed_depth_stencil GL_OES_depth_texture_cube_map GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_3D GL_OES_texture_float GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_texture_npot GL_OES_vertex_half_float GL_OES_vertex_type_10_10_10_2 GL_OES_vertex_array_object GL_QCOM_alpha_test GL_QCOM_binning_control GL_QCOM_driver_control GL_QCOM_perfmon_global_mode GL_QCOM_extended_get GL_QCOM_extended_get2 GL_QCOM_tiled_rendering GL_QCOM_writeonly_rendering GL_EXT_sRGB GL_EXT_sRGB_write_control GL_EXT_texture_sRGB_decode GL_EXT_multisampled_render_to_texture GL_EXT_color_buffer_float GL_EXT_color_buffer_half_float GL_EXT_disjoint_timer_query 
  Region undefinedRegion (this=0xb8859fe4, count=1)
    [  0, 1024, 768, 1280]
  orientation=0, isDisplayOn=1
  last eglSwapBuffers() time: 1251.335000 us
  last transaction time     : 305.204000 us
  transaction-flags         : 00000000
  refresh-rate              : 59.999999 fps
  x-dpi                     : 319.790009
  y-dpi                     : 318.744995
  gpu_to_cpu_unsupported    : 0
  eglSwapBuffers time: 0.000000 us
  transaction time: 0.000000 us
VSYNC state: disabled
  soft-vsync: disabled
  numListeners=14,
  events-delivered: 26757
    0xb88642c0: count=-1
    0xb88680e8: count=-1
    0xb88684d0: count=-1
    0xb886c8f8: count=-1
    0xb886c9b8: count=-1
    0xb886cf10: count=-1
    0xb8874450: count=-1
    0xb88ac618: count=-1
    0xb88c1658: count=-1
    0xb88fda88: count=-1
    0xb89005d0: count=-1
    0xb8912398: count=-1
    0xb89125d8: count=-1
    0xb8913e50: count=-1
h/w composer state:
  h/w composer present and enabled
Hardware Composer state (version 01020000):
  mDebugForceFakeVSync=0
  Display[0] configurations (* current):
    * 0: 768x1280, xdpi=319.790009, ydpi=318.744995, refresh=16666667
  numHwLayers=3, flags=00000000
    type   |  handle  | hint | flag | tr | blnd |   format    |     source crop (l,t,r,b)      |          frame         | name 
-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------
       HWC | b8878638 | 0002 | 0000 | 04 | 0100 | ? 00000011  |      0,      0,    640,    480 |    0,    0,  768, 1024 | SurfaceView
       HWC | b88a6110 | 0002 | 0000 | 00 | 0105 | RGBA_8888   |      0,      0,    768,   1280 |    0,    0,  768, 1280 | co.pumpup.app/co.pumpup.app.Activities.CameraEditorActivity
 FB TARGET | b8911608 | 0000 | 0000 | 00 | 0105 | RGBA_8888   |      0,      0,    768,   1280 |    0,    0,  768, 1280 | HWC_FRAMEBUFFER_TARGET
Qualcomm HWC state:
  MDPVersion=440
  DisplayPanel=8
HWC Map for Dpy: "PRIMARY" 
PREV_FRAME: layerCount: 2    mdpCount: 2                 cacheCount: 0 
CURR_FRAME: layerCount: 2    mdpCount: 2                 fbCount: 0 
needsFBRedraw: NO  pipesUsed: 2  MaxPipesPerMixer: 4 
 ---------------------------------------------  
 listIdx | cached? | mdpIndex | comptype  |  Z  
 ---------------------------------------------  
       0 |      NO |        0 |       MDP |  0 
       1 |      NO |        1 |       MDP |  1 


Overlay State
==========================
Ctrl(mdp_overlay) id=1 z=1 fg=0 alpha=255 mask=-1 flags=0x60000
    src(msmfb_img) w=768 h=1280 format=12 MDP_RGBA_8888
    src_rect(mdp_rect) x=0 y=0 w=768 h=1280
    dst_rect(mdp_rect) x=0 y=0 w=768 h=1280
Data(msmfb_overlay_data) id=1
    data(msmfb_data) offset=0 memid=90 id=0 flags=0x0 priv=0
Attached to dpy=0

Ctrl(mdp_overlay) id=3 z=0 fg=0 alpha=255 mask=-1 flags=0x940000
    src(msmfb_img) w=480 h=640 format=6 MDP_Y_CRCB_H2V2
    src_rect(mdp_rect) x=0 y=0 w=480 h=640
    dst_rect(mdp_rect) x=0 y=0 w=768 h=1024
Data(msmfb_overlay_data) id=3
    data(msmfb_data) offset=462848 memid=123 id=0 flags=0x0 priv=0
Attached to dpy=0

Pipes used=2

MdpRotCtrl(msm_rotator_img_info) sessid=3966423424 rot=4, enable=1 downscale=0
    src w=640 h=480 format=6 MDP_Y_CRCB_H2V2
    dst w=480 h=640 format=6 MDP_Y_CRCB_H2V2
    src_rect x=0 y=0 w=640 h=480
MdpRotData(msm_rotator_data_info) sessid=3966423424 verkey=0
    src offset=0 memid=98 id=0 flags=0x0 priv=0
    dst offset=462848 memid=123 id=0 flags=0x0 priv=0
    src_chroma offset=0 memid=0 id=0 flags=0x0 priv=0
    dst_chroma offset=0 memid=0 id=0 flags=0x0 priv=0

================
Allocated buffers:
0xb8862b88:  288.00 KiB |  768 ( 768) x   96 |        1 | 0x00000900
0xb88642e8: 3840.00 KiB |  768 ( 768) x 1280 |        1 | 0x00001a00
0xb8868488: 3840.00 KiB |  768 (1024) x 1280 |        3 | 0x00000900
0xb886c8a0: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb8873348:  150.00 KiB |  768 ( 768) x   50 |        1 | 0x00000900
0xb88773c8:  150.00 KiB |  768 ( 768) x   50 |        1 | 0x00000900
0xb8878160: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb88785f0: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb8878638: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb8879b08: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb88a6110: 3840.00 KiB |  768 ( 768) x 1280 |        1 | 0x00000900
0xb88c3308: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb88c5138:  288.00 KiB |  768 ( 768) x   96 |        1 | 0x00000900
0xb88ed258: 3840.00 KiB |  768 ( 768) x 1280 |        1 | 0x00000900
0xb88fdc40: 3840.00 KiB |  768 (1024) x 1280 |        3 | 0x00000900
0xb88fdf68:  288.00 KiB |  768 ( 768) x   96 |        1 | 0x00000900
0xb88fedf0: 3840.00 KiB |  768 (1024) x 1280 |        3 | 0x00000900
0xb88ff3d0: 3840.00 KiB |  768 ( 768) x 1280 |        1 | 0x00000900
0xb89000e0: unknown     |  640 ( 640) x  480 |       11 | 0x50020900
0xb89004b8:  150.00 KiB |  768 ( 768) x   50 |        1 | 0x00000900
0xb8911608: 3840.00 KiB |  768 ( 768) x 1280 |        1 | 0x00001a00
Total allocated (estimate): 32034.00 KB
//摄像机逻辑

private void adjustAspectRatio()
    {
        List<Camera.Size> sizesOfPreviews = _camera.getParameters().getSupportedPreviewSizes();
        Camera.Size size = getOptimalPreviewSize(sizesOfPreviews,Helper.screenWidthPX(_context),Helper.screenHeightPX(_context));

        int cameraWidth = size.height;
        int cameraHeight = size.width;

        System.out.println("Optimal camera size is " + cameraWidth + " x " + cameraHeight);
        _camera.getParameters().setPreviewSize(cameraWidth, cameraHeight);
        _parentLayout.getLayoutParams().width = 768;
        _parentLayout.getLayoutParams().height = 1024;
        //_parentLayout.setAspectRatio((double)cameraWidth/cameraHeight);
    }

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

        if (sizes == null)
            return null;

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

        int targetHeight = h;

        for (Camera.Size size : sizes) {
            double ratio = (double) size.height / size.width;
            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 (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }

        return optimalSize;
    }
private void adjustAspectRatio()
{
列表大小预览=_camera.getParameters().getSupportedPreviewSizes();
Camera.Size Size=getOptimalPreviewSize(sizeOffReviews,Helper.screenWidthPX(_上下文),Helper.screenHeightPX(_上下文));
int cameraWidth=size.height;
int cameraHeight=size.width;
System.out.println(“最佳摄像机尺寸为”+摄像机宽度+“x”+摄像机高度);
_camera.getParameters().setPreviewSize(cameraWidth,cameraHeight);
_parentLayout.getLayoutParams().width=768;
_parentLayout.getLayoutParams().height=1024;
//_parentLayout.setAspectRatio((双)cameraWidth/cameraHeight);
}
私人相机。大小getOptimalPreviewSize(列表大小,整数w,整数h)
{
最终双纵横比公差=0.1;
双目标率=(双)高/低;
如果(大小==null)
返回null;
照相机。大小优化大小=空;
double minDiff=double.MAX_值;
int targetHeight=h;
用于(相机尺寸:尺寸){
双倍比率=(双倍)size.height/size.width;
if(数学绝对值(比率-目标比率)>纵横比公差)
继续;
if(数学绝对值(尺寸高度-目标光)
使用
setFixedSize()
在这里没有意义。它设置曲面本身的大小,而不是视图的大小,但对于摄影机预览,曲面的大小由来自摄影机的帧确定。使用adb shell dumpsys SurfaceFlinger在显示曲面时,将曲面的大小与SurfaceView窗口的大小进行比较(有关转储中HWC摘要的更多信息,请参阅)。请参阅Grafika的AspectFrameLayout或基于TextureView的视频播放器,了解纵横比校正。嗯,但这适用于95%的手机。但在Nexus6上,它不适用于背面摄像头,因此我的逻辑存在缺陷。nexus 6支持的最大预览大小为1920 x 1440,但将曲面设置为这些值会导致图像倾斜。我一直在看Grafika的东西,但我想我可以通过设置surfaceHolder的大小而不是frameLayout的大小来省去麻烦。有两种大小,“surface”大小和“view”大小
setFixedSize()
设置曲面大小,这在绘制OpenGL ES或画布时非常有用,因为它设置了绘制到的缓冲区的大小。来自摄像机或解码视频的帧在不复制或修改的情况下传递,而不是按比例复制到缓冲区中,因此设置
private void adjustAspectRatio()
    {
        List<Camera.Size> sizesOfPreviews = _camera.getParameters().getSupportedPreviewSizes();
        Camera.Size size = getOptimalPreviewSize(sizesOfPreviews,Helper.screenWidthPX(_context),Helper.screenHeightPX(_context));

        int cameraWidth = size.height;
        int cameraHeight = size.width;

        System.out.println("Optimal camera size is " + cameraWidth + " x " + cameraHeight);
        _camera.getParameters().setPreviewSize(cameraWidth, cameraHeight);
        _parentLayout.getLayoutParams().width = 768;
        _parentLayout.getLayoutParams().height = 1024;
        //_parentLayout.setAspectRatio((double)cameraWidth/cameraHeight);
    }

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

        if (sizes == null)
            return null;

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

        int targetHeight = h;

        for (Camera.Size size : sizes) {
            double ratio = (double) size.height / size.width;
            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 (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }

        return optimalSize;
    }