Beispiel #1
1
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null) {
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
      mCamera.setPreviewDisplay(mHolder);

      // reference former
      Camera.Parameters parameters = mCamera.getParameters();
      int capWidth = parameters.getPreviewSize().width;
      int capHeight = parameters.getPreviewSize().height;
      // android.graphics.imageformat
      int pixelformat = parameters.getPreviewFormat();
      PixelFormat pixelinfo = new PixelFormat();
      PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);
      int cameraIndex = 0;
      boolean frontFacing = false;

      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      Camera.getCameraInfo(cameraIndex, cameraInfo);
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        frontFacing = true;
      }
      // For the default NV21 format, bitsPerPixel = 12.
      int bufSize = capWidth * capHeight * pixelinfo.bitsPerPixel / 8;

      for (int i = 0; i < 5; i++) {
        mCamera.addCallbackBuffer(new byte[bufSize]);
      }

      mCamera.startPreview();

      nftSimpleActivity.nativeVideoInit(capWidth, capHeight, cameraIndex, frontFacing);
    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
Beispiel #2
0
  private int tryStartCapture(int width, int height, int frameRate) {
    if (camera == null) {
      Log.e(TAG, "Camera not initialized %d" + id);
      return -1;
    }

    Log.d(
        TAG,
        "tryStartCapture: "
            + width
            + "x"
            + height
            + ", frameRate: "
            + frameRate
            + ", isCaptureRunning: "
            + isCaptureRunning
            + ", isSurfaceReady: "
            + isSurfaceReady
            + ", isCaptureStarted: "
            + isCaptureStarted);

    if (isCaptureRunning || !isCaptureStarted) {
      return 0;
    }

    CaptureCapabilityAndroid currentCapability = new CaptureCapabilityAndroid();
    currentCapability.width = width;
    currentCapability.height = height;
    currentCapability.maxFPS = frameRate;
    PixelFormat.getPixelFormatInfo(PIXEL_FORMAT, pixelFormat);

    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(currentCapability.width, currentCapability.height);
    parameters.setPreviewFormat(PIXEL_FORMAT);
    parameters.setPreviewFrameRate(currentCapability.maxFPS);
    try {
      camera.setParameters(parameters);
    } catch (RuntimeException e) {
      Log.e(TAG, "setParameters failed", e);
      return -1;
    }

    int bufSize = width * height * pixelFormat.bitsPerPixel / 8;
    byte[] buffer = null;
    for (int i = 0; i < numCaptureBuffers; i++) {
      buffer = new byte[bufSize];
      camera.addCallbackBuffer(buffer);
    }
    camera.setPreviewCallbackWithBuffer(this);
    ownsBuffers = true;

    camera.startPreview();
    previewBufferLock.lock();
    expectedFrameSize = bufSize;
    isCaptureRunning = true;
    previewBufferLock.unlock();

    return 0;
  }
  @SuppressLint("NewApi") // CameraInfo
  @SuppressWarnings("deprecation") // setPreviewFrameRate
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    if (camera != null) {

      String camResolution =
          PreferenceManager.getDefaultSharedPreferences(getContext())
              .getString(
                  "pref_cameraResolution",
                  getResources().getString(R.string.pref_defaultValue_cameraResolution));
      String[] dims = camResolution.split("x", 2);
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPreviewSize(Integer.parseInt(dims[0]), Integer.parseInt(dims[1]));
      parameters.setPreviewFrameRate(30);
      camera.setParameters(parameters);

      parameters = camera.getParameters();
      int capWidth = parameters.getPreviewSize().width;
      int capHeight = parameters.getPreviewSize().height;
      int pixelformat = parameters.getPreviewFormat(); // android.graphics.imageformat
      PixelFormat pixelinfo = new PixelFormat();
      PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);
      int cameraIndex = 0;
      boolean frontFacing = false;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraIndex =
            Integer.parseInt(
                PreferenceManager.getDefaultSharedPreferences(getContext())
                    .getString("pref_cameraIndex", "0"));
        Camera.getCameraInfo(cameraIndex, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) frontFacing = true;
      }

      int bufSize =
          capWidth
              * capHeight
              * pixelinfo.bitsPerPixel
              / 8; // For the default NV21 format, bitsPerPixel = 12.

      for (int i = 0; i < 5; i++) camera.addCallbackBuffer(new byte[bufSize]);

      camera.startPreview();

      nftBookActivity.nativeVideoInit(capWidth, capHeight, cameraIndex, frontFacing);
    }
  }
  public void setCallback() {
    int bufferSize = 0;
    int pformat;
    int bitsPerPixel;

    pformat = mCamera.getParameters().getPreviewFormat();

    PixelFormat info = new PixelFormat();
    PixelFormat.getPixelFormatInfo(pformat, info);
    bitsPerPixel = info.bitsPerPixel;

    bufferSize = mPreviewWidth * mPreviewHeight * bitsPerPixel / 8;

    mPreviewBuffer = null;

    mPreviewBuffer = new byte[bufferSize + 4096];

    mCamera.addCallbackBuffer(mPreviewBuffer);
    mCamera.setPreviewCallbackWithBuffer(mCameraCallback);
  }
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    w = optimalSize.width;
    h = optimalSize.height;
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(w, h);
    mCamera.setParameters(parameters);
    PixelFormat p = new PixelFormat();
    PixelFormat.getPixelFormatInfo(parameters.getPreviewFormat(), p);

    // Must call this before calling addCallbackBuffer to get all the
    // reflection variables setup
    initAddCallbackBufferMethod();
    int bufSize = ((h * w) * p.bitsPerPixel) / 8;
    Log.d(
        "AR",
        "Camera parameters: Size: "
            + bufSize
            + ", Height: "
            + h
            + ", Width: "
            + w
            + ", pixelformat: "
            + p.toString());
    if (first) {
      myThread.setImageSizeAndRun(h, w);

      first = false;
    }
    // Add a buffer for the detection. This will be added to the queue again once it is free to use
    // otherwise the frames will be discarded.
    byte[] buffer = new byte[bufSize];
    addCallbackBuffer(buffer);

    setPreviewCallbackWithBuffer();
    //        mCamera.setOneShotPreviewCallback(this);

    mCamera.startPreview();
  }
Beispiel #6
0
  public static CapabilitiesImmutable fixCaps(
      boolean matchFormatPrecise, int format, CapabilitiesImmutable rCaps) {
    PixelFormat pf = new PixelFormat();
    PixelFormat.getPixelFormatInfo(format, pf);
    final CapabilitiesImmutable res;
    int r, g, b, a;

    switch (format) {
      case PixelFormat.RGBA_8888:
        r = 8;
        g = 8;
        b = 8;
        a = 8;
        break;
      case PixelFormat.RGBX_8888:
        r = 8;
        g = 8;
        b = 8;
        a = 0;
        break;
      case PixelFormat.RGB_888:
        r = 8;
        g = 8;
        b = 8;
        a = 0;
        break;
      case PixelFormat.RGB_565:
        r = 5;
        g = 6;
        b = 5;
        a = 0;
        break;
      case PixelFormat.RGBA_5551:
        r = 5;
        g = 5;
        b = 5;
        a = 1;
        break;
      case PixelFormat.RGBA_4444:
        r = 4;
        g = 4;
        b = 4;
        a = 4;
        break;
      case PixelFormat.RGB_332:
        r = 3;
        g = 3;
        b = 2;
        a = 0;
        break;
      default:
        throw new InternalError("Unhandled pixelformat: " + format);
    }
    final boolean change =
        matchFormatPrecise
            || rCaps.getRedBits() > r
                && rCaps.getGreenBits() > g
                && rCaps.getBlueBits() > b
                && rCaps.getAlphaBits() > a;

    if (change) {
      Capabilities nCaps = (Capabilities) rCaps.cloneMutable();
      nCaps.setRedBits(r);
      nCaps.setGreenBits(g);
      nCaps.setBlueBits(b);
      nCaps.setAlphaBits(a);
      res = nCaps;
    } else {
      res = rCaps;
    }
    Log.d(MD.TAG, "fixCaps:    format: " + format);
    Log.d(MD.TAG, "fixCaps: requested: " + rCaps);
    Log.d(MD.TAG, "fixCaps:    chosen: " + res);

    return res;
  }
  @SuppressWarnings("deprecation") // setPreviewFrameRate, getPreviewFrameRate
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    if (camera == null) {
      // Camera wasn't opened successfully?
      Log.e(TAG, "No camera in surfaceChanged");
      return;
    }

    Log.i(TAG, "Surfaced changed, setting up camera and starting preview");

    String camResolution =
        PreferenceManager.getDefaultSharedPreferences(getContext())
            .getString(
                "pref_cameraResolution",
                getResources().getString(R.string.pref_defaultValue_cameraResolution));
    String[] dims = camResolution.split("x", 2);
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(Integer.parseInt(dims[0]), Integer.parseInt(dims[1]));
    parameters.setPreviewFrameRate(30);
    camera.setParameters(parameters);

    parameters = camera.getParameters();
    captureWidth = parameters.getPreviewSize().width;
    captureHeight = parameters.getPreviewSize().height;
    captureRate = parameters.getPreviewFrameRate();
    int pixelformat = parameters.getPreviewFormat(); // android.graphics.imageformat
    PixelFormat pixelinfo = new PixelFormat();
    PixelFormat.getPixelFormatInfo(pixelformat, pixelinfo);
    int cameraIndex = 0;
    boolean cameraIsFrontFacing = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      cameraIndex =
          Integer.parseInt(
              PreferenceManager.getDefaultSharedPreferences(getContext())
                  .getString("pref_cameraIndex", "0"));
      Camera.getCameraInfo(cameraIndex, cameraInfo);
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) cameraIsFrontFacing = true;
    }

    mParent.onCameraConfigured(captureWidth, captureHeight, w, h);

    int bufSize =
        captureWidth
            * captureHeight
            * pixelinfo.bitsPerPixel
            / 8; // For the default NV21 format, bitsPerPixel = 12.
    Log.i(
        TAG,
        "Camera buffers will be "
            + captureWidth
            + "x"
            + captureHeight
            + "@"
            + pixelinfo.bitsPerPixel
            + "bpp, "
            + bufSize
            + "bytes.");
    cameraWrapper = new CameraWrapper(camera);
    cameraWrapper.configureCallback(
        this, true, 10, bufSize); // For the default NV21 format, bitsPerPixel = 12.

    camera.startPreview();

    if (listener != null)
      listener.cameraPreviewStarted(
          captureWidth, captureHeight, captureRate, cameraIndex, cameraIsFrontFacing);
  }