Package 

Class ImageProcessor

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable

    
    public final class ImageProcessor
     implements Closeable
                        

    Converts camera frames and bitmaps into the normalized [1, 3, H, W] float tensors vision models expect, replacing the hand-rolled resize/normalize loops apps otherwise write per model.

    The pipeline rotates the source upright according to the supplied ImageOrientation, resizes it per ImageProcessorConfig.resizeMode, and normalizes it per ImageProcessorConfig.normalization. The resize and normalize steps use NEON where available.

    Warning: These APIs are experimental and subject to change without notice

    Thread-safety: an ImageProcessor is NOT thread-safe. Internal scratch buffers are reused across calls, so concurrent calls on one instance are unsafe. Use one instance per thread; separate instances are independent.

    • Constructor Detail

      • ImageProcessor

        ImageProcessor(ImageProcessorConfig config)
        Parameters:
        config - The target size, resize mode, and normalization to apply.
    • Method Detail

      • close

         Unit close()

        Releases native resources. Call this when done with the processor.

      • process

        @JvmOverloads() final Tensor process(Bitmap bitmap, ImageOrientation orientation)

        Process an ARGB_8888 bitmap into a normalized float tensor.

        Parameters:
        bitmap - The source bitmap.
        orientation - The EXIF orientation of the bitmap's contents.
      • process

        @JvmOverloads() final Tensor process(Bitmap bitmap)

        Process an ARGB_8888 bitmap into a normalized float tensor.

        Parameters:
        bitmap - The source bitmap.
      • processInto

        @JvmOverloads() final Unit processInto(Bitmap bitmap, Tensor tensor, ImageOrientation orientation)

        Process an ARGB_8888 bitmap into a caller-provided tensor, reusing its storage.

        Avoids the per-call allocation of process, which matters for sustained video. tensor must be a float Tensor shaped [1, 3, targetHeight, targetWidth]; its storage is overwritten, so the caller must finish using the previous contents before calling again.

        Parameters:
        bitmap - The source bitmap.
        tensor - The output tensor to fill.
        orientation - The EXIF orientation of the bitmap's contents.
      • processInto

        @JvmOverloads() final Unit processInto(Bitmap bitmap, Tensor tensor)

        Process an ARGB_8888 bitmap into a caller-provided tensor, reusing its storage.

        Avoids the per-call allocation of process, which matters for sustained video. tensor must be a float Tensor shaped [1, 3, targetHeight, targetWidth]; its storage is overwritten, so the caller must finish using the previous contents before calling again.

        Parameters:
        bitmap - The source bitmap.
        tensor - The output tensor to fill.
      • processYuv

        @JvmOverloads() final Tensor processYuv(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, ImageOrientation orientation, YuvRange range)

        Process semi-planar YUV (NV12/NV21) camera planes into a normalized float tensor.

        For a CameraX ImageProxy in YUV_420_888, pass planes[0].buffer as yPlane and the interleaved chroma plane as uvPlane: planes[1].buffer for YuvFormat.NV12, or planes[2].buffer for YuvFormat.NV21. Both buffers must be direct.

        Only semi-planar chroma is supported. Check planes[1].pixelStride == 2 before calling; a stride of 1 means fully planar I420, which this path cannot consume.

        Both buffers are bounds-checked against the strides and dimensions given here, so a plane a camera HAL trimmed short is rejected rather than read past its end. A complete chroma plane is decoded exactly. The one allowance is the plane's final byte: a CameraX plane ends one byte short of it (planes[1] and planes[2] are views one byte apart into the same allocation), so a buffer missing exactly that byte is accepted, and the decode fills that one sample in from the neighboring pair.

        Parameters:
        yPlane - Direct buffer holding the luma plane.
        yStride - Row stride of the luma plane, in bytes.
        uvPlane - Direct buffer holding the interleaved chroma plane.
        uvStride - Row stride of the chroma plane, in bytes.
        width - Source width in pixels.
        height - Source height in pixels.
        format - Chroma order of the interleaved plane.
        orientation - The EXIF orientation of the frame's contents.
        range - Quantization range of the samples.
      • processYuv

        @JvmOverloads() final Tensor processYuv(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, ImageOrientation orientation)

        Process semi-planar YUV (NV12/NV21) camera planes into a normalized float tensor.

        For a CameraX ImageProxy in YUV_420_888, pass planes[0].buffer as yPlane and the interleaved chroma plane as uvPlane: planes[1].buffer for YuvFormat.NV12, or planes[2].buffer for YuvFormat.NV21. Both buffers must be direct.

        Only semi-planar chroma is supported. Check planes[1].pixelStride == 2 before calling; a stride of 1 means fully planar I420, which this path cannot consume.

        Both buffers are bounds-checked against the strides and dimensions given here, so a plane a camera HAL trimmed short is rejected rather than read past its end. A complete chroma plane is decoded exactly. The one allowance is the plane's final byte: a CameraX plane ends one byte short of it (planes[1] and planes[2] are views one byte apart into the same allocation), so a buffer missing exactly that byte is accepted, and the decode fills that one sample in from the neighboring pair.

        Parameters:
        yPlane - Direct buffer holding the luma plane.
        yStride - Row stride of the luma plane, in bytes.
        uvPlane - Direct buffer holding the interleaved chroma plane.
        uvStride - Row stride of the chroma plane, in bytes.
        width - Source width in pixels.
        height - Source height in pixels.
        format - Chroma order of the interleaved plane.
        orientation - The EXIF orientation of the frame's contents.
      • processYuv

        @JvmOverloads() final Tensor processYuv(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format)

        Process semi-planar YUV (NV12/NV21) camera planes into a normalized float tensor.

        For a CameraX ImageProxy in YUV_420_888, pass planes[0].buffer as yPlane and the interleaved chroma plane as uvPlane: planes[1].buffer for YuvFormat.NV12, or planes[2].buffer for YuvFormat.NV21. Both buffers must be direct.

        Only semi-planar chroma is supported. Check planes[1].pixelStride == 2 before calling; a stride of 1 means fully planar I420, which this path cannot consume.

        Both buffers are bounds-checked against the strides and dimensions given here, so a plane a camera HAL trimmed short is rejected rather than read past its end. A complete chroma plane is decoded exactly. The one allowance is the plane's final byte: a CameraX plane ends one byte short of it (planes[1] and planes[2] are views one byte apart into the same allocation), so a buffer missing exactly that byte is accepted, and the decode fills that one sample in from the neighboring pair.

        Parameters:
        yPlane - Direct buffer holding the luma plane.
        yStride - Row stride of the luma plane, in bytes.
        uvPlane - Direct buffer holding the interleaved chroma plane.
        uvStride - Row stride of the chroma plane, in bytes.
        width - Source width in pixels.
        height - Source height in pixels.
        format - Chroma order of the interleaved plane.
      • computeOutputShape

        @JvmOverloads() final LongArray computeOutputShape(Integer inputWidth, Integer inputHeight, ImageOrientation orientation)

        Shape of the tensor this processor produces for the given source.

        Parameters:
        inputWidth - Source width in pixels.
        inputHeight - Source height in pixels.
        orientation - The EXIF orientation of the source.
      • computeOutputShape

        @JvmOverloads() final LongArray computeOutputShape(Integer inputWidth, Integer inputHeight)

        Shape of the tensor this processor produces for the given source.

        Parameters:
        inputWidth - Source width in pixels.
        inputHeight - Source height in pixels.