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 datadataFormat
: Source image data format. See the supported formatsImageFormatwidth
: Width of source imageheight
: Height of the source imagedegree
: The Angle to be rotatedrect
: Rectangle coordinates to be clippedRect
fornull
Is not clippedtargetFormat
: Image format after processing. See supported formatsImageFormatpriorityClip
: Uses priority tailoring. The default value isture
This 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 parameters
bitmap
, currently only supportedRGB_565
和RGBA_8888
Two types of bitmap conversion
2.2 Conversion between images of various formats
external fun imageFormatConvert(
data: ByteArray.
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(
data: ByteArray.
width: Int.
height: Int.
@SupportFormat dataFormat: Int.
bitmapConfig: Int
):Bitmap?
Copy the code
Parameter Description:
data
: Source image datawidth
: Width of the imageheight
: Height of the imagedataFormat
For details about the supported image formats, see source image formatImageFormat
Enumeration classbitmapConfig
: Bitmap format to be converted. Currently, only Bitmap format is supportedRGB565
和ARGB_8888
Two formatsBitmap
The return value
: Returns the convertedBitmap
2.4 Rotation and cropping operations for images of various formats
external fun dataClipRotate(
data: ByteArray.
@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 datadataFormat
For details about the supported image formats, see source image formatImageFormat
Enumeration classwidth
: Width of the imageheight
: Height of the imagedegree
: Image rotation Angle, supported only0
,90
,180
,270
Four anglesrect
: Clipped rectangle coordinates, which can benull
Is displayed, indicating that no clipping operation is performedtargetFormat
: Target image format to be converted. For details about supported image formats, seeImageFormat
Enumeration classpriorityClip
: Whether to perform the cutting operation preferentially, cutting and then rotating and rotating and then trimming are two different operationsThe 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
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(
data: ByteArray.
@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 datadataFormat
For details about the supported image formats, see source image formatImageFormat
Enumeration classwidth
: Width of the imageheight
: Height of the imagedegree
: Image rotation Angle, supported only0
,90
,180
,270
Four anglesrect
: Clipped rectangle coordinates, which can benull
Is displayed, indicating that no clipping operation is performedbitmapConfig
: Needs to be converted toBitmap
Format, currently supported onlyRGB565
和ARGB_8888
Two formatsBitmap
priorityClip
: Whether to perform the cutting operation preferentially, cutting and then rotating and rotating and then trimming are two different operationsThe return value
: Returns the convertedBitmap
2.6 Mirror flipping operation of images of various formats
external fun dataMirror(
data: ByteArray.
width: Int.
height: Int.
@SupportFormat dataFormat: Int.
@SupportFormat targetFormat: Int.
isVerticalMirror: Boolean = false
): ByteArray?
Copy the code
Parameter Description:
data
: Source image datawidth
: Width of the imageheight
: Height of the imagedataFormat
For details about the supported image formats, see source image formatImageFormat
Enumeration classtargetFormat
: Image format to be converted. For details about supported image formats, seeImageFormat
Enumeration classisVerticalMirror
: Indicates whether to perform vertical mirroring. Default valuefalse
, that is, flip horizontally, if istrue
That is, the image is carried out180
Rotation, not true vertical mirror imageThe return value
: Returns the converted data
For isVerticalMirror differences, see the following figure:
2.7 Zoom operation of images in various formats
external fun dataScale(
data: ByteArray.
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 datawidth
: Width of the imageheight
: Height of the imagedstWidth
: Width of the target imagedstHeight
: Height of the target imagedataFormat
For details about the supported image formats, see source image formatImageFormat
Enumeration classtargetFormat
: Image format to be converted. For details about supported image formats, seeImageFormat
Enumeration 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 |
NV21 Format image data intoI420 Formatted data |
nv21ToRgb565 |
NV21 Format image data intoRGB_565 Formatted data |
nv21ToRgb24 |
NV21 Format image data intoRGB24 Formatted data |
nv21ToRgba |
NV21 Format image data intoARGB_8888 Formatted data |
nv21ToBitmap8888 |
NV21 Format image data intoARGB_8888 The format ofBitmap |
nv21ToBitmap565 |
NV21 Format image data intoRGB_565 The format ofBitmap |
nv21Rotate |
NV21 Format image data rotation operation |
nv21Clip |
NV21 Format image data cropping operation |
nv21Mirror |
NV21 Format image data horizontal mirror operation |
nv21Scale |
NV21 Format image data zoom operation |
bitmapToNV21 |
willBitmap Converted toNV21 Format data |
I420 format image operation
The operation methods of I420 format images are as follows:
methods | Method statement |
---|---|
i420ToNV21 |
I420 Format image data intoNV21 Formatted data |
i420ToRgb565 |
I420 Format image data intoRGB_565 Formatted data |
i420ToRgb24 |
I420 Format image data intoRGB24 Formatted data |
i420ToRgba |
I420 Format image data intoARGB_8888 Formatted data |
i420ToBitmap8888 |
I420 Format image data intoARGB_8888 The format ofBitmap |
i420ToBitmap565 |
I420 Format image data intoRGB_565 The format ofBitmap |
i420Rotate |
I420 Format image data rotation operation |
i420Clip |
I420 Format image data cropping operation |
i420Mirror |
I420 Format image data horizontal mirror operation |
i420Scale |
I420 Format image data zoom operation |
bitmapToI420 |
willBitmap Converted toI420 Format 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.