Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there should be some problem with setting preview parameters. Try below.</p> <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (isPreviewRunning) { camera.stopPreview(); } try{ Camera.Parameters p = camera.getParameters(); if(p!=null){ List&lt;Size&gt; sizes = p.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, w, h); p.setPreviewSize(optimalSize.width, optimalSize.height); camera.setParameters(p); camera.setPreviewDisplay(holder);; camera.startPreview(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } isPreviewRunning = true; } private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) { // TODO Auto-generated method stub final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) w / h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; // Try to find an size match aspect ratio and size for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) &lt; minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) &lt; minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (isPreviewRunning) { camera.stopPreview(); } try{ Camera.Parameters p = camera.getParameters(); if(p!=null){ List&lt;Size&gt; sizes = p.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, w, h); p.setPreviewSize(optimalSize.width, optimalSize.height); camera.setParameters(p); camera.setPreviewDisplay(holder);; camera.startPreview(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } isPreviewRunning = true; } private Size getOptimalPreviewSize(List sizes, int w, int h) { // TODO Auto-generated method stub final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) w / h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; // Try to find an size match aspect ratio and size for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) &lt; minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) &lt; minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } </code></pre> <p>Put this code in your surfaceChanged(). getOptimalPreviewSize() is used to set preview parameters according to device resolution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload