Android: Supported Camera Preview Size Is Wrong Aspect Ratio

4.5k views Asked by At

Solution: I was not actually setting the camera preview but rather scaling the default preview it came with. You need to assign the parameters back to the camera, because _camera.getParamters.setPreviewSize() does not work!

I am working on getting Android cameras to preview on my phone (ideally fullscreen).

However I've run into a situation where I feel both the hardware and software are conspiring against me :/

I get a list of camera supported screen sizes. For half the phones (about 6 of my 12 devices) this works fine when I set the preview match the closest preview for the screen.

However on the other half, the preview is stretched and skewed. There are a number of posts that exist and claim to address this issue but do not.

The reason I am finding is this: the list of supported camera screen sizes that I am getting from the hardware itself is wrong. Why? Because when manually set the camera up to one of its highest supported sizes the aspect ratio is still off:

_camera.getParameters().setPreviewSize(720,1280);
getHolder().setFixedSize(720, 1280);

For this phone, the screen size is 768 x 1280. The closest supported screen size is 720 x 1280. Setting it to this value still results in a distorted image. The next supported screen size resolution is 1280 x 960. Which to me, is interesting, because its saying its supporting 2 different aspect ratios, but some of these 'supported' aspect ratios are skewed. i.e. it says its supports 4:3 and 16:9, but only the 4:3 comes out looking normal!

getHolder().setFixedSize(720, 960); //fixes issue even though this is NOT a supported screen size

When I force the surface holder to match a 4:3 aspect ratio, by using the non supported screen size, 720 x 960, voila, the camera preview comes out normal.

My question is, how am I supposed to know what the aspect ratio is for the camera in a camera with supported aspect ratios different than what it says?

Update

Digging into my phone specifics:

http://www.gsmarena.com/lg_nexus_4_e960-5048.php

This phone has a camera which saves images as 3264 x 2448. A 4:3 aspect ratio. I am going to see if I can fetch this data from the phone and use THAT for setting the desired aspect ratio.

Update 2

Using

System.out.println("Camera picture size is " + _camera.getParameters().getPictureSize().width + " x " + _camera.getParameters().getPictureSize().height);

It gives me 640 x 480. Which with a little maths is a 4:3 aspect ratio. I then use this and scale my camera appropriately.

It's hacky, but it gets the job done!

//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");
    }
}

And apply the fix to the camera:

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

Update

This works for a lot of phones but on some even THIS is wrong. The Nexus 6 reports a back facing camera aspect ratio of 4:3 and its highest supported ratio is 1440 x 1920 but setting it to this results in a skewed image. I have absolutely no idea why. None of this makes sense. It says it wants a 4:3 aspect ratio for the picture, but only display properly with a 16:9 ratio (front and back). SO WEIRD

Here is the sysdump on the 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 [email protected] 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

//Camera logic

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

There are 0 answers