Java Code Examples for android.media.ImageReader#setOnImageAvailableListener()
The following examples show how to use
android.media.ImageReader#setOnImageAvailableListener() .
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: Camera2Recorder.java From DeviceConnect-Android with MIT License | 6 votes |
/** * 静止画の撮影を行います. * * @param listener 静止画の撮影結果を通知するリスナー */ private void takePhotoInternal(final @NonNull OnPhotoEventListener listener) { try { ImageReader stillImageReader = mCameraWrapper.createStillImageReader(ImageFormat.JPEG); stillImageReader.setOnImageAvailableListener((reader) -> { Image photo = reader.acquireNextImage(); if (photo == null) { listener.onFailedTakePhoto("Failed to acquire image."); return; } storePhoto(photo, listener); photo.close(); }, mPhotoHandler); mCameraWrapper.takeStillImage(stillImageReader.getSurface()); } catch (CameraWrapperException e) { if (DEBUG) { Log.e(TAG, "Failed to take photo.", e); } listener.onFailedTakePhoto("Failed to take photo."); } }
Example 2
Source File: ImageMetadataSynchronizer.java From libsoftwaresync with Apache License 2.0 | 5 votes |
/** * 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 3
Source File: ScreenCapturer.java From habpanelviewer with GNU General Public License v3.0 | 5 votes |
public synchronized Bitmap captureScreen() throws IllegalStateException { AtomicReference<Image> imageHolder = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2); mImageReader.setOnImageAvailableListener(imageReader -> { imageHolder.set(mImageReader.acquireLatestImage()); latch.countDown(); }, mHandler); VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror", mWidth, mHeight, mDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mImageReader.getSurface(), null, null); try { latch.await(1, TimeUnit.SECONDS); if (latch.getCount() == 1) { throw new IllegalStateException("Screen capturing timed out"); } final Image image = imageHolder.get(); Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * mWidth; // create bitmap Bitmap bmp = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888); bmp.copyPixelsFromBuffer(buffer); image.close(); return bmp; } catch (InterruptedException e) { throw new IllegalStateException("Got interrupt while capturing screen"); } finally { display.release(); } }