public interface

ImageProxy

implements java.lang.AutoCloseable

 androidx.camera.core.ImageProxy

Subclasses:

SettableImageProxy, ForwardingImageProxy, RgbaImageProxy

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

An image proxy which has a similar interface as .

Summary

Methods
public voidclose()

Closes the underlying .

public RectgetCropRect()

Returns the crop rectangle.

public intgetFormat()

Returns the image format.

public intgetHeight()

Returns the image height.

public ImagegetImage()

Returns the android Image.

public ImageInfogetImageInfo()

Returns the ImageInfo.

public ImageProxy.PlaneProxygetPlanes()

Returns the array of planes.

public intgetWidth()

Returns the image width.

public voidsetCropRect(Rect rect)

Sets the crop rectangle.

public BitmaptoBitmap()

Converts ImageProxy to Bitmap.

Methods

public void close()

Closes the underlying .

See also:

public Rect getCropRect()

Returns the crop rectangle.

See also:

public void setCropRect(Rect rect)

Sets the crop rectangle.

See also:

public int getFormat()

Returns the image format.

The image format can be one of the or constants.

See also:

public int getHeight()

Returns the image height.

See also:

public int getWidth()

Returns the image width.

See also:

public ImageProxy.PlaneProxy getPlanes()

Returns the array of planes.

See also:

public ImageInfo getImageInfo()

Returns the ImageInfo.

public Image getImage()

Returns the android Image.

If the ImageProxy is a wrapper for an android Image, it will return the Image. It is possible for an ImageProxy to wrap something that isn't an Image. If that's the case then it will return null.

The returned image should not be closed by the application. Instead it should be closed by the ImageProxy, which happens, for example, on return from the ImageAnalysis.Analyzer function. Destroying the ImageAnalysis will close the underlying . So an Image obtained with this method will behave as such.

Returns:

the android image.

See also:

public Bitmap toBitmap()

Converts ImageProxy to Bitmap.

The supported ImageProxy format is , or . If format is invalid, an java.lang.IllegalArgumentException will be thrown. If the conversion to bimap failed, an java.lang.UnsupportedOperationException will be thrown.

Returns:

Bitmap instance.

Source

/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.core;

import static androidx.camera.core.internal.utils.ImageUtil.createBitmapFromImageProxy;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.media.Image;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.nio.ByteBuffer;

/** An image proxy which has a similar interface as {@link android.media.Image}. */
public interface ImageProxy extends AutoCloseable {
    /**
     * Closes the underlying {@link android.media.Image}.
     *
     * @see android.media.Image#close()
     */
    @Override
    void close();

    /**
     * Returns the crop rectangle.
     *
     * @see android.media.Image#getCropRect()
     */
    @NonNull
    Rect getCropRect();

    /**
     * Sets the crop rectangle.
     *
     * @see android.media.Image#setCropRect(Rect)
     */
    void setCropRect(@Nullable Rect rect);

    /**
     * Returns the image format.
     *
     * <p> The image format can be one of the {@link ImageFormat} or
     * {@link PixelFormat} constants.
     *
     * @see android.media.Image#getFormat()
     */
    int getFormat();

    /**
     * Returns the image height.
     *
     * @see android.media.Image#getHeight()
     */
    int getHeight();

    /**
     * Returns the image width.
     *
     * @see android.media.Image#getWidth()
     */
    int getWidth();

    /**
     * Returns the array of planes.
     *
     * @see android.media.Image#getPlanes()
     */
    @NonNull
    @SuppressLint("ArrayReturn")
    PlaneProxy[] getPlanes();

    /** A plane proxy which has an analogous interface as {@link android.media.Image.Plane}. */
    interface PlaneProxy {
        /**
         * Returns the row stride.
         *
         * @see android.media.Image.Plane#getRowStride()
         */
        int getRowStride();

        /**
         * Returns the pixel stride.
         *
         * @see android.media.Image.Plane#getPixelStride()
         */
        int getPixelStride();

        /**
         * Returns the pixels buffer.
         *
         * @see android.media.Image.Plane#getBuffer()
         */
        @NonNull
        ByteBuffer getBuffer();
    }

    /** Returns the {@link ImageInfo}. */
    @NonNull
    ImageInfo getImageInfo();

    /**
     * Returns the android {@link Image}.
     *
     * <p>If the ImageProxy is a wrapper for an android {@link Image}, it will return the
     * {@link Image}. It is possible for an ImageProxy to wrap something that isn't an
     * {@link Image}. If that's the case then it will return null.
     *
     * <p>The returned image should not be closed by the application. Instead it should be closed by
     * the ImageProxy, which happens, for example, on return from the {@link ImageAnalysis.Analyzer}
     * function.  Destroying the {@link ImageAnalysis} will close the underlying
     * {@link android.media.ImageReader}.  So an {@link Image} obtained with this method will behave
     * as such.
     *
     * @return the android image.
     * @see android.media.Image#close()
     */
    @Nullable
    @ExperimentalGetImage
    Image getImage();

    /**
     * Converts {@link ImageProxy} to {@link Bitmap}.
     *
     * <p>The supported {@link ImageProxy} format is {@link ImageFormat#YUV_420_888},
     * {@link ImageFormat#JPEG} or {@link PixelFormat#RGBA_8888}. If format is invalid, an
     * {@link IllegalArgumentException} will be thrown. If the conversion to bimap failed, an
     * {@link UnsupportedOperationException} will be thrown.
     *
     * @return {@link Bitmap} instance.
     */
    @NonNull
    default Bitmap toBitmap() {
        return createBitmapFromImageProxy(this);
    }
}