Java Code Examples for android.media.Image#Plane

The following examples show how to use android.media.Image#Plane . 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: DngCreator.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Write the pixel data to a DNG file with the currently configured metadata.
 *
 * <p>
 * For this method to succeed, the {@link android.media.Image} input must contain
 * {@link android.graphics.ImageFormat#RAW_SENSOR} pixel data, otherwise an
 * {@link java.lang.IllegalArgumentException} will be thrown.
 * </p>
 *
 * @param dngOutput an {@link java.io.OutputStream} to write the DNG file to.
 * @param pixels an {@link android.media.Image} to write.
 *
 * @throws java.io.IOException if an error was encountered in the output stream.
 * @throws java.lang.IllegalArgumentException if an image with an unsupported format was used.
 * @throws java.lang.IllegalStateException if not enough metadata information has been
 *          set to write a well-formatted DNG file.
 */
public void writeImage(@NonNull OutputStream dngOutput, @NonNull Image pixels)
        throws IOException {
    if (dngOutput == null) {
        throw new IllegalArgumentException("Null dngOutput to writeImage");
    } else if (pixels == null) {
        throw new IllegalArgumentException("Null pixels to writeImage");
    }

    int format = pixels.getFormat();
    if (format != ImageFormat.RAW_SENSOR) {
        throw new IllegalArgumentException("Unsupported image format " + format);
    }

    Image.Plane[] planes = pixels.getPlanes();
    if (planes == null || planes.length <= 0) {
        throw new IllegalArgumentException("Image with no planes passed to writeImage");
    }

    ByteBuffer buf = planes[0].getBuffer();
    writeByteBuffer(pixels.getWidth(), pixels.getHeight(), buf, dngOutput,
            planes[0].getPixelStride(), planes[0].getRowStride(), 0);
}
 
Example 2
Source File: SRManager.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 通过底层来获取下一帧的图像
 */
public Bitmap cutoutFrame() {
    Image image = imageReader.acquireLatestImage();
    if (image == null) {
        return null;
    }
    int width = image.getWidth();
    int height = image.getHeight();
    final Image.Plane[] planes = image.getPlanes();
    final ByteBuffer buffer = planes[0].getBuffer();
    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);
    return Bitmap.createBitmap(bitmap, 0, 0, width, height);
}
 
Example 3
Source File: ImageDecoder.java    From FastBarcodeScanner with Apache License 2.0 6 votes vote down vote up
public static byte[] Serialize(Image image)
{
    if (image==null)
        return null;

    Image.Plane[] planes = image.getPlanes();

    // NV21 expects planes in order YVU, not YUV:
    if (image.getFormat() == ImageFormat.YUV_420_888)
        planes = new Image.Plane[] {planes[0], planes[2], planes[1]};

    byte[] serializeBytes = new byte[getSerializedSize(image)];
    int nextFree = 0;

    for (Image.Plane plane: planes)
    {
        ByteBuffer buffer = plane.getBuffer();
        buffer.position(0);
        int nBytes = buffer.remaining();
        plane.getBuffer().get(serializeBytes, nextFree, nBytes);
        nextFree += nBytes;
    }

    return serializeBytes;
}
 
Example 4
Source File: ImageUtils.java    From ScreenCapture with MIT License 6 votes vote down vote up
/**
     * 这个方法可以转换,但是得到的图片右边多了一列,比如上面方法得到1080x2160,这个方法得到1088x2160
     * 所以要对得到的Bitmap裁剪一下
     *
     * @param image
     * @param config
     * @return
     */
    public static Bitmap image_2_bitmap(Image image, Bitmap.Config config) {

        int width = image.getWidth();
        int height = image.getHeight();
        Bitmap bitmap;

        final Image.Plane[] planes = image.getPlanes();
        final ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        int rowStride = planes[0].getRowStride();
        int rowPadding = rowStride - pixelStride * width;
        Log.d("WOW",
                "pixelStride:" + pixelStride + ". rowStride:" + rowStride + ". rowPadding" + rowPadding);

        bitmap = Bitmap.createBitmap(
                width + rowPadding / pixelStride/*equals: rowStride/pixelStride */
                , height, config);
        bitmap.copyPixelsFromBuffer(buffer);

        return Bitmap.createBitmap(bitmap, 0, 0, width, height);
//        return bitmap;
    }
 
Example 5
Source File: ImageCapture.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
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 6
Source File: RgbYuvConverter.java    From CameraCompat with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static int image2yuvCrop(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 image2yuvCrop(imageIn.getWidth(), imageIn.getHeight(), Y, Cr, Cb, CrPixelStride,
            CbPixelStride, outputHeight, yuvOut);
}
 
Example 7
Source File: CameraActivity.java    From next18-ai-in-motion with Apache License 2.0 5 votes vote down vote up
protected void fillBytes(final Image.Plane[] planes, final byte[][] yuvBytes) {
    // Because of the variable row stride it's not possible to know in
    // advance the actual necessary dimensions of the yuv planes.
    for (int i = 0; i < planes.length; ++i) {
        final ByteBuffer buffer = planes[i].getBuffer();
        if (yuvBytes[i] == null) {
            yuvBytes[i] = new byte[buffer.capacity()];
        }
        buffer.get(yuvBytes[i]);
    }
}
 
Example 8
Source File: ImageDecoder.java    From FastBarcodeScanner with Apache License 2.0 5 votes vote down vote up
private static void getNV21(Image src, byte[] dest)
{
    // Check nPlanes etc.
    Image.Plane yPlane = src.getPlanes()[0];
    Image.Plane uPlane = src.getPlanes()[1];
    Image.Plane vPlane = src.getPlanes()[2];

    int ySize = yPlane.getBuffer().capacity();
    int uSize = uPlane.getBuffer().capacity();
    int vSize = vPlane.getBuffer().capacity();

    if (ySize != src.getWidth() * src.getHeight())
        throw new RuntimeException("Y-plane in planar YUV_420_888 is expected to be width*height bytes");

    if (ySize != 2 * (uSize + 1))
        throw new RuntimeException("U-plane in planar YUV_420_888 is expected to be (width*height/2 - 1) bytes");

    if (ySize != 2 * (vSize + 1))
        throw new RuntimeException("V-plane in planar YUV_420_888 is expected to be (width*height/2 - 1) bytes");

    //int nextFree = getNonInterleaved(yPlane.getBuffer(), dest, 0);
    //getInterleaved(vPlane.getBuffer(), 2, dest, nextFree, 2);
    //getInterleaved(uPlane.getBuffer(), 2, dest, nextFree + 1, 2);
    int nextFree = 0;
    nextFree += getNonInterleaved(yPlane.getBuffer(), dest, nextFree);
    nextFree += getNonInterleaved(vPlane.getBuffer(), dest, nextFree);
    nextFree += getNonInterleaved(uPlane.getBuffer(), dest, nextFree);
}
 
Example 9
Source File: Camera2.java    From cameraview with Apache License 2.0 5 votes vote down vote up
@Override
public void onImageAvailable(ImageReader reader) {
    try (Image image = reader.acquireNextImage()) {
        Image.Plane[] planes = image.getPlanes();
        if (planes.length > 0) {
            ByteBuffer buffer = planes[0].getBuffer();
            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);
            mCallback.onPictureTaken(data);
        }
    }
}
 
Example 10
Source File: AndroidImageProxy.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public Plane(Image.Plane imagePlane)
{
    // Copying out the contents of the Image.Plane means that this Plane
    // implementation can be thread-safe (without requiring any locking)
    // and can have getters which do not throw a RuntimeException if
    // the underlying Image is closed.
    mPixelStride = imagePlane.getPixelStride();
    mRowStride = imagePlane.getRowStride();
    mBuffer = imagePlane.getBuffer();
}
 
Example 11
Source File: AndroidImageProxy.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public AndroidImageProxy(android.media.Image image)
{
    mLock = new Object();

    mImage = image;
    // Copying out the contents of the Image means that this Image
    // implementation can be thread-safe (without requiring any locking)
    // and can have getters which do not throw a RuntimeException if
    // the underlying Image is closed.
    mFormat = mImage.getFormat();
    mWidth = mImage.getWidth();
    mHeight = mImage.getHeight();
    mTimestamp = mImage.getTimestamp();

    Image.Plane[] planes;
    planes = mImage.getPlanes();
    if (planes == null)
    {
        mPlanes = ImmutableList.of();
    } else
    {
        List<ImageProxy.Plane> wrappedPlanes = new ArrayList<>(planes.length);
        for (int i = 0; i < planes.length; i++)
        {
            wrappedPlanes.add(new Plane(planes[i]));
        }
        mPlanes = ImmutableList.copyOf(wrappedPlanes);
    }
}
 
Example 12
Source File: ImageUtils.java    From android-yolo-v2 with Do What The F*ck You Want To Public License 5 votes vote down vote up
private static byte[][] fillBytes(final Image.Plane[] planes) {
    byte[][] yuvBytes = new byte[3][];
    for (int i = 0; i < planes.length; ++i) {
        final ByteBuffer buffer = planes[i].getBuffer();
        if (yuvBytes[i] == null) {
            yuvBytes[i] = new byte[buffer.capacity()];
        }
        buffer.get(yuvBytes[i]);
    }

    return yuvBytes;
}
 
Example 13
Source File: Camera2.java    From LockDemo with Apache License 2.0 5 votes vote down vote up
@Override
public void onImageAvailable(ImageReader reader) {
    try (Image image = reader.acquireNextImage()) {
        Image.Plane[] planes = image.getPlanes();
        if (planes.length > 0) {
            ByteBuffer buffer = planes[0].getBuffer();
            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);
            mCallback.onPictureTaken(data);
        }
    }
}
 
Example 14
Source File: ImageUtils.java    From ScreenCapture with MIT License 5 votes vote down vote up
public static Bitmap image_ARGB8888_2_bitmap(DisplayMetrics metrics, Image image) {
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();

        int width = image.getWidth();
//        Log.d("WOW", "image w = " + width);
        int height = image.getHeight();
//        Log.d("WOW", "image h = " + height);

        int pixelStride = planes[0].getPixelStride();
//        Log.d("WOW", "pixelStride is " + pixelStride);
        int rowStride = planes[0].getRowStride();
//        Log.d("WOW", "row Stride is " + rowStride);
        int rowPadding = rowStride - pixelStride * width;
//        Log.d("WOW", "rowPadding is " + rowPadding);

        int offset = 0;
        Bitmap bitmap;
        bitmap = Bitmap.createBitmap(metrics, width, height, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                int pixel = 0;
                pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                pixel |= (buffer.get(offset + 2) & 0xff);       // B
                pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                bitmap.setPixel(j, i, pixel);
                offset += pixelStride;
            }
            offset += rowPadding;
        }
        return bitmap;
    }
 
Example 15
Source File: Camera2.java    From MediaPickerInstagram with Apache License 2.0 5 votes vote down vote up
@Override
public void onImageAvailable(ImageReader reader) {
    try (Image image = reader.acquireNextImage()) {
        Image.Plane[] planes = image.getPlanes();
        if (planes.length > 0) {
            ByteBuffer buffer = planes[0].getBuffer();
            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);
            mCallback.onPictureTaken(data);
        }
    }
}
 
Example 16
Source File: ScreenCapturer.java    From habpanelviewer with GNU General Public License v3.0 5 votes vote down vote up
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 17
Source File: ImageUtils.java    From android-yolo-v2 with Do What The F*ck You Want To Public License 4 votes vote down vote up
public static int[] convertYUVToARGB(final Image image, final int previewWidth, final int previewHeight) {
    final Image.Plane[] planes = image.getPlanes();
    byte[][] yuvBytes = fillBytes(planes);
    return ImageUtils.convertYUV420ToARGB8888(yuvBytes[0], yuvBytes[1], yuvBytes[2], previewWidth,
            previewHeight, planes[0].getRowStride(), planes[1].getRowStride(), planes[1].getPixelStride());
}
 
Example 18
Source File: ResultProcessor.java    From libsoftwaresync with Apache License 2.0 4 votes vote down vote up
private static YuvImage yuvImageFromNv21Image(Image src) {
  long t0 = System.nanoTime();

  Image.Plane[] planes = src.getPlanes();
  Image.Plane luma = planes[0];
  Image.Plane chromaU = planes[1];
  Image.Plane chromaV = planes[2];

  int width = src.getWidth();
  int height = src.getHeight();

  // Luma should be tightly packed and chroma should be tightly interleaved.
  assert (luma.getPixelStride() == 1);
  assert (chromaU.getPixelStride() == 2);
  assert (chromaV.getPixelStride() == 2);

  // Duplicate (shallow copy) each buffer so as to not disturb the underlying position/limit/etc.
  ByteBuffer lumaBuffer = luma.getBuffer().duplicate();
  ByteBuffer chromaUBuffer = chromaU.getBuffer().duplicate();
  ByteBuffer chromaVBuffer = chromaV.getBuffer().duplicate();

  // Yes, y, v, then u since it's NV21.
  int[] yvuRowStrides =
      new int[] {luma.getRowStride(), chromaV.getRowStride(), chromaU.getRowStride()};

  // Compute bytes needed to concatenate all the (potentially padded) YUV data in one buffer.
  int lumaBytes = height * luma.getRowStride();
  int interleavedChromaBytes = (height / 2) * chromaV.getRowStride();
  assert (lumaBuffer.capacity() == lumaBytes);
  int packedYVUBytes = lumaBytes + interleavedChromaBytes;
  byte[] packedYVU = new byte[packedYVUBytes];

  int packedYVUOffset = 0;
  lumaBuffer.get(
      packedYVU,
      packedYVUOffset,
      lumaBuffer.capacity()); // packedYVU[0..lumaBytes) <-- lumaBuffer.
  packedYVUOffset += lumaBuffer.capacity();

  // Write the V buffer. Since the V buffer contains U data, write all of V and then check how
  // much U data is left over. There be at most 1 byte plus padding.
  chromaVBuffer.get(packedYVU, packedYVUOffset, /*length=*/ chromaVBuffer.capacity());
  packedYVUOffset += chromaVBuffer.capacity();

  // Write the remaining portion of the U buffer (if any).
  int chromaUPosition = chromaVBuffer.capacity() - 1;
  if (chromaUPosition < chromaUBuffer.capacity()) {
    chromaUBuffer.position(chromaUPosition);

    int remainingBytes = Math.min(chromaUBuffer.remaining(), lumaBytes - packedYVUOffset);

    if (remainingBytes > 0) {
      chromaUBuffer.get(packedYVU, packedYVUOffset, remainingBytes);
    }
  }
  YuvImage yuvImage = new YuvImage(packedYVU, ImageFormat.NV21, width, height, yvuRowStrides);

  long t1 = System.nanoTime();
  Log.i(TAG, String.format("yuvImageFromNv212Image took %f ms.", (t1 - t0) * 1e-6f));

  return yuvImage;
}
 
Example 19
Source File: DngCreator.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a direct RGB {@link ByteBuffer} from a YUV420_888 {@link Image}.
 */
private static ByteBuffer convertToRGB(Image yuvImage) {
    // TODO: Optimize this with renderscript intrinsic.
    int width = yuvImage.getWidth();
    int height = yuvImage.getHeight();
    ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_RGB_PIX * width * height);

    Image.Plane yPlane = yuvImage.getPlanes()[0];
    Image.Plane uPlane = yuvImage.getPlanes()[1];
    Image.Plane vPlane = yuvImage.getPlanes()[2];

    ByteBuffer yBuf = yPlane.getBuffer();
    ByteBuffer uBuf = uPlane.getBuffer();
    ByteBuffer vBuf = vPlane.getBuffer();

    yBuf.rewind();
    uBuf.rewind();
    vBuf.rewind();

    int yRowStride = yPlane.getRowStride();
    int vRowStride = vPlane.getRowStride();
    int uRowStride = uPlane.getRowStride();

    int yPixStride = yPlane.getPixelStride();
    int vPixStride = vPlane.getPixelStride();
    int uPixStride = uPlane.getPixelStride();

    byte[] yuvPixel = { 0, 0, 0 };
    byte[] yFullRow = new byte[yPixStride * (width - 1) + 1];
    byte[] uFullRow = new byte[uPixStride * (width / 2 - 1) + 1];
    byte[] vFullRow = new byte[vPixStride * (width / 2 - 1) + 1];
    byte[] finalRow = new byte[BYTES_PER_RGB_PIX * width];
    for (int i = 0; i < height; i++) {
        int halfH = i / 2;
        yBuf.position(yRowStride * i);
        yBuf.get(yFullRow);
        uBuf.position(uRowStride * halfH);
        uBuf.get(uFullRow);
        vBuf.position(vRowStride * halfH);
        vBuf.get(vFullRow);
        for (int j = 0; j < width; j++) {
            int halfW = j / 2;
            yuvPixel[0] = yFullRow[yPixStride * j];
            yuvPixel[1] = uFullRow[uPixStride * halfW];
            yuvPixel[2] = vFullRow[vPixStride * halfW];
            yuvToRgb(yuvPixel, j * BYTES_PER_RGB_PIX, /*out*/finalRow);
        }
        buf.put(finalRow);
    }

    yBuf.rewind();
    uBuf.rewind();
    vBuf.rewind();
    buf.rewind();
    return buf;
}
 
Example 20
Source File: ImageUtils.java    From FastBarcodeScanner with Apache License 2.0 4 votes vote down vote up
/**
 * Takes an Android Image in the YUV_420_888 format and returns an OpenCV Mat.
 *
 * @param image Image in the YUV_420_888 format.
 * @return OpenCV Mat.
 */
public static byte[] imageToMat(Image image) {
    ByteBuffer buffer;
    int rowStride;
    int pixelStride;
    int pixelWidth = image.getWidth();
    int pixelHeight = image.getHeight();
    int encodedRowStart = 0;

    Image.Plane[] planes = image.getPlanes();
    int bytesPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;
    byte[] pixels = new byte[image.getWidth() * image.getHeight() * bytesPerPixel];
    byte[] rowData = new byte[planes[0].getRowStride()];

    for (int i = 0; i < planes.length; i++) {
        buffer = planes[i].getBuffer();
        rowStride = planes[i].getRowStride();
        pixelStride = planes[i].getPixelStride();
        int encodedWidthInPixels = (i == 0) ? pixelWidth : pixelWidth / 2;
        int encodedHeightInPixels = (i == 0) ? pixelHeight : pixelHeight / 2;
        for (int row = 0; row < encodedHeightInPixels; row++) {
            if (pixelStride == bytesPerPixel) {
                int encodedWidthInBytes = encodedWidthInPixels * bytesPerPixel;
                buffer.get(pixels, encodedRowStart, encodedWidthInBytes);

                // Advance buffer the remainder of the row stride, unless on the last row.
                // Otherwise, this will throw an IllegalArgumentException because the buffer
                // doesn't include the last padding.
                if (encodedHeightInPixels - row != 1) {
                    int padding = rowStride - encodedWidthInBytes;
                    buffer.position(buffer.position() + padding);
                }
                encodedRowStart += encodedWidthInBytes;
            } else {

                // On the last row only read the width of the image minus the pixel stride
                // plus one. Otherwise, this will throw a BufferUnderflowException because the
                // buffer doesn't include the last padding.
                if (encodedHeightInPixels - row == 1) {
                    buffer.get(rowData, 0, pixelWidth - pixelStride + 1);
                } else {
                    buffer.get(rowData, 0, rowStride);
                }

                for (int col = 0; col < encodedWidthInPixels; col++) {
                    pixels[encodedRowStart++] = rowData[col * pixelStride];
                }
            }
        }
    }

    // Finally, create the Mat.
    //Mat mat = new Mat(pixelHeight + pixelHeight / 2, pixelWidth, CvType.CV_8UC1);
    //mat.put(0, 0, pixels);

    return pixels;
}