-
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classImageProcessor.Companion
-
Field Summary
Fields Modifier and Type Field Description private final BooleanisValidprivate final ImageProcessorConfigconfig
-
Constructor Summary
Constructors Constructor Description ImageProcessor(ImageProcessorConfig config)
-
Method Summary
Modifier and Type Method Description final BooleangetIsValid()final ImageProcessorConfiggetConfig()Unitclose()Releases native resources. final Tensorprocess(Bitmap bitmap, ImageOrientation orientation)Process an ARGB_8888 bitmap into a normalized float tensor. final Tensorprocess(Bitmap bitmap)Process an ARGB_8888 bitmap into a normalized float tensor. final UnitprocessInto(Bitmap bitmap, Tensor tensor, ImageOrientation orientation)Process an ARGB_8888 bitmap into a caller-provided tensor, reusing its storage. final UnitprocessInto(Bitmap bitmap, Tensor tensor)Process an ARGB_8888 bitmap into a caller-provided tensor, reusing its storage. final TensorprocessYuv(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. final TensorprocessYuv(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. final TensorprocessYuv(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. final UnitprocessYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor, ImageOrientation orientation, YuvRange range)Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage. final UnitprocessYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor, ImageOrientation orientation)Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage. final UnitprocessYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor)Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage. final LongArraycomputeOutputShape(Integer inputWidth, Integer inputHeight, ImageOrientation orientation)Shape of the tensor this processor produces for the given source. final LongArraycomputeOutputShape(Integer inputWidth, Integer inputHeight)Shape of the tensor this processor produces for the given source. final LetterboxPaddingcomputeLetterboxPadding(Integer inputWidth, Integer inputHeight, ImageOrientation orientation)Left and top offsets (in pixels) of the resized content within the letterboxed output, letting callers map model output back to source coordinates without replicating the resize geometry. final LetterboxPaddingcomputeLetterboxPadding(Integer inputWidth, Integer inputHeight)Left and top offsets (in pixels) of the resized content within the letterboxed output, letting callers map model output back to source coordinates without replicating the resize geometry. -
-
Constructor Detail
-
ImageProcessor
ImageProcessor(ImageProcessorConfig config)
- Parameters:
config- The target size, resize mode, and normalization to apply.
-
-
Method Detail
-
getIsValid
final Boolean getIsValid()
-
getConfig
final ImageProcessorConfig getConfig()
-
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.
tensormust 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.
tensormust 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
ImageProxyinYUV_420_888, passplanes[0].bufferas yPlane and the interleaved chroma plane as uvPlane:planes[1].bufferfor YuvFormat.NV12, orplanes[2].bufferfor YuvFormat.NV21. Both buffers must be direct.Only semi-planar chroma is supported. Check
planes[1].pixelStride == 2before 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]andplanes[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
ImageProxyinYUV_420_888, passplanes[0].bufferas yPlane and the interleaved chroma plane as uvPlane:planes[1].bufferfor YuvFormat.NV12, orplanes[2].bufferfor YuvFormat.NV21. Both buffers must be direct.Only semi-planar chroma is supported. Check
planes[1].pixelStride == 2before 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]andplanes[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
ImageProxyinYUV_420_888, passplanes[0].bufferas yPlane and the interleaved chroma plane as uvPlane:planes[1].bufferfor YuvFormat.NV12, orplanes[2].bufferfor YuvFormat.NV21. Both buffers must be direct.Only semi-planar chroma is supported. Check
planes[1].pixelStride == 2before 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]andplanes[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.
-
processYuvInto
@JvmOverloads() final Unit processYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor, ImageOrientation orientation, YuvRange range)
Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage.
See processYuv for the plane contract and processInto for the reuse contract.
-
processYuvInto
@JvmOverloads() final Unit processYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor, ImageOrientation orientation)
Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage.
See processYuv for the plane contract and processInto for the reuse contract.
-
processYuvInto
@JvmOverloads() final Unit processYuvInto(ByteBuffer yPlane, Integer yStride, ByteBuffer uvPlane, Integer uvStride, Integer width, Integer height, YuvFormat format, Tensor tensor)
Process semi-planar YUV camera planes into a caller-provided tensor, reusing its storage.
See processYuv for the plane contract and processInto for the reuse contract.
-
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.
-
computeLetterboxPadding
@JvmOverloads() final LetterboxPadding computeLetterboxPadding(Integer inputWidth, Integer inputHeight, ImageOrientation orientation)
Left and top offsets (in pixels) of the resized content within the letterboxed output, letting callers map model output back to source coordinates without replicating the resize geometry. Returns
(0, 0)for ResizeMode.STRETCH or LetterboxAnchor.TOP_LEFT.- Parameters:
inputWidth- Source width in pixels.inputHeight- Source height in pixels.orientation- The EXIF orientation of the source.
-
computeLetterboxPadding
@JvmOverloads() final LetterboxPadding computeLetterboxPadding(Integer inputWidth, Integer inputHeight)
Left and top offsets (in pixels) of the resized content within the letterboxed output, letting callers map model output back to source coordinates without replicating the resize geometry. Returns
(0, 0)for ResizeMode.STRETCH or LetterboxAnchor.TOP_LEFT.- Parameters:
inputWidth- Source width in pixels.inputHeight- Source height in pixels.
-
-
-
-