Exemplo n.º 1
0
/*
 * Query the camera to retrieve the supported video frame sizes
 * and also to see whether CameraParameters::setVideoSize()
 * is supported or not.
 * @param params CameraParameters to retrieve the information
 * @@param isSetVideoSizeSupported retunrs whether method
 *      CameraParameters::setVideoSize() is supported or not.
 * @param sizes returns the vector of Size objects for the
 *      supported video frame sizes advertised by the camera.
 */
static void getSupportedVideoSizes(
    const CameraParameters& params,
    bool *isSetVideoSizeSupported,
    Vector<Size>& sizes) {

    *isSetVideoSizeSupported = true;
    params.getSupportedVideoSizes(sizes);
    if (sizes.size() == 0) {
        ALOGD("Camera does not support setVideoSize()");
        params.getSupportedPreviewSizes(sizes);
        *isSetVideoSizeSupported = false;
    }
}
NS_IMETHODIMP
GonkCameraInputStream::Init(nsACString& aContentType, nsCaptureParams* aParams)
{
  if (XRE_GetProcessType() != GeckoProcessType_Default)
    return NS_ERROR_NOT_IMPLEMENTED;

  mContentType = aContentType;
  mWidth = aParams->width;
  mHeight = aParams->height;
  mCamera = aParams->camera;

  PRUint32 maxNumCameras = getNumberOfCameras();

  if (maxNumCameras == 0)
    return NS_ERROR_FAILURE;

  if (mCamera >= maxNumCameras)
    mCamera = 0;

  mHardware = CameraHardwareInterface::openCamera(mCamera);

  if (!mHardware)
    return NS_ERROR_FAILURE;

  mHardware->setCallbacks(NULL, GonkCameraInputStream::DataCallback, NULL, this);

  mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);

  CameraParameters params = mHardware->getParameters();

  printf_stderr("Preview format : %s\n", params.get(params.KEY_SUPPORTED_PREVIEW_FORMATS));

  Vector<Size> previewSizes;
  params.getSupportedPreviewSizes(previewSizes);

  // find the available preview size closest to the requested size.
  PRUint32 minSizeDelta = PR_UINT32_MAX;
  PRUint32 bestWidth = mWidth;
  PRUint32 bestHeight = mHeight;
  for (PRUint32 i = 0; i < previewSizes.size(); i++) {
    Size size = previewSizes[i];
    PRUint32 delta = abs(size.width * size.height - mWidth * mHeight); 
    if (delta < minSizeDelta) {
      minSizeDelta = delta;
      bestWidth = size.width;
      bestHeight = size.height;
    }
  }
  mWidth = bestWidth;
  mHeight = bestHeight;
  params.setPreviewSize(mWidth, mHeight);

  // try to set preferred image format
  params.setPreviewFormat("yuv420p");

  params.setPreviewFrameRate(mFps);
  mHardware->setParameters(params);
  params = mHardware->getParameters();
  mFps = params.getPreviewFrameRate();

  mIs420p = !strcmp(params.getPreviewFormat(), "yuv420p");

  mHardware->startPreview();

  mClosed = false;
  return NS_OK;
}