2011-12-19 62 views
2

我使用HTC Wildfire和Android 2.2.1測試我的應用程序。 默認相機預覽尺寸爲768x432。我從設備日誌中看到它:設置相機預覽緩衝區大小

12-19 18:41:34.088: D/QualcommCameraHardware(72): initPreview E: preview size=768x432 
12-19 18:41:34.088: D/QualcommCameraHardware(72): initPreview E: preview size=768x432 videosize = 768 x 432 
12-19 18:41:34.088: D/QualcommCameraHardware(72): mBufferSize=497664, mAlignedBufferSize=524288 

好的,它的工作原理。但是,如果我嘗試將預覽大小更改爲320x240或800x480等不同的值,屏幕上只會出現一些像素。這裏最有趣的事情是,如果我使用預覽與緩衝器大小時,則默認的(497664)我在日誌中出現此錯誤:

12-19 18:52:49.288: E/Camera-JNI(5776): Manually set buffer was too small! Expected 497664 bytes, but got 144000! 

所以很明顯,實際緩衝區大小沒有改變。有什麼辦法可以解決這個問題嗎?

我也應該注意到,有與Android的華碩Eee Pad的變壓器沒有這樣的問題3.2.1

回答

4

我已經想清楚是什麼問題了。 看來,在Android 2.2.1你必須設置相機參數之前,您撥打

camera.startPreview() 

但是對後來的系統如Android 3.2.1這不再是一個問題(但也可能是供應商特定的)。

0

在Android攝像頭是一種巫術的。我會建議看其預覽尺寸實際上是在支持(當然,至少通告):

 Camera.Parameters cameraParameters = camera.getParameters(); 
     // print out parameters 
     Log.d(LOG_TAG, "flash modes:" + cameraParameters.getSupportedFlashModes()); 
     Log.d(LOG_TAG, "autofocus:" + cameraParameters.getSupportedFocusModes()); 
     Log.d(LOG_TAG, "preview formats:" + cameraParameters.getSupportedPreviewFormats()); 
     Log.d(LOG_TAG, "scene modes:" + cameraParameters.getSupportedSceneModes()); 
     Log.d(LOG_TAG, "white balance modes:" + cameraParameters.getSupportedWhiteBalance()); 

不幸的是,不是所有的預覽大小quaranted工作,即使他們是在此列表中。至少不是摩托羅拉無畏或HTC英雄。所以我確實如下:

  // as we know, that big preview size can produce RE on HTC Hero, 
     // we just iterate through allowed preview sizes until we find something proper 
     // go for maximal allowed preview size 

     previewSize = cameraParameters.getPreviewSize(); 

     if (previewSize.width <= MIN_PREVIEW_WIDTH) { 
      Log.d(LOG_TAG, "preview size is too small:" + previewSize.width + "x" + previewSize.height); 
      final List<Camera.Size> sizes = cameraParameters.getSupportedPreviewSizes(); 
      Collections.sort(sizes, new Comparator<Camera.Size>() { 
       public int compare(Camera.Size o1, Camera.Size o2) { 
        return new Integer(o2.width).compareTo(o1.width); 
       } 
      }); 

      for (Camera.Size size : sizes) { 
       cameraParameters.setPreviewSize(size.width, size.height); 
       camera.setParameters(cameraParameters); 
       Log.d(LOG_TAG, "attempt preview size:" + size.width + "x" + size.height); 
       try { 
        camera.startPreview(); 
        Log.d(LOG_TAG, "...accepted - go along"); 
        // ok, camera accepted out settings. since we know, 
        // that some implementations may choose different preview format, 
        // we retrieve parameters again. just to be sure 
        cameraParameters = camera.getParameters(); 
        break; 
       } catch (RuntimeException rx) { 
        // ups, camera did not like this size 
        Log.d(LOG_TAG, "...barfed, try next"); 
       } 

      } 
     } else { 
      Log.d(LOG_TAG, " accepted default preview size on the spot:" + previewSize.width + "x" + previewSize.height); 
      camera.startPreview(); 
     } 
+0

- 但我得到的異常而設定的,有時在預覽回調如果預覽大小太大了 – Max 2011-12-19 19:15:47

+0

我做同樣的 – 2011-12-19 19:26:57

0

我對2.2版本的解決方案:

Camera.PictureCallback photoCallback=new Camera.PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
mCamera.addCallbackBuffer(data); 
mCamera.startPreview(); 

http://developer.android.com/reference/android/hardware/Camera.html#addCallbackBuffer%28byte[]%29 
     } 
       }; 
我實際使用cameraParameters.getSupportedPreviewSizes()