Java Code Examples for android.media.ImageReader#OnImageAvailableListener

The following examples show how to use android.media.ImageReader#OnImageAvailableListener . 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: Camera2StateMachine.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * カメラデバイスのプレビューを開始します.
 * <p>
 * {@link #setTextureView(AutoFitTextureView)} もしくは、{@link #setTextureView(AutoFitTextureView)}
 * のどちらに設定してから呼び出してください。
 * </p>
 *
 * @return プレビューの開始に成功した場合はtrue、それ以外はfalse
 */
public boolean startPreview(final ImageReader.OnImageAvailableListener previewListener) {
    if (mState == null) {
        mPreviewListener = previewListener;
        ((OpenCameraState) mOpenCameraState).mOpenCallback = () -> nextState(mCreateSessionState);
        if (mTextureView != null) {
            nextState(mInitSurfaceState);
            return true;
        } else if (mSurfaceTexture != null) {
            nextState(mOpenCameraState);
            return true;
        }
    } else if (mState == mOpenCameraState) {
        mPreviewListener = previewListener;
        nextState(mCreateSessionState);
        return true;
    }
    return false;
}
 
Example 2
Source File: Camera2Proxy.java    From mobile-ar-sensor-logger with GNU General Public License v3.0 5 votes vote down vote up
public void setImageAvailableListener(ImageReader.OnImageAvailableListener
                                              onImageAvailableListener) {
    if (mImageReader == null) {
        Log.w(TAG, "setImageAvailableListener: mImageReader is null");
        return;
    }
    mImageReader.setOnImageAvailableListener(onImageAvailableListener, null);
}
 
Example 3
Source File: ImageMetadataSynchronizer.java    From libsoftwaresync with Apache License 2.0 5 votes vote down vote up
/**
 * Create a ImageMetadataSynchronizer for the List of ImageReaders. 'imageReaders' should not
 * contain any duplicates. Each reader's OnImageAvailableListener is set to an internal
 * ImageReader.OnImageAvailableListener of the synchronizer and it is an error to change it
 * externally. We take ownership of each ImageReader and will close them when this
 * ImageMetadataSynchronizer is closed.
 *
 * <p>imageHandler can be set to an arbitrary non-null Handler and is shared across all
 * ImageReader instances.
 *
 * <p>Callback.onDataAvailable() is called with Image's in the same order as imageReaders.
 */
@SuppressWarnings("JdkObsolete")
public ImageMetadataSynchronizer(List<ImageReader> imageReaders, Handler imageHandler) {
  closed = false;

  createCaptureCallback();

  this.imageReaders.addAll(imageReaders);
  // Create a queue and a listener per ImageReader.
  int nReaders = imageReaders.size();
  for (int i = 0; i < nReaders; ++i) {
    final int readerIndex = i;
    ImageReader reader = imageReaders.get(readerIndex);
    pendingImageQueues.add(new LinkedList<>());
    imagesAcquired.add(0);

    ImageReader.OnImageAvailableListener listener =
        reader1 -> {
          synchronized (ImageMetadataSynchronizer.this) {
            if (closed) {
              return;
            }
            int nImagesAcquired = imagesAcquired.get(readerIndex);
            if (nImagesAcquired < reader1.getMaxImages()) {
              Image image = reader1.acquireNextImage();
              imagesAcquired.set(readerIndex, nImagesAcquired + 1);
              handleImageLocked(readerIndex, image);
            }
          }
        };
    reader.setOnImageAvailableListener(listener, imageHandler);
  }
}
 
Example 4
Source File: DoorbellCamera.java    From androidthings-cameraCar with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
public void initializeCamera(Context context,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
    String[] camIds = {};
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.d(TAG, "Cam access exception getting IDs", e);
    }
    if (camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    String id = camIds[0];
    Log.d(TAG, "Using camera id " + id);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(IMAGE_WIDTH, IMAGE_HEIGHT,
            ImageFormat.JPEG, MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(
            imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(id, mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 5
Source File: Camera2Proxy.java    From CameraDemo with Apache License 2.0 5 votes vote down vote up
public void setImageAvailableListener(ImageReader.OnImageAvailableListener onImageAvailableListener) {
    if (mImageReader == null) {
        Log.w(TAG, "setImageAvailableListener: mImageReader is null");
        return;
    }
    mImageReader.setOnImageAvailableListener(onImageAvailableListener, null);
}
 
Example 6
Source File: CameraHandler.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
@SuppressLint("MissingPermission")
public void initializeCamera(Context context, int previewWidth, int previewHeight,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    if (initialized) {
        throw new IllegalStateException(
                "CameraHandler is already initialized or is initializing");
    }
    initialized = true;

    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String[] camIds = null;
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.w(TAG, "Cannot get the list of available cameras", e);
    }
    if (camIds == null || camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    Log.d(TAG, "Using camera id " + camIds[0]);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(previewWidth, previewHeight, ImageFormat.JPEG,
            MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(camIds[0], mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 7
Source File: CameraHandler.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
@SuppressLint("MissingPermission")
public void initializeCamera(Context context, int previewWidth, int previewHeight,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    if (initialized) {
        throw new IllegalStateException(
                "CameraHandler is already initialized or is initializing");
    }
    initialized = true;

    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String[] camIds = null;
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.w(TAG, "Cannot get the list of available cameras", e);
    }
    if (camIds == null || camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    Log.d(TAG, "Using camera id " + camIds[0]);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(previewWidth, previewHeight, ImageFormat.JPEG,
            MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(camIds[0], mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 8
Source File: CameraHandler.java    From androidthings-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
@SuppressLint("MissingPermission")
public void initializeCamera(Context context, int previewWidth, int previewHeight,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    if (initialized) {
        throw new IllegalStateException(
                "CameraHandler is already initialized or is initializing");
    }
    initialized = true;

    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String[] camIds = null;
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.w(TAG, "Cannot get the list of available cameras", e);
    }
    if (camIds == null || camIds.length < 1) {
        Log.d(TAG, "No cameras found");
        return;
    }
    Log.d(TAG, "Using camera id " + camIds[0]);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(previewWidth, previewHeight, ImageFormat.JPEG,
            MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(camIds[0], mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 9
Source File: CameraHandler.java    From sample-tensorflow-imageclassifier with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
@SuppressLint("MissingPermission")
public void initializeCamera(Context context, Handler backgroundHandler, Size minSize,
                             ImageReader.OnImageAvailableListener imageAvailableListener)
    throws CameraAccessException {
    if (initialized) {
        throw new IllegalStateException(
                "CameraHandler is already initialized or is initializing");
    }
    initialized = true;
    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    String camId = getCameraId(context);

    // Initialize the image processor with the largest available size.
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(camId);
    StreamConfigurationMap map = characteristics.get(
        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

    Size bestSize = getBestCameraSize(map.getOutputSizes(ImageFormat.JPEG), minSize);
    if (bestSize == null) {
        throw new RuntimeException("We could not find a camera resolution that is larger than "
                + minSize.getWidth() + "x" + minSize.getHeight());
    }

    mImageReader = ImageReader.newInstance(bestSize.getWidth(), bestSize.getHeight(),
        ImageFormat.JPEG, MAX_IMAGES);
    mImageDimensions = bestSize;
    Log.d(TAG, "Will capture photos that are " + mImageDimensions.getWidth() + " x " +
        mImageDimensions.getHeight());
    mImageReader.setOnImageAvailableListener(imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(camId, mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.e(TAG, "Camera access exception", cae);
    }
}
 
Example 10
Source File: DoorbellCamera.java    From doorbell with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the camera device
 */
public void initializeCamera(Context context,
                             Handler backgroundHandler,
                             ImageReader.OnImageAvailableListener imageAvailableListener) {
    // Discover the camera instance
    CameraManager manager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
    String[] camIds = {};
    try {
        camIds = manager.getCameraIdList();
    } catch (CameraAccessException e) {
        Log.e(TAG, "Cam access exception getting IDs", e);
    }
    if (camIds.length < 1) {
        Log.e(TAG, "No cameras found");
        return;
    }
    String id = camIds[0];
    Log.d(TAG, "Using camera id " + id);

    // Initialize the image processor
    mImageReader = ImageReader.newInstance(IMAGE_WIDTH, IMAGE_HEIGHT,
            ImageFormat.JPEG, MAX_IMAGES);
    mImageReader.setOnImageAvailableListener(
            imageAvailableListener, backgroundHandler);

    // Open the camera resource
    try {
        manager.openCamera(id, mStateCallback, backgroundHandler);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "Camera access exception", cae);
    }
}
 
Example 11
Source File: Camera2StateMachine.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * カメラデバイスの写真撮影を行います.
 * <p>
 * 写真撮影を行う場合には、{@link #startPreview()} を行い状態を {@link #STATE_PREVIEW} にしておく必要があります。
 * </p>
 * <p>
 * プレビューサイズ、写真サイズを切り替える場合には、プレビューを停止してから、プレビューを開始する必要があります。
 * </p>
 * @param listener 写真撮影した結果を通知するリスナー
 * @return 撮影開始に成功したらtrue、それ以外はfalse
 */
public boolean takePicture(final ImageReader.OnImageAvailableListener listener) {
    if (mState != mPreviewState) {
        if (DEBUG) {
            Log.w(TAG, "It is invalid state to take a picture. state=" + mState);
        }
        return false;
    }

    mTakePictureListener = listener;
    nextState(mAutoFocusState);
    return true;
}
 
Example 12
Source File: Camera2Wrapper.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * カメラデバイスの写真撮影を行います.
 * <p>
 * 写真撮影を行う場合には、{@link #startPreview()} を行い状態を {@link #STATE_PREVIEW} にしておく必要があります。
 * </p>
 * <p>
 * プレビューサイズ、写真サイズを切り替える場合には、プレビューを停止してから、プレビューを開始する必要があります。
 * </p>
 * @param listener 写真撮影した結果を通知するリスナー
 * @return 撮影開始に成功したらtrue、それ以外はfalse
 */
public boolean takePicture(final ImageReader.OnImageAvailableListener listener) {
    if (mState != mPreviewState) {
        if (DEBUG) {
            Log.w(TAG, "It is invalid state to take a picture. state=" + mState);
        }
        return false;
    }

    mTakePictureListener = listener;
    mTakePictureState.setReturnState(mState);
    nextState(mAutoFocusState);
    return true;
}
 
Example 13
Source File: CameraSurfaceVideoEncoder.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 写真撮影を行います.
 *
 * @param l 撮影した写真を通知するリスナー
 */
public void takePicture(ImageReader.OnImageAvailableListener l) {
    if (mCamera2 == null) {
        if (DEBUG) {
            Log.w(TAG, "Camera2 is null.");
        }
        return;
    }
    mCamera2.takePicture(l);
}
 
Example 14
Source File: LollipopCamera.java    From LiveMultimedia with Apache License 2.0 5 votes vote down vote up
public void  setupImageListener() {
    mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            mBackgroundHandler.post(new ImageSaverThread(reader.acquireNextImage(), mFile));
        }
    };
}