android.media.Image Java Examples
The following examples show how to use
android.media.Image.
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: AndroidImageReaderProxy.java From Camera2 with Apache License 2.0 | 6 votes |
@Override @Nullable public ImageProxy acquireLatestImage() { synchronized (mLock) { Image image = mDelegate.acquireLatestImage(); if (image == null) { return null; } else { return new AndroidImageProxy(image); } } }
Example #2
Source File: SurfaceCamera2Activity.java From CameraDemo with Apache License 2.0 | 6 votes |
@Override protected Bitmap doInBackground(Image ... images) { ByteBuffer buffer = images[0].getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); long time = System.currentTimeMillis(); if (mCameraProxy.isFrontCamera()) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.d(TAG, "BitmapFactory.decodeByteArray time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); // 前置摄像头需要左右镜像 Bitmap rotateBitmap = ImageUtils.rotateBitmap(bitmap, 0, true, true); Log.d(TAG, "rotateBitmap time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); ImageUtils.saveBitmap(rotateBitmap); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); rotateBitmap.recycle(); } else { ImageUtils.saveImage(bytes); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); } images[0].close(); return ImageUtils.getLatestThumbBitmap(); }
Example #3
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(() -> { labelResult = predictor.predict(visionImage); requestRender(); }); }
Example #4
Source File: HyperionScreenEncoder.java From hyperion-android-grabber with MIT License | 6 votes |
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onImageAvailable(ImageReader reader) { if (mListener != null && isCapturing()) { try { long now = System.nanoTime(); Image img = reader.acquireLatestImage(); if (img != null && now - lastFrame >= min_nano_time) { sendImage(img); img.close(); lastFrame = now; } else if (img != null) { img.close(); } } catch (final Exception e) { if (DEBUG) Log.w(TAG, "sendImage exception:", e); } } }
Example #5
Source File: GLSurfaceCamera2Activity.java From CameraDemo with Apache License 2.0 | 6 votes |
@Override protected Bitmap doInBackground(Image... images) { ByteBuffer buffer = images[0].getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); long time = System.currentTimeMillis(); if (mCameraProxy.isFrontCamera()) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.d(TAG, "BitmapFactory.decodeByteArray time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); // 前置摄像头需要左右镜像 Bitmap rotateBitmap = ImageUtils.rotateBitmap(bitmap, 0, true, true); Log.d(TAG, "rotateBitmap time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); ImageUtils.saveBitmap(rotateBitmap); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); rotateBitmap.recycle(); } else { ImageUtils.saveImage(bytes); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); } images[0].close(); return ImageUtils.getLatestThumbBitmap(); }
Example #6
Source File: TextureCamera2Activity.java From CameraDemo with Apache License 2.0 | 6 votes |
@Override protected Bitmap doInBackground(Image ... images) { ByteBuffer buffer = images[0].getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); long time = System.currentTimeMillis(); if (mCameraProxy.isFrontCamera()) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.d(TAG, "BitmapFactory.decodeByteArray time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); // 前置摄像头需要左右镜像 Bitmap rotateBitmap = ImageUtils.rotateBitmap(bitmap, 0, true, true); Log.d(TAG, "rotateBitmap time: " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); ImageUtils.saveBitmap(rotateBitmap); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); rotateBitmap.recycle(); } else { ImageUtils.saveImage(bytes); Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time)); } images[0].close(); return ImageUtils.getLatestThumbBitmap(); }
Example #7
Source File: VNCServer.java From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void push(Image image, int pixelFormat) { Image.Plane[] planes = image.getPlanes(); ByteBuffer b = planes[0].getBuffer(); if (pixelFormat == PixelFormat.RGBA_8888) { // planes[0].getPixelStride() has to be 4 (32 bit) jni_push_pixels_rgba_8888(b, planes[0].getRowStride()); } else if (pixelFormat == PixelFormat.RGB_565) { // planes[0].getPixelStride() has to be 16 (16 bit) jni_push_pixels_rgba_565(b, planes[0].getRowStride()); } else { Log.e(TAG, "Image reader acquired unsupported image format " + pixelFormat); } }
Example #8
Source File: DngCreator.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
/** * Set the thumbnail image. * * <p> * Pixel data is interpreted as a {@link android.graphics.ImageFormat#YUV_420_888} image. * Thumbnail images with a dimension larger than {@link #MAX_THUMBNAIL_DIMENSION} will be * rejected. * </p> * * @param pixels an {@link android.media.Image} object with the format * {@link android.graphics.ImageFormat#YUV_420_888}. * @return this {@link #DngCreator} object. * @throws java.lang.IllegalArgumentException if the given thumbnail image has a dimension * larger than {@link #MAX_THUMBNAIL_DIMENSION}. */ @NonNull public DngCreator setThumbnail(@NonNull Image pixels) { if (pixels == null) { throw new IllegalArgumentException("Null argument to setThumbnail"); } int format = pixels.getFormat(); if (format != ImageFormat.YUV_420_888) { throw new IllegalArgumentException("Unsupported Image format " + format); } int width = pixels.getWidth(); int height = pixels.getHeight(); if (width > MAX_THUMBNAIL_DIMENSION || height > MAX_THUMBNAIL_DIMENSION) { throw new IllegalArgumentException("Thumbnail dimensions width,height (" + width + "," + height + ") too large, dimensions must be smaller than " + MAX_THUMBNAIL_DIMENSION); } ByteBuffer rgbBuffer = convertToRGB(pixels); nativeSetThumbnail(rgbBuffer, width, height); return this; }
Example #9
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; } visionImage = FritzVisionImage.fromMediaImage(image, orientation); image.close(); }
Example #10
Source File: ImagePreprocessor.java From androidthings-imageclassifier with Apache License 2.0 | 6 votes |
public Bitmap preprocessImage(final Image image) { if (image == null) { return null; } Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth()); Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight()); if (croppedBitmap != null && rgbFrameBitmap != null) { ByteBuffer bb = image.getPlanes()[0].getBuffer(); rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb)); cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0); } image.close(); // For debugging if (SAVE_PREVIEW_BITMAP) { saveBitmap(croppedBitmap); } return croppedBitmap; }
Example #11
Source File: ImagePreprocessor.java From androidthings-imageclassifier with Apache License 2.0 | 6 votes |
public Bitmap preprocessImage(final Image image) { if (image == null) { return null; } Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth()); Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight()); if (croppedBitmap != null && rgbFrameBitmap != null) { ByteBuffer bb = image.getPlanes()[0].getBuffer(); rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb)); cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0); } image.close(); // For debugging if (SAVE_PREVIEW_BITMAP) { saveBitmap(croppedBitmap); } return croppedBitmap; }
Example #12
Source File: Camera2BasicFragment.java From Cam2Caption with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onImageAvailable(ImageReader reader) { Image image = null; try { image = reader.acquireLatestImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] imageBytes = new byte[buffer.remaining()]; buffer.get(imageBytes); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); final String text = runModel(bitmap); getActivity().runOnUiThread(new Runnable() { @Override public void run() { textView.setText(text); } }); } finally { if (image != null) { image.close(); } } }
Example #13
Source File: AndroidImageReaderProxy.java From Camera2 with Apache License 2.0 | 6 votes |
@Override @Nullable public ImageProxy acquireNextImage() { synchronized (mLock) { Image image = mDelegate.acquireNextImage(); if (image == null) { return null; } else { return new AndroidImageProxy(image); } } }
Example #14
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 #15
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 #16
Source File: DirectChain.java From CameraCompat with MIT License | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void sendNormalImage(Image image) { if (mIsFrontCamera && mEnableMirror) { if (mRotation == Rotation.ROTATION_90) { RgbYuvConverter.image2yuvCropFlip(image, mVideoHeight, mGLYuvBuffer.array()); } else { RgbYuvConverter.image2yuvCropRotateC180Flip(image, mVideoHeight, mGLYuvBuffer.array()); } } else { if (mRotation == Rotation.ROTATION_90) { RgbYuvConverter.image2yuvCropRotateC180(image, mVideoHeight, mGLYuvBuffer.array()); } else { RgbYuvConverter.image2yuvCrop(image, mVideoHeight, mGLYuvBuffer.array()); } } mVideoCaptureCallback.onFrameData(mGLYuvBuffer.array(), image.getWidth(), mVideoHeight); }
Example #17
Source File: GPUImageChain.java From CameraCompat with MIT License | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void sendNormalImage(Image image) { if (mIsFrontCamera && mEnableMirror) { if (mGLRender.getRotation() == Rotation.ROTATION_90) { RgbYuvConverter.image2yuvCropFlip(image, mGLRender.getVideoHeight(), mGLYuvBuffer.array()); } else { RgbYuvConverter.image2yuvCropRotateC180Flip(image, mGLRender.getVideoHeight(), mGLYuvBuffer.array()); } } else { if (mGLRender.getRotation() == Rotation.ROTATION_90) { RgbYuvConverter.image2yuvCropRotateC180(image, mGLRender.getVideoHeight(), mGLYuvBuffer.array()); } else { RgbYuvConverter.image2yuvCrop(image, mGLRender.getVideoHeight(), mGLYuvBuffer.array()); } } mVideoCaptureCallback.onFrameData(mGLYuvBuffer.array(), image.getWidth(), mGLRender.getVideoHeight()); }
Example #18
Source File: ImageCapture.java From DoraemonKit with Apache License 2.0 | 6 votes |
void capture() { if (isCapturing) { return; } if (mImageReader == null) { return; } isCapturing = true; Image image = mImageReader.acquireLatestImage(); if (image == null) { return; } int width = image.getWidth(); int height = image.getHeight(); Image.Plane[] planes = image.getPlanes(); ByteBuffer buffer = planes[0].getBuffer(); int pixelStride = planes[0].getPixelStride(); int rowStride = planes[0].getRowStride(); int rowPaddingStride = rowStride - pixelStride * width; int rowPadding = rowPaddingStride / pixelStride; Bitmap recordBitmap = Bitmap.createBitmap(width + rowPadding, height, Bitmap.Config.ARGB_8888); recordBitmap.copyPixelsFromBuffer(buffer); mBitmap = Bitmap.createBitmap(recordBitmap, 0, 0, width, height); image.close(); isCapturing = false; }
Example #19
Source File: BaseRecordingActivity.java From fritz-examples with MIT License | 6 votes |
@Override public void onImageAvailable(final ImageReader reader) { Image image = reader.acquireLatestImage(); if (image == null) { return; } // Save Images when we're recording if (!isRecording.get()) { image.close(); return; } // Only grab a frame every 100ms if (System.currentTimeMillis() - lastRecordedFrameAt.get() < TIME_BETWEEN_FRAMES_MS) { image.close(); return; } // Add the frame to a queue to process lastRecordedFrameAt.set(System.currentTimeMillis()); final FritzVisionImage fritzImage = FritzVisionImage.fromMediaImage(image, orientation); videoProcessingQueue.addVisionImage(fritzImage); image.close(); }
Example #20
Source File: ImagePreprocessor.java From androidthings-imageclassifier with Apache License 2.0 | 6 votes |
public Bitmap preprocessImage(final Image image) { if (image == null) { return null; } Assert.assertEquals("Invalid size width", rgbFrameBitmap.getWidth(), image.getWidth()); Assert.assertEquals("Invalid size height", rgbFrameBitmap.getHeight(), image.getHeight()); if (croppedBitmap != null && rgbFrameBitmap != null) { ByteBuffer bb = image.getPlanes()[0].getBuffer(); rgbFrameBitmap = BitmapFactory.decodeStream(new ByteBufferBackedInputStream(bb)); cropAndRescaleBitmap(rgbFrameBitmap, croppedBitmap, 0); } image.close(); // For debugging if (SAVE_PREVIEW_BITMAP) { saveBitmap(croppedBitmap); } return croppedBitmap; }
Example #21
Source File: QRReaderActivity.java From DeviceConnect-Android with MIT License | 5 votes |
/** * {@link Image} から {@link Bitmap} を作成します. * <p> * デコードに失敗した場合は null を返却します。 * </p> * @param image カメラのプレビュー * @return {@link Bitmap} のインスタンス */ private Bitmap createBitmapFromImage(final Image image) { try { ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] jpeg = new byte[buffer.remaining()]; buffer.get(jpeg); return BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length); } catch (Throwable t) { return null; } }
Example #22
Source File: MainActivity.java From androidthings-cameraCar with Apache License 2.0 | 5 votes |
@Override public void onImageAvailable(ImageReader reader) { Log.d(TAG, "PhotoCamera OnImageAvailableListener"); 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 #23
Source File: RgbYuvConverter.java From CameraCompat with MIT License | 5 votes |
/** * rotate 180 degree in counter clockwise and change to yuv */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static int image2yuvCropRotateC180(Image imageIn, int outputHeight, byte[] yuvOut) { Image.Plane[] planes = imageIn.getPlanes(); ByteBuffer Y = planes[0].getBuffer(); ByteBuffer Cr = planes[2].getBuffer(); int CrPixelStride = planes[2].getPixelStride(); ByteBuffer Cb = planes[1].getBuffer(); int CbPixelStride = planes[1].getPixelStride(); return image2yuvCropRotateC180(imageIn.getWidth(), imageIn.getHeight(), Y, Cr, Cb, CrPixelStride, CbPixelStride, outputHeight, yuvOut); }
Example #24
Source File: CameraHelp2.java From WeiXinRecordedDemo with MIT License | 5 votes |
private byte[] getYUVI420(Image image){ int width = image.getWidth(); int height = image.getHeight(); byte[] yuvI420 = new byte[image.getWidth()*image.getHeight()*3/2]; byte[] yData = new byte[image.getPlanes()[0].getBuffer().remaining()]; byte[] uData = new byte[image.getPlanes()[1].getBuffer().remaining()]; byte[] vData = new byte[image.getPlanes()[2].getBuffer().remaining()]; image.getPlanes()[0].getBuffer().get(yData); image.getPlanes()[1].getBuffer().get(uData); image.getPlanes()[2].getBuffer().get(vData); System.arraycopy(yData, 0, yuvI420, 0, yData.length); int index = yData.length; for (int r = 0; r < height / 2; ++r) { for (int c = 0; c < width; c += 2) { //各一个byte存一个U值和V值 yuvI420[index++] = uData[r * width + c]; } } for (int r = 0; r < height / 2; ++r) { for (int c = 0; c < width; c += 2) { //各一个byte存一个U值和V值 yuvI420[index++] = vData[r * width + c]; } } return yuvI420; }
Example #25
Source File: RgbYuvConverter.java From CameraCompat with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static int image2yuvCropFlip(Image imageIn, int outputHeight, byte[] yuvOut) { Image.Plane[] planes = imageIn.getPlanes(); ByteBuffer Y = planes[0].getBuffer(); ByteBuffer Cr = planes[2].getBuffer(); int CrPixelStride = planes[2].getPixelStride(); ByteBuffer Cb = planes[1].getBuffer(); int CbPixelStride = planes[1].getPixelStride(); return image2yuvCropFlip(imageIn.getWidth(), imageIn.getHeight(), Y, Cr, Cb, CrPixelStride, CbPixelStride, outputHeight, yuvOut); }
Example #26
Source File: DirectChain.java From CameraCompat with MIT License | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onFrameData(Image image, Runnable postProcessedTask) { notifyVideoSizeChanged(image.getWidth(), image.getHeight()); sendNormalImage(image); postProcessedTask.run(); }
Example #27
Source File: Camera2Source.java From Machine-Learning-Projects-for-Mobile-Applications with MIT License | 5 votes |
private byte[] convertYUV420888ToNV21(Image imgYUV420) { // Converting YUV_420_888 data to NV21. byte[] data; ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer(); ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer(); int buffer0_size = buffer0.remaining(); int buffer2_size = buffer2.remaining(); data = new byte[buffer0_size + buffer2_size]; buffer0.get(data, 0, buffer0_size); buffer2.get(data, buffer0_size, buffer2_size); return data; }
Example #28
Source File: FileSystem.java From pixelvisualcorecamera with Apache License 2.0 | 5 votes |
/** * Saves an image to the file system. Should be called on a background thread. */ public static SaveImageResult saveImage(Context context, Image image, boolean isApi1) { Log.i(TAG, String.format("saveImage format: %d, h: %d, w: %d, timestamp: %d", image.getFormat(), image.getHeight(), image.getWidth(), image.getTimestamp())); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); return saveBytesToDiskAndReturnResult(context, buffer, isApi1); }
Example #29
Source File: HyperionScreenEncoder.java From hyperion-android-grabber with MIT License | 5 votes |
@Override public void resumeRecording() { if (DEBUG) Log.i(TAG, "resumeRecording Called"); if (!isCapturing() && mImageReader != null) { if (DEBUG) Log.i(TAG, "Resuming reading images"); Image img = mImageReader.acquireNextImage(); setCapturing(true); if (img != null) { img.close(); } } }
Example #30
Source File: ScreenShotActivity.java From TVRemoteIME with GNU General Public License v2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.KITKAT) public void startScreenShot() { virtualDisplay(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Image image = mImageReader.acquireLatestImage(); AsyncTaskCompat.executeParallel(new ScreenShotSaveTask(), image); } }, 300); }