android.media.ImageReader Java Examples
The following examples show how to use
android.media.ImageReader.
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: ImageCapture.java From DoraemonKit with Apache License 2.0 | 6 votes |
/** * */ void initImageRead(MediaProjection mediaProjection) { if (mediaProjection == null) { LogHelper.e(TAG, "mediaProjection == null"); return; } int width = UIUtils.getWidthPixels(); int height = UIUtils.getRealHeightPixels(); int dpi = UIUtils.getDensityDpi(); mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2); /** * 获取getSurface */ mediaProjection.createVirtualDisplay("ScreenCapture", width, height, dpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); }
Example #2
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 #3
Source File: Screenshotter.java From loco-answers with GNU General Public License v3.0 | 6 votes |
@Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); if (image == null) { Log.d(TAG, "onImageAvailable: image is null"); return; } final Image.Plane[] planes = image.getPlanes(); final Buffer buffer = planes[0].getBuffer().rewind(); int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * width; Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(buffer); tearDown(); image.close(); cb.onScreenshot(bitmap); }
Example #4
Source File: Camera2.java From VIA-AI with MIT License | 6 votes |
@Override public void onImageAvailable(ImageReader reader) { synchronized (mLock) { if (borrowedImage == null) { Image i = reader.acquireNextImage(); borrowedImage = i; if (bDebug) { Log.d(TAG, "==== OnImageReady ===="); Log.d(TAG, "Size:" + i.getWidth() + "x" + i.getHeight()); Log.d(TAG, "Format:" + i.getFormat()); Log.d(TAG, "#Planes:" + i.getPlanes().length); Log.d(TAG, "Y-Plane Pixel Stride:" + i.getPlanes()[0].getPixelStride()); Log.d(TAG, "Y-Planes Row Stride" + i.getPlanes()[0].getRowStride()); Log.d(TAG, "Y(i[0]) Start Address:" + NativeRender.getPointerFromByteBuffer(i.getPlanes()[0].getBuffer(), 0)); Log.d(TAG, "U(i[0]) Start Address:" + NativeRender.getPointerFromByteBuffer(i.getPlanes()[1].getBuffer(), 0)); Log.d(TAG, "V(i[0]) Start Address:" + NativeRender.getPointerFromByteBuffer(i.getPlanes()[2].getBuffer(), 0)); Log.d(TAG, "======================"); } if (mCallback != null) mCallback.onFrameReady(); } } }
Example #5
Source File: HeifReader.java From heifreader with MIT License | 6 votes |
private static Bitmap renderHevcImageWithFormat(ByteBuffer bitstream, ImageInfo info, int imageFormat) throws FormatFallbackException { try (ImageReader reader = ImageReader.newInstance(info.size.getWidth(), info.size.getHeight(), imageFormat, 1)) { renderHevcImage(bitstream, info, reader.getSurface()); Image image = null; try { try { image = reader.acquireNextImage(); } catch (UnsupportedOperationException ex) { throw new FormatFallbackException(ex); } switch (image.getFormat()) { case ImageFormat.YUV_420_888: case ImageFormat.YV12: return convertYuv420ToBitmap(image); case ImageFormat.RGB_565: return convertRgb565ToBitmap(image); default: throw new RuntimeException("unsupported image format(" + image.getFormat() + ")"); } } finally { if (image != null) { image.close(); } } } }
Example #6
Source File: Camera2StateMachine.java From DeviceConnect-Android with MIT License | 6 votes |
/** * カメラデバイスのプレビューを開始します. * <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 #7
Source File: MainActivity.java From fritz-examples with MIT License | 6 votes |
@Override public void onImageAvailable(final ImageReader reader) { Image image = reader.acquireLatestImage(); if (image == null) { return; } if (!shouldSample.get()) { image.close(); return; } if (!isComputing.compareAndSet(false, true)) { image.close(); return; } visionImage = FritzVisionImage.fromMediaImage(image, orientation); image.close(); runInBackground(() -> { objectResult = predictor.predict(visionImage); requestRender(); }); }
Example #8
Source File: DoorbellActivity.java From doorbell with Apache License 2.0 | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); // get image bytes ByteBuffer imageBuf = image.getPlanes()[0].getBuffer(); final byte[] imageBytes = new byte[imageBuf.remaining()]; imageBuf.get(imageBytes); image.close(); onPictureTaken(imageBytes); }
Example #9
Source File: ClassifierActivity.java From android-yolo-v2 with Do What The F*ck You Want To Public License | 5 votes |
@Override public void onImageAvailable(final ImageReader reader) { Image image = null; try { image = reader.acquireLatestImage(); if (image == null) { return; } if (computing) { image.close(); return; } computing = true; fillCroppedBitmap(image); image.close(); } catch (final Exception ex) { if (image != null) { image.close(); } Log.e(LOGGING_TAG, ex.getMessage()); } runInBackground(() -> { final long startTime = SystemClock.uptimeMillis(); final List<Recognition> results = recognizer.recognizeImage(croppedBitmap); lastProcessingTimeMs = SystemClock.uptimeMillis() - startTime; overlayView.setResults(results); speak(results); requestRender(); computing = false; }); }
Example #10
Source File: Camera2Fragment.java From 361Camera with Apache License 2.0 | 5 votes |
private ImageSaver(Image image, File file, CaptureResult result, CameraCharacteristics characteristics, Context context, RefCountedAutoCloseable<ImageReader> reader) { mImage = image; mFile = file; mCaptureResult = result; mCharacteristics = characteristics; mContext = context; mReader = reader; }
Example #11
Source File: Camera2.java From cameraview with Apache License 2.0 | 5 votes |
private void prepareImageReader() { if (mImageReader != null) { mImageReader.close(); } Size largest = mPictureSizes.sizes(mAspectRatio).last(); mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, /* maxImages */ 2); mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null); }
Example #12
Source File: HyperionScreenEncoder.java From hyperion-android-grabber with MIT License | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) private void setImageReader() { if (DEBUG) Log.d(TAG, "Setting image reader " + String.valueOf(isCapturing())); mImageReader = ImageReader.newInstance(getGrabberWidth(), getGrabberHeight(), PixelFormat.RGBA_8888, MAX_IMAGE_READER_IMAGES); mImageReader.setOnImageAvailableListener(imageAvailableListener, mHandler); mVirtualDisplay.setSurface(mImageReader.getSurface()); setCapturing(true); }
Example #13
Source File: CameraConfig.java From AndroidDemo with MIT License | 5 votes |
public void setImageFormat(int imageFormat) { if (streamConfigurationMap.isOutputSupportedFor(imageFormat)) { imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), imageFormat, 1); imageReader.setOnImageAvailableListener(imageAvailableListener, handler); } else { Log.w(TAG, "ImageFormat <" + imageFormat + "> is not support."); } }
Example #14
Source File: RecordService.java From screen-share-to-browser with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); HandlerThread serviceThread = new HandlerThread("service_thread", android.os.Process.THREAD_PRIORITY_BACKGROUND); serviceThread.start(); running = false; threadCount = Runtime.getRuntime().availableProcessors(); Log.d(TAG, "onCreate: threadCount" + threadCount); executorService = Executors.newFixedThreadPool(threadCount); HandlerThread handlerThread = new HandlerThread("Screen Record"); handlerThread.start(); screenHandler = new ScreenHandler(handlerThread.getLooper()); //get the size of the window WindowManager mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE); // width = mWindowManager.getDefaultDisplay().getWidth() + 40; width = mWindowManager.getDefaultDisplay().getWidth(); height = mWindowManager.getDefaultDisplay().getHeight(); //height = 2300; Log.i(TAG, "onCreate: w is " + width + " h is " + height); imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2); scheduler = Schedulers.from(executorService); // Disposable disposable = // getByteBufferObservable() // .subscribeOn(Schedulers.io()) // .observeOn(scheduler) // .map(getBitmapFunction()) // .subscribe(getBitmapConsumer()); // compositeDisposable.add(disposable); }
Example #15
Source File: Camera2Source.java From Machine-Learning-Projects-for-Mobile-Applications with MIT License | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Image mImage = reader.acquireNextImage(); if (mImage == null) { return; } mFrameProcessor.setNextFrame(convertYUV420888ToNV21(mImage)); mImage.close(); }
Example #16
Source File: CameraController.java From libsoftwaresync with Apache License 2.0 | 5 votes |
public List<Surface> getOutputSurfaces() { List<Surface> surfaces = new ArrayList<>(); for (ImageReader reader : imageReaders) { surfaces.add(reader.getSurface()); } return surfaces; }
Example #17
Source File: Camera2PreviewCallback.java From CameraCompat with MIT License | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { try { final Image image = reader.acquireLatestImage(); if (image != null) { mCameraFrameCallback.onFrameData(image, image::close); } } catch (OutOfMemoryError | IllegalStateException e) { CameraCompat.onError(CameraCompat.ERR_UNKNOWN); } }
Example #18
Source File: Camera2StateMachine.java From DeviceConnect-Android with MIT License | 5 votes |
/** * カメラデバイスの写真撮影を行います. * <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 #19
Source File: FragmentDecoder.java From camera2QRcodeReader with MIT License | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Log.e(TAG, "onImageAvailable: " + count++); Image img = null; img = reader.acquireLatestImage(); Result rawResult = null; try { if (img == null) throw new NullPointerException("cannot be null"); ByteBuffer buffer = img.getPlanes()[0].getBuffer(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); int width = img.getWidth(); int height = img.getHeight(); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); rawResult = mQrReader.decode(bitmap); onQRCodeRead(rawResult.getText()); } catch (ReaderException ignored) { Log.e(TAG, "Reader shows an exception! ", ignored); /* Ignored */ } catch (NullPointerException ex) { ex.printStackTrace(); } finally { mQrReader.reset(); Log.e(TAG, "in the finally! ------------"); if (img != null) img.close(); } if (rawResult != null) { Log.e(TAG, "Decoding successful!"); } else { Log.d(TAG, "No QR code found…"); } }
Example #20
Source File: LiveCameraActivity.java From fritz-examples with MIT License | 5 votes |
@Override public void onImageAvailable(final ImageReader reader) { Image image = reader.acquireLatestImage(); if (image == null) { return; } if (!computing.compareAndSet(false, true)) { image.close(); return; } setupImageForPrediction(image); image.close(); runInBackground( new Runnable() { @Override public void run() { runInference(); // Fire callback to change the OverlayView requestRender(); computing.set(false); } }); }
Example #21
Source File: Camera2Proxy.java From CameraDemo with Apache License 2.0 | 5 votes |
public void setImageAvailableListener(ImageReader.OnImageAvailableListener onImageAvailableListener) { if (mImageReader == null) { Log.w(TAG, "setImageAvailableListener: mImageReader is null"); return; } mImageReader.setOnImageAvailableListener(onImageAvailableListener, null); }
Example #22
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(); } }
Example #23
Source File: CameraDriver.java From android-robocar with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void onImageAvailable(final ImageReader reader) { Timber.d("Picture is available."); new Thread(new Runnable() { @Override public void run() { File root = ImageSaver.getRoot(CameraOperator.ROBOCAR_FOLDER); new ImageSaver(reader.acquireLatestImage(), root, PHOTO_FILENAME).run(); processPhoto(root, PHOTO_FILENAME); } }).start(); }
Example #24
Source File: MainActivity.java From AndroidPlayground with MIT License | 5 votes |
@Override public void onImageAvailable(ImageReader imageReader) { Log.d("MainActivity", "onImageAvailable"); final Image image = imageReader.acquireLatestImage(); count++; if (count == 100) { byte[] yuv = new byte[image.getWidth() * image.getHeight() * 3 / 2]; image2yuv(image, yuv); saveRawYuvData(yuv, image.getWidth(), image.getHeight(), "org"); } image.close(); }
Example #25
Source File: CameraHandler.java From androidthings-imageclassifier with Apache License 2.0 | 5 votes |
/** * 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 #26
Source File: CameraHandler.java From androidthings-imageclassifier with Apache License 2.0 | 5 votes |
/** * 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 #27
Source File: ImageClassifierActivity.java From androidthings-imageclassifier with Apache License 2.0 | 5 votes |
/** * Initialize the camera that will be used to capture images. */ private void initCamera() { mImagePreprocessor = new ImagePreprocessor(PREVIEW_IMAGE_WIDTH, PREVIEW_IMAGE_HEIGHT, TF_INPUT_IMAGE_WIDTH, TF_INPUT_IMAGE_HEIGHT); mCameraHandler = CameraHandler.getInstance(); mCameraHandler.initializeCamera(this, PREVIEW_IMAGE_WIDTH, PREVIEW_IMAGE_HEIGHT, null, new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader imageReader) { Bitmap bitmap = mImagePreprocessor.preprocessImage(imageReader.acquireNextImage()); onPhotoReady(bitmap); } }); }
Example #28
Source File: Camera2Source.java From Camera2Vision with Apache License 2.0 | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Image mImage = reader.acquireNextImage(); if(mImage == null) { return; } mFrameProcessor.setNextFrame(convertYUV420888ToNV21(mImage)); mImage.close(); }
Example #29
Source File: ImageScreenCast.java From DeviceConnect-Android with MIT License | 5 votes |
ImageScreenCast(final Context context, final MediaProjection mediaProjection, final ImageReader imageReader, int width, int height) { super(context, mediaProjection, width, height); mImageReader = imageReader; }
Example #30
Source File: SRManager.java From VMLibrary with Apache License 2.0 | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); long currTime = System.currentTimeMillis(); VMLog.d("捕获图片有效回调 %d", currTime - oldTime); if (currTime - oldTime > 100) { oldTime = currTime; Bitmap bitmap = null; if (image != null) { Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); int width = image.getWidth(); int height = image.getHeight(); int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * width; // create bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(buffer); bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height); if (screenShortCallback != null) { screenShortCallback.onBitmap(bitmap); } } } if (image != null) { image.close(); } }