Java Code Examples for android.media.Image#getPlanes()

The following examples show how to use android.media.Image#getPlanes() . 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: ImageUtils.java    From SimpleSmsRemote with MIT License 8 votes vote down vote up
/**
 * Retrieve Bitmap with specific format from ImageReader.
 *
 * @param imageReader the image reader
 * @return bitmap
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static Bitmap GetBitmapFromImageReader(ImageReader imageReader) {
    Bitmap bitmap;

    //get image buffer
    Image image = imageReader.acquireLatestImage();
    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 * image.getWidth();
    // create bitmap
    bitmap = Bitmap.createBitmap(image.getWidth() + rowPadding / pixelStride, image.getHeight(), Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    image.close();
    return bitmap;
}
 
Example 2
Source File: CameraUtil.java    From AndroidDemo with MIT License 8 votes vote down vote up
/**
     * 获取流
     *
     * @param image       预览帧
     * @param orientation 旋转方向
     */
    private byte[][] getBytes(Image image, int orientation) {
//        if (capacity == -1) {
//            size = getSize();//Size
//            capacity = (int) (size.getWidth() * size.getHeight() * 1.5);
//        }
        int len = image.getPlanes().length;
        byte[][] bytes = new byte[len][];
        int count = 0;
        for (int i = 0; i < len; i++) {
            ByteBuffer buffer = image.getPlanes()[i].getBuffer();
            int remaining = buffer.remaining();
            byte[] data = new byte[remaining];
            buffer.get(data);
            bytes[i] = data;
//            bytes[i] = fixOrientation(data, size, orientation);
            count += remaining;
        }
        Log.d(TAG, "bytes = " + count);
        if (capacity == -1) {
            capacity = count;
        }
        return bytes;
    }
 
Example 3
Source File: VNCServer.java    From CatVision-io-SDK-Android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 4
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 5
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 6
Source File: ImageScreenCast.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
private Bitmap decodeToBitmap(final Image img) {
    Image.Plane[] planes = img.getPlanes();
    if (planes[0].getBuffer() == null) {
        return null;
    }

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

    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(planes[0].getBuffer());
    img.close();

    return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);
}
 
Example 7
Source File: Screenshotter.java    From loco-answers with GNU General Public License v3.0 6 votes vote down vote up
@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 8
Source File: ImageDecoder.java    From FastBarcodeScanner with Apache License 2.0 5 votes vote down vote up
public static byte[] ToNV21(Image image)
{
    if (image.getPlanes().length != 3)
        throw new RuntimeException("Expected 3 planes for planar YUV");

    byte[] nv21Bytes = new byte[getNV21Size(image)];
    getNV21(image, nv21Bytes);

    return nv21Bytes;
}
 
Example 9
Source File: ImageDecoder.java    From FastBarcodeScanner with Apache License 2.0 5 votes vote down vote up
private static int getSerializedSize(Image image) {
    int size = 0;

    for (Image.Plane plane: image.getPlanes())
    {
        size += plane.getBuffer().capacity();
    }

    return size;
}
 
Example 10
Source File: ScreenCaptureSocket.java    From pc-android-controller-android with Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap doInBackground(Image... params) {

    if (params == null || params.length < 1 || params[0] == null) {

        L.e(" params is null ...");
        return null;
    }

    Image image = params[0];

    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();
    image.close();
    int rowPadding = rowStride - pixelStride * width;
    Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
    image.close();

    compressAndWrite(bitmap);

    return null;
}
 
Example 11
Source File: HyperionScreenEncoder.java    From hyperion-android-grabber with MIT License 5 votes vote down vote up
private void sendImage(Image img) {
    Image.Plane plane = img.getPlanes()[0];
    ByteBuffer buffer = plane.getBuffer();

    int width = img.getWidth();
    int height = img.getHeight();
    int pixelStride = plane.getPixelStride();
    int rowStride = plane.getRowStride();
    int firstX = 0;
    int firstY = 0;

    if (mRemoveBorders || mAvgColor) {
        mBorderProcessor.parseBorder(buffer, width, height, rowStride, pixelStride);
        BorderProcessor.BorderObject border = mBorderProcessor.getCurrentBorder();
        if (border != null && border.isKnown()) {
            firstX = border.getHorizontalBorderIndex();
            firstY = border.getVerticalBorderIndex();
        }
    }

    if (mAvgColor) {
        mListener.sendFrame(
                getAverageColor(buffer, width, height, rowStride, pixelStride, firstX, firstY),
                1,
                1
        );
    } else {
        mListener.sendFrame(
                getPixels(buffer, width, height, rowStride, pixelStride, firstX, firstY),
                width - firstX * 2,
                height - firstY * 2
        );
    }
}
 
Example 12
Source File: AutomotivePlanner.java    From VIA-AI with MIT License 5 votes vote down vote up
/**
 *  Buffer image frame to task.
 * @param missionID buffer target task.
 * @param frame is the object of {@link android.media.Image}.
 * @param roiX the x of roi.
 * @param roiY the y of roi.
 * @param roiWidth the width of roi.
 * @param roiHeight the height of roi.
 * @return true will return if operation success, otherwise, false will return. if the task is inactive, this operation will return false.
 * @exception IllegalArgumentException will throw if the channel of frame not 3.
 */
public boolean bufferFrame(@NonNull SensingTaskID missionID, @NonNull Image frame, int roiX, int roiY, int roiWidth, int roiHeight) {
    boolean ret = false;

    SensingUnit unit = mSensingUnitList.get(missionID.ordinal());
    if(frame != null && unit != null && unit.module != null && unit.task != null) {
        if(frame.getPlanes().length == 3) { // YUV or RGB
            Image.Plane yPlane = frame.getPlanes()[0];
            Image.Plane uPlane = frame.getPlanes()[1];
            Image.Plane vPlane = frame.getPlanes()[2];

            // TODO : check frame format is support or not.
            boolean isFormatValid = true;

            // buffer frane
            if(isFormatValid) {
                ret = unit.module.bufferFrame(yPlane.getBuffer(), uPlane.getBuffer(), vPlane.getBuffer(),
                        frame.getWidth(), frame.getHeight(),
                        yPlane.getRowStride(), uPlane.getRowStride(), vPlane.getRowStride(),
                        yPlane.getPixelStride(), uPlane.getPixelStride(), vPlane.getPixelStride(),
                        roiX, roiY, roiWidth, roiHeight);
                unit.task.notifyFrameReady();
            }
        }
        else {
            throw new IllegalArgumentException("The channel of source frame must be 3 , current is " + frame.getPlanes().length);
        }
    }
    return ret;
}
 
Example 13
Source File: OneCameraImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
/**
 * Given an image reader, this extracts the final image. If the image in the
 * reader is JPEG, we extract and return it as is. If the image is YUV, we
 * convert it to JPEG and return the result.
 *
 * @param image the image we got from the image reader.
 * @return A valid JPEG image.
 */
private static byte[] acquireJpegBytesAndClose(Image image)
{
    ByteBuffer buffer;
    if (image.getFormat() == ImageFormat.JPEG)
    {
        Image.Plane plane0 = image.getPlanes()[0];
        buffer = plane0.getBuffer();
    } else if (image.getFormat() == ImageFormat.YUV_420_888)
    {
        buffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 3);

        Log.v(TAG, "Compressing JPEG with software encoder.");
        int numBytes = JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(image), buffer,
                JPEG_QUALITY);

        if (numBytes < 0)
        {
            throw new RuntimeException("Error compressing jpeg.");
        }
        buffer.limit(numBytes);
    } else
    {
        throw new RuntimeException("Unsupported image format.");
    }

    byte[] imageBytes = new byte[buffer.remaining()];
    buffer.get(imageBytes);
    buffer.rewind();
    image.close();
    return imageBytes;
}
 
Example 14
Source File: RgbYuvConverter.java    From CameraCompat with MIT License 5 votes vote down vote up
/**
 * 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 15
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 16
Source File: HeifReader.java    From heifreader with MIT License 4 votes vote down vote up
private static Bitmap convertRgb565ToBitmap(Image image) {
    Bitmap bmp = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.RGB_565);
    Image.Plane[] planes = image.getPlanes();
    bmp.copyPixelsFromBuffer(planes[0].getBuffer());
    return bmp;
}
 
Example 17
Source File: ImageUtils.java    From FastBarcodeScanner with Apache License 2.0 4 votes vote down vote up
public static byte[] getPlane(Image image, int planeNo)
{
    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 + col] = rowData[col * pixelStride];
                }
                //encodedRowStart += encodedWidthInBytes;
            }
        }
    }

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

    return pixels;
}
 
Example 18
Source File: CameraActivity.java    From dbclf with Apache License 2.0 4 votes vote down vote up
/**
 * Callback for Camera2 API
 */
@Override
public void onImageAvailable(final ImageReader reader) {
    //We need to wait until we have some size from onPreviewSizeChosen
    if (previewWidth == 0 || previewHeight == 0) {
        return;
    }
    if (rgbBytes == null) {
        rgbBytes = new int[previewWidth * previewHeight];
    }
    try {
        final Image image = reader.acquireLatestImage();

        if (image == null) {
            return;
        }

        if (isProcessingFrame) {
            image.close();
            return;
        }
        isProcessingFrame = true;
        final Plane[] planes = image.getPlanes();
        fillBytes(planes, yuvBytes);
        yRowStride = planes[0].getRowStride();
        final int uvRowStride = planes[1].getRowStride();
        final int uvPixelStride = planes[1].getPixelStride();

        imageConverter = () -> ImageUtils.convertYUV420ToARGB8888(
                yuvBytes[0],
                yuvBytes[1],
                yuvBytes[2],
                previewWidth,
                previewHeight,
                yRowStride,
                uvRowStride,
                uvPixelStride,
                rgbBytes);

        postInferenceCallback = () -> {
            image.close();
            isProcessingFrame = false;
        };

        processImage();
    } catch (final Exception ignored) {
    }
}
 
Example 19
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 20
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;
}