Preface detail image format specification interface specifications ImageFormat enumeration class enumeration class FilterModeImageInfoYuvUtils class 2.1 Bitmap converted to other image formats 2.2 various formats of mutual transformation between image 2.3 various formats of image is transformed into Bitmap2.4 Image rotation and cropping operations of various formats 2.5 Images of various formats are rotated and cropped into relevant Bitmap2.6 Mirror flip operations of images of various formats 2.7 Zoom operations of images of various formats ImageUtilsNV21 image operations of I420 image operations


preface

In daily development, when it comes to Android Camera or image-related development, there are more or less contact with some image formats, and various operations will be involved between these different formats. Here, an image manipulation library is encapsulated by Google’s open source framework Libyuv. It involves the image conversion operation commonly used in Android.


Functional specifications

    √ Support RGB_565, ARGB_8888, RGB24, I420, NV21 5 format image conversion operation

    √ Support to convert Bitmap into the above five image formats

    √ Convert images in the above format to Bitmap

    √ Support rotation cropping operation between images in the above formats

    √ Support zoom operation between images in the above formats

    √ Support image mirroring operation between the above formats

    √ The bottom layer relies on the libyuv framework, which is safe, stable and efficient


Image format Description

For instructions on image formats, see here: Common Image formats in Android

Interface specification

Common image format conversion library address:ImageUtils

ImageFormat enumeration class

This class is used to illustrate the format of data conversion currently supported, as follows:

enum class ImageFormat(var format: Int) {

    NV21(1),

    I420(2),

    RGB_565(3),

    RGB_888(4),

    ARGB_8888(5)

}

Copy the code

For details on the above data formats, please refer to common Image formats in Android


Enumeration class FilterMode

This enumeration class describes the filter modes supported when scaling an image, as follows:

enum class FilterMode(var filter: Int) {

    FilterNone(0),

    FilterLinear(1),

    FilterBilinear(2),

    FilterBox(3)

}

Copy the code

For a description of filtering modes, see filter.md


ImageInfo

This class is mainly used to assist image data rotation clipping, as follows:

class ImageInfo(

    val data: ByteArray,

    val dataFormat: ImageFormat,

    val width: Int.

    val height: Int.

    @RotateDegree val degree: Int = 0.

    val rect: Rect? = null.

    val targetFormat: ImageFormat,

    val priorityClip: Boolean = true

)

Copy the code

The parameters are described as follows:

  • data: Source image data
  • dataFormat: Source image data format. See the supported formatsImageFormat
  • width: Width of source image
  • height: Height of the source image
  • degree: The Angle to be rotated
  • rect: Rectangle coordinates to be clippedRectfornullIs not clipped
  • targetFormat: Image format after processing. See supported formatsImageFormat
  • priorityClip: Uses priority tailoring. The default value istureThis parameter is very important. We’ll talk about it later

YuvUtils class

This class is the core class of the library

2.1 Bitmap is converted to other image formats

There are five main interfaces:

external fun bitmapToNV21(bitmap: Bitmap?).: ByteArray?

external fun bitmapToRgb565(bitmap: Bitmap?).: ByteArray?

external fun bitmapToRgb24(bitmap: Bitmap?).: ByteArray?

external fun bitmapToRgba(bitmap: Bitmap?).: ByteArray?

external fun bitmapToI420(bitmap: Bitmap?).: ByteArray?

Copy the code
  • About the parametersbitmap, currently only supportedRGB_565RGBA_8888Two types of bitmap conversion

2.2 Conversion between images of various formats

external fun imageFormatConvert(

    dataByteArray.

    width: Int.

    height: Int.

    @SupportFormat dataFormat: Int.

    @SupportFormat targetFormat: Int

)
: ByteArray?

Copy the code

Parameter Description:

  • Data: source image data

  • Width: the width of the image

  • Height: The height of the image

  • DataFormat: source ImageFormat, supported image formats, see the ImageFormat enumeration class

  • TargetFormat: ImageFormat to be converted. For supported image formats, see the ImageFormat enumeration class

  • Return value: Returns the converted data

    For example, a 640 * 480 resolution NV21 format data, now need to convert it to RGB24 format, we can call:

YuvUtils.imageFormatConvert(data.640.480.1.4)

Copy the code

2.3 Images of various formats are converted into Bitmap

external fun imageToBitmap(

    dataByteArray.

    width: Int.

    height: Int.

    @SupportFormat dataFormat: Int.

    bitmapConfig: Int

)
:Bitmap?

Copy the code

Parameter Description:

  • data: Source image data
  • width: Width of the image
  • height: Height of the image
  • dataFormatFor details about the supported image formats, see source image formatImageFormatEnumeration class
  • bitmapConfig: Bitmap format to be converted. Currently, only Bitmap format is supportedRGB565ARGB_8888Two formatsBitmap
  • The return value: Returns the convertedBitmap

2.4 Rotation and cropping operations for images of various formats

external fun dataClipRotate(

    dataByteArray.

    @SupportFormat dataFormat: Int.

    width: Int.

    height: Int.

    @RotateDegree degree: Int.

    rect: Rect? .

    targetFormat: Int.

    priorityClip: Boolean

)
: ByteArray?

Copy the code

Parameter Description:

  • data: Source image data
  • dataFormatFor details about the supported image formats, see source image formatImageFormatEnumeration class
  • width: Width of the image
  • height: Height of the image
  • degree: Image rotation Angle, supported only0,90,180,270Four angles
  • rect: Clipped rectangle coordinates, which can benullIs displayed, indicating that no clipping operation is performed
  • targetFormat: Target image format to be converted. For details about supported image formats, seeImageFormatEnumeration class
  • priorityClip: Whether to perform the cutting operation preferentially, cutting and then rotating and rotating and then trimming are two different operations
  • The return value: Returns the converted data

Such as:

YuvUtils.dataClipRotate(

            rgba_data, 5, width, height, 90.

            Rect(100.0.300.300), 5.false

        )

Copy the code
image

As you can see from the above operation, the selection of priorityClip is very important!

2.5 Images of various formats are transformed into relevant bitmaps through rotation and cropping

external fun dataClipRotateToBitmap(

    dataByteArray.

    @SupportFormat dataFormat: Int.

    width: Int.

    height: Int.

    @RotateDegree degree: Int.

    rect: Rect? .

    bitmapConfig: Int.

    priorityClip: Boolean

)
: Bitmap?

Copy the code

Parameter Description:

  • data: Source image data
  • dataFormatFor details about the supported image formats, see source image formatImageFormatEnumeration class
  • width: Width of the image
  • height: Height of the image
  • degree: Image rotation Angle, supported only0,90,180,270Four angles
  • rect: Clipped rectangle coordinates, which can benullIs displayed, indicating that no clipping operation is performed
  • bitmapConfig: Needs to be converted toBitmapFormat, currently supported onlyRGB565ARGB_8888Two formatsBitmap
  • priorityClip: Whether to perform the cutting operation preferentially, cutting and then rotating and rotating and then trimming are two different operations
  • The return value: Returns the convertedBitmap

2.6 Mirror flipping operation of images of various formats

external fun dataMirror(

    dataByteArray.

    width: Int.

    height: Int.

    @SupportFormat dataFormat: Int.

    @SupportFormat targetFormat: Int.

    isVerticalMirror: Boolean = false

)
: ByteArray?

Copy the code

Parameter Description:

  • data: Source image data
  • width: Width of the image
  • height: Height of the image
  • dataFormatFor details about the supported image formats, see source image formatImageFormatEnumeration class
  • targetFormat: Image format to be converted. For details about supported image formats, seeImageFormatEnumeration class
  • isVerticalMirror: Indicates whether to perform vertical mirroring. Default valuefalse, that is, flip horizontally, if istrueThat is, the image is carried out180Rotation, not true vertical mirror image
  • The return value: Returns the converted data

For isVerticalMirror differences, see the following figure:

image

2.7 Zoom operation of images in various formats

external fun dataScale(

    dataByteArray.

    width: Int.

    height: Int.

    dstWidth: Int.

    dstHeight: Int.

    @SupportFormat dataFormat: Int.

    @SupportFormat targetFormat: Int.

    @SupportFilter filterMode: Int = 0

)
: ByteArray?

Copy the code

Parameter Description:

  • data: Source image data
  • width: Width of the image
  • height: Height of the image
  • dstWidth: Width of the target image
  • dstHeight: Height of the target image
  • dataFormatFor details about the supported image formats, see source image formatImageFormatEnumeration class
  • targetFormat: Image format to be converted. For details about supported image formats, seeImageFormatEnumeration class

ImageUtils

The ImageUtils class is a secondary wrapper of the YuvUtils class, operating in each image format for easier external calls.

NV21 format image operation

The operation methods of NV21 image are as follows:

methods Method statement
nv21ToI420 NV21Format image data intoI420Formatted data
nv21ToRgb565 NV21Format image data intoRGB_565Formatted data
nv21ToRgb24 NV21Format image data intoRGB24Formatted data
nv21ToRgba NV21Format image data intoARGB_8888Formatted data
nv21ToBitmap8888 NV21Format image data intoARGB_8888The format ofBitmap
nv21ToBitmap565 NV21Format image data intoRGB_565The format ofBitmap
nv21Rotate NV21Format image data rotation operation
nv21Clip NV21Format image data cropping operation
nv21Mirror NV21Format image data horizontal mirror operation
nv21Scale NV21Format image data zoom operation
bitmapToNV21 willBitmapConverted toNV21Format data

I420 format image operation

The operation methods of I420 format images are as follows:

methods Method statement
i420ToNV21 I420Format image data intoNV21Formatted data
i420ToRgb565 I420Format image data intoRGB_565Formatted data
i420ToRgb24 I420Format image data intoRGB24Formatted data
i420ToRgba I420Format image data intoARGB_8888Formatted data
i420ToBitmap8888 I420Format image data intoARGB_8888The format ofBitmap
i420ToBitmap565 I420Format image data intoRGB_565The format ofBitmap
i420Rotate I420Format image data rotation operation
i420Clip I420Format image data cropping operation
i420Mirror I420Format image data horizontal mirror operation
i420Scale I420Format image data zoom operation
bitmapToI420 willBitmapConverted toI420Format data

The RGB24, RGB_565, and ARGB_8888 formats are similar to the interface methods shown above. In addition, some existing interfaces are the packaging of YuvUtils interface, and the basic operations are similar, not explained.