Java Code Examples for android.hardware.Camera#addCallbackBuffer()

The following examples show how to use android.hardware.Camera#addCallbackBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CameraSource.java    From samples-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the frame data received from the camera.  This adds the previous unused frame buffer
 * (if present) back to the camera, and keeps a pending reference to the frame data for
 * future use.
 */
void setNextFrame(byte[] data, Camera camera) {
    synchronized (mLock) {
        if (mPendingFrameData != null) {
            camera.addCallbackBuffer(mPendingFrameData.array());
            mPendingFrameData = null;
        }

        if (!mBytesToByteBuffer.containsKey(data)) {
            Log.d(TAG,
                "Skipping frame.  Could not find ByteBuffer associated with the image " +
                "data from the camera.");
            return;
        }

        // Timestamp and frame ID are maintained here, which will give downstream code some
        // idea of the timing of frames received and when frames were dropped along the way.
        mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
        mPendingFrameId++;
        mPendingFrameData = mBytesToByteBuffer.get(data);

        // Notify the processor thread if it is waiting on the next frame (see below).
        mLock.notifyAll();
    }
}
 
Example 2
Source File: CameraSource.java    From Barcode-Reader with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the frame data received from the camera.  This adds the previous unused frame buffer
 * (if present) back to the camera, and keeps a pending reference to the frame data for
 * future use.
 */
void setNextFrame(byte[] data, Camera camera) {
    synchronized (mLock) {
        if (mPendingFrameData != null) {
            camera.addCallbackBuffer(mPendingFrameData.array());
            mPendingFrameData = null;
        }

        if (!mBytesToByteBuffer.containsKey(data)) {
            Log.d(TAG,
                    "Skipping frame.  Could not find ByteBuffer associated with the image " +
                            "data from the camera.");
            return;
        }

        // Timestamp and frame ID are maintained here, which will give downstream code some
        // idea of the timing of frames received and when frames were dropped along the way.
        mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
        mPendingFrameId++;
        mPendingFrameData = mBytesToByteBuffer.get(data);

        // Notify the processor thread if it is waiting on the next frame (see below).
        mLock.notifyAll();
    }
}
 
Example 3
Source File: CameraSource.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
/**
 * Sets the frame data received from the camera.  This adds the previous unused frame buffer
 * (if present) back to the camera, and keeps a pending reference to the frame data for
 * future use.
 */
private void setNextFrame(byte[] data, Camera camera) {
    synchronized (mLock) {
        if (mPendingFrameData != null) {
            camera.addCallbackBuffer(mPendingFrameData.array());
            mPendingFrameData = null;
        }

        if (!bytesToByteBuffer.containsKey(data)) {
            Log.d(TAG,
                    "Skipping frame.  Could not find ByteBuffer associated with the image " +
                            "data from the camera.");
            return;
        }

        // Timestamp and frame ID are maintained here, which will give downstream code some
        // idea of the timing of frames received and when frames were dropped along the way.
        mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
        mPendingFrameId++;
        mPendingFrameData = bytesToByteBuffer.get(data);

        // Notify the processor thread if it is waiting on the next frame (see below).
        mLock.notifyAll();
    }
}
 
Example 4
Source File: CameraSource.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
/**
 * Sets the frame data received from the camera.  This adds the previous unused frame buffer
 * (if present) back to the camera, and keeps a pending reference to the frame data for
 * future use.
 */
void setNextFrame(byte[] data, Camera camera) {
    synchronized (mLock) {
        if (mPendingFrameData != null) {
            camera.addCallbackBuffer(mPendingFrameData.array());
            mPendingFrameData = null;
        }

        // Timestamp and frame ID are maintained here, which will give downstream code some
        // idea of the timing of frames received and when frames were dropped along the way.
        mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
        mPendingFrameId++;
        mPendingFrameData = mBytesToByteBuffer.get(data);

        // Notify the processor thread if it is waiting on the next frame (see below).
        mLock.notifyAll();
    }
}
 
Example 5
Source File: CameraSource.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
/**
 * Sets the frame data received from the camera.  This adds the previous unused frame buffer
 * (if present) back to the camera, and keeps a pending reference to the frame data for
 * future use.
 */
void setNextFrame(byte[] data, Camera camera) {
    synchronized (mLock) {
        if (mPendingFrameData != null) {
            camera.addCallbackBuffer(mPendingFrameData.array());
            mPendingFrameData = null;
        }

        // Timestamp and frame ID are maintained here, which will give downstream code some
        // idea of the timing of frames received and when frames were dropped along the way.
        mPendingTimeMillis = SystemClock.elapsedRealtime() - mStartTimeMillis;
        mPendingFrameId++;
        mPendingFrameData = mBytesToByteBuffer.get(data);

        // Notify the processor thread if it is waiting on the next frame (see below).
        mLock.notifyAll();
    }
}
 
Example 6
Source File: CameraSource.java    From mlkit-material-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the frame data received from the camera. This adds the previous unused frame buffer (if
 * present) back to the camera, and keeps a pending reference to the frame data for future use.
 */
@SuppressWarnings("ByteBufferBackingArray")
void setNextFrame(byte[] data, Camera camera) {
  synchronized (lock) {
    if (pendingFrameData != null) {
      camera.addCallbackBuffer(pendingFrameData.array());
      pendingFrameData = null;
    }

    if (!bytesToByteBuffer.containsKey(data)) {
      Log.d(
          TAG,
          "Skipping frame. Could not find ByteBuffer associated with the image "
              + "data from the camera.");
      return;
    }

    pendingFrameData = bytesToByteBuffer.get(data);

    // Notify the processor thread if it is waiting on the next frame (see below).
    lock.notifyAll();
  }
}
 
Example 7
Source File: VideoPusher.java    From LivePublisher with MIT License 6 votes vote down vote up
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
	if (mPusherRuning) {
		switch (screen) {
		case SCREEN_PORTRAIT:
			portraitData2Raw(data);
			break;
		case SCREEN_LANDSCAPE_LEFT:
			raw = data;
			break;
		case SCREEN_LANDSCAPE_RIGHT:
			landscapeData2Raw(data);
			break;
		}
		mNative.fireVideo(raw);
	}
	camera.addCallbackBuffer(buffer);
}
 
Example 8
Source File: VideoCapture.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    mPreviewBufferLock.lock();
    try {
        if (!mIsRunning) {
            return;
        }
        if (data.length == mExpectedFrameSize) {
            int rotation = getDeviceOrientation();
            if (rotation != mDeviceOrientation) {
                mDeviceOrientation = rotation;
                Log.d(TAG,
                      "onPreviewFrame: device orientation=" +
                      mDeviceOrientation + ", camera orientation=" +
                      mCameraOrientation);
            }
            boolean flipVertical = false;
            boolean flipHorizontal = false;
            if (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                rotation = (mCameraOrientation + rotation) % 360;
                rotation = (360 - rotation) % 360;
                flipHorizontal = (rotation == 270 || rotation == 90);
                flipVertical = flipHorizontal;
            } else {
                rotation = (mCameraOrientation - rotation + 360) % 360;
            }
            nativeOnFrameAvailable(mNativeVideoCaptureDeviceAndroid,
                    data, mExpectedFrameSize,
                    rotation, flipVertical, flipHorizontal);
        }
    } finally {
        mPreviewBufferLock.unlock();
        if (camera != null) {
            camera.addCallbackBuffer(data);
        }
    }
}
 
Example 9
Source File: CameraUtils8.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addCallbackBuffer(Camera camera, byte[] data) {
    if (ownsBuffers) {
        // Give the video buffer to the camera service again.
        camera.addCallbackBuffer(data);
    }
}
 
Example 10
Source File: CameraStreamer.java    From peepers with Apache License 2.0 4 votes vote down vote up
private void startStreamingIfRunning() throws IOException
{
    // Throws RuntimeException if the camera is currently opened
    // by another application.
    final Camera camera = Camera.open(mCameraIndex);
    final Camera.Parameters params = camera.getParameters();

    final List<Camera.Size> supportedPreviewSizes = params.getSupportedPreviewSizes();
    final Camera.Size selectedPreviewSize = supportedPreviewSizes.get(mPreviewSizeIndex);
    params.setPreviewSize(selectedPreviewSize.width, selectedPreviewSize.height);

    if (mUseFlashLight)
    {
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    } // if

    // Set Preview FPS range. The range with the greatest maximum
    // is returned first.
    final List<int[]> supportedPreviewFpsRanges = params.getSupportedPreviewFpsRange();
    // XXX: However sometimes it returns null. This is a known bug
    // https://code.google.com/p/android/issues/detail?id=6271
    // In which case, we just don't set it.
    if (supportedPreviewFpsRanges != null)
    {
        final int[] range = supportedPreviewFpsRanges.get(0);
        params.setPreviewFpsRange(range[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
                range[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
        camera.setParameters(params);
    } // if

    // Set up preview callback
    mPreviewFormat = params.getPreviewFormat();
    final Camera.Size previewSize = params.getPreviewSize();
    mPreviewWidth = previewSize.width;
    mPreviewHeight = previewSize.height;
    final int BITS_PER_BYTE = 8;
    final int bytesPerPixel = ImageFormat.getBitsPerPixel(mPreviewFormat) / BITS_PER_BYTE;
    // XXX: According to the documentation the buffer size can be
    // calculated by width * height * bytesPerPixel. However, this
    // returned an error saying it was too small. It always needed
    // to be exactly 1.5 times larger.
    mPreviewBufferSize = mPreviewWidth * mPreviewHeight * bytesPerPixel * 3 / 2 + 1;
    camera.addCallbackBuffer(new byte[mPreviewBufferSize]);
    mPreviewRect = new Rect(0, 0, mPreviewWidth, mPreviewHeight);
    camera.setPreviewCallbackWithBuffer(mPreviewCallback);

    // We assumed that the compressed image will be no bigger than
    // the uncompressed image.
    mJpegOutputStream = new MemoryOutputStream(mPreviewBufferSize);

    final MJpegHttpStreamer streamer = new MJpegHttpStreamer(mPort, mPreviewBufferSize);
    streamer.start();

    synchronized (mLock)
    {
        if (!mRunning)
        {
            streamer.stop();
            camera.release();
            return;
        } // if

        try
        {
            camera.setPreviewDisplay(mPreviewDisplay);
        } // try
        catch (final IOException e)
        {
            streamer.stop();
            camera.release();
            throw e;
        } // catch

        mMJpegHttpStreamer = streamer;
        camera.startPreview();
        mCamera = camera;
    } // synchronized
}
 
Example 11
Source File: CameraSource.java    From mlkit-material-android with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws IOException if camera cannot be found or preview cannot be processed.
 */
private Camera createCamera() throws IOException {
  Camera camera = Camera.open();
  if (camera == null) {
    throw new IOException("There is no back-facing camera.");
  }

  Camera.Parameters parameters = camera.getParameters();
  setPreviewAndPictureSize(camera, parameters);
  setRotation(camera, parameters);

  int[] previewFpsRange = selectPreviewFpsRange(camera);
  if (previewFpsRange == null) {
    throw new IOException("Could not find suitable preview frames per second range.");
  }
  parameters.setPreviewFpsRange(
      previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
      previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);

  parameters.setPreviewFormat(IMAGE_FORMAT);

  if (parameters
      .getSupportedFocusModes()
      .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
  } else {
    Log.i(TAG, "Camera auto focus is not supported on this device.");
  }

  camera.setParameters(parameters);

  camera.setPreviewCallbackWithBuffer(processingRunnable::setNextFrame);

  // Four frame buffers are needed for working with the camera:
  //
  //   one for the frame that is currently being executed upon in doing detection
  //   one for the next pending frame to process immediately upon completing detection
  //   two for the frames that the camera uses to populate future preview images
  //
  // Through trial and error it appears that two free buffers, in addition to the two buffers
  // used in this code, are needed for the camera to work properly. Perhaps the camera has one
  // thread for acquiring images, and another thread for calling into user code. If only three
  // buffers are used, then the camera will spew thousands of warning messages when detection
  // takes a non-trivial amount of time.
  camera.addCallbackBuffer(createPreviewBuffer(previewSize));
  camera.addCallbackBuffer(createPreviewBuffer(previewSize));
  camera.addCallbackBuffer(createPreviewBuffer(previewSize));
  camera.addCallbackBuffer(createPreviewBuffer(previewSize));

  return camera;
}
 
Example 12
Source File: CameraSource.java    From ETHWallet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                mFocusMode)) {
            parameters.setFocusMode(mFocusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + mFocusMode + " is not supported on this device.");
        }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
        if (parameters.getSupportedFlashModes().contains(
                mFlashMode)) {
            parameters.setFlashMode(mFlashMode);
        } else {
            Log.i(TAG, "Camera flash mode: " + mFlashMode + " is not supported on this device.");
        }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
}
 
Example 13
Source File: CameraSource.java    From Camera2Vision with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the camera and starts sending preview frames to the underlying detector.  The supplied
 * surface holder is used for the preview so frames can be displayed to the user.
 *
 * @param surfaceHolder the surface holder to use for the preview frames
 * @throws IOException if the supplied surface holder could not be used as the preview display
 */
@RequiresPermission(Manifest.permission.CAMERA)
public CameraSource start(SurfaceHolder surfaceHolder) throws IOException {
    synchronized (mCameraLock) {
        if (mCamera != null) {
            return this;
        }
        try {
            previewSurfaceHolder = surfaceHolder;
            int requestedCameraId = getIdForRequestedCamera(mFacing);
            if (requestedCameraId == -1) {throw new RuntimeException("Could not find requested camera.");}

            Camera camera = Camera.open(requestedCameraId);

            Camera.Size pictureSize = getBestAspectPictureSize(camera, mContext);
            Camera.Size previewSize = getBestAspectPreviewSize(camera, mContext);
            mPreviewSize = new Size(previewSize.width, previewSize.height);
            mPictureSize = new Size(pictureSize.width, pictureSize.height);
            int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
            if (previewFpsRange == null) {throw new RuntimeException("Could not find suitable preview frames per second range.");}

            Camera.Parameters parameters = camera.getParameters();
            parameters.setPictureSize(pictureSize.width, pictureSize.height);
            parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            parameters.setPreviewFpsRange(previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
            parameters.setPreviewFormat(ImageFormat.NV21);

            setRotation(camera, parameters, requestedCameraId);

            if (mFocusMode != null) {
                if (parameters.getSupportedFocusModes().contains(mFocusMode)) {
                    parameters.setFocusMode(mFocusMode);
                } else {
                    Log.i(TAG, "Camera focus mode: " + mFocusMode + " is not supported on this device.");
                }
            }

            // setting mFocusMode to the one set in the params
            mFocusMode = parameters.getFocusMode();

            if (mFlashMode != null) {
                if (parameters.getSupportedFlashModes() != null) {
                    if (parameters.getSupportedFlashModes().contains(mFlashMode)) {
                        parameters.setFlashMode(mFlashMode);
                    } else {
                        Log.i(TAG, "Camera flash mode: " + mFlashMode + " is not supported on this device.");
                    }
                }
            }

            // setting mFlashMode to the one set in the params
            mFlashMode = parameters.getFlashMode();

            camera.setParameters(parameters);

            // Four frame buffers are needed for working with the camera:
            //
            //   one for the frame that is currently being executed upon in doing detection
            //   one for the next pending frame to process immediately upon completing detection
            //   two for the frames that the camera uses to populate future preview images
            camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
            camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
            camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
            camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
            camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

            mCamera = camera;

            mCamera.setPreviewDisplay(previewSurfaceHolder);
            mCamera.startPreview();

            mProcessingThread = new Thread(mFrameProcessor);
            mFrameProcessor.setActive(true);
            mProcessingThread.start();

        } catch (RuntimeException e) {
            e.printStackTrace();
            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
            return null;
        }
    }
    return this;
}
 
Example 14
Source File: Camera1ApiManager.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 4 votes vote down vote up
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
  getCameraData.inputYUVData(new Frame(data, rotation, isFrontCamera && isPortrait, imageFormat));
  camera.addCallbackBuffer(yuvBuffer);
}
 
Example 15
Source File: CameraSource.java    From android-vision with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, requestedPreviewWidth, requestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    previewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, requestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(previewSize.getWidth(), previewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (focusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                focusMode)) {
            parameters.setFocusMode(focusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + focusMode +
                    " is not supported on this device.");
        }
    }

    // setting focusMode to the one set in the params
    focusMode = parameters.getFocusMode();

    if (flashMode != null) {
        if (parameters.getSupportedFlashModes().contains(
                flashMode)) {
            parameters.setFlashMode(flashMode);
        } else {
            Log.i(TAG, "Camera flash mode: " + flashMode +
                    " is not supported on this device.");
        }
    }

    // setting flashMode to the one set in the params
    flashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(previewSize));
    camera.addCallbackBuffer(createPreviewBuffer(previewSize));
    camera.addCallbackBuffer(createPreviewBuffer(previewSize));
    camera.addCallbackBuffer(createPreviewBuffer(previewSize));

    return camera;
}
 
Example 16
Source File: FuTextureCamera.java    From FuAgoraDemoDroid with MIT License 4 votes vote down vote up
@Override
public void onPreviewFrame(byte[] data, Camera mCamera) {
    mCameraNV21Byte = data;
    mCamera.addCallbackBuffer(data);
}
 
Example 17
Source File: CameraSource.java    From AndroidApp with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                mFocusMode)) {
            parameters.setFocusMode(mFocusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + mFocusMode + " is not supported on this device.");
        }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
        if (parameters.getSupportedFlashModes().contains(
                mFlashMode)) {
            parameters.setFlashMode(mFlashMode);
        } else {
            Log.i(TAG, "Camera flash mode: " + mFlashMode + " is not supported on this device.");
        }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
}
 
Example 18
Source File: VideoCaptureAndroid.java    From webrtc-app-mono with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
  ProvideCameraFrame(data, data.length, native_capturer);
  camera.addCallbackBuffer(data);
}
 
Example 19
Source File: CameraSource.java    From android-vision with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                mFocusMode)) {
            parameters.setFocusMode(mFocusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + mFocusMode +
                " is not supported on this device.");
        }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
        if (parameters.getSupportedFlashModes().contains(
                mFlashMode)) {
            parameters.setFlashMode(mFlashMode);
        } else {
            Log.i(TAG, "Camera flash mode: " + mFlashMode +
                " is not supported on this device.");
        }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
}
 
Example 20
Source File: CameraSource.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                mFocusMode)) {
            parameters.setFocusMode(mFocusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + mFocusMode + " is not supported on this device.");
        }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
        if (parameters.getSupportedFlashModes() != null) {
            if (parameters.getSupportedFlashModes().contains(
                    mFlashMode)) {
                parameters.setFlashMode(mFlashMode);
            } else {
                Log.i(TAG, "Camera flash mode: " + mFlashMode + " is not supported on this device.");
            }
        }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
}