Image processing technology: image cutting, labels, stickers, decals, ultra detailed development steps
Huawei mobile services 2021-06-19 18:04:19

Last one : The filter of image processing 、 The development of graphic typesetting In detail , How to add the filter and text typesetting ability in the image editing ability to the application is explained in detail , Today, we continue to bring the other three capabilities of Huawei's five image editing capabilities : Image cutting 、 Theme Tags , And decals , I believe that students who study hard can successfully create their own image editing applications .

Image clipping

Edit adjustment service , The implementation of all interfaces of image clipping service is based on CropLayoutView, It's a custom View,import The package name of is com.huawei.hms.image.vision.crop.CropLayoutView, It can be directly integrated into layout Of XML in .

1、 Get CropLayoutView object .

adopt findViewById Get CropLayoutView After the object , You can call specific logical methods .

CropLayoutView cropLayoutView = findViewById(R.id.cropImageView);

2、 Set the image you want to crop , After setting up , You can do that. view To operate .

cropLayoutView.setImageBitmap(inputBm);
  • If you need to rotate the picture 90 degree , Call the image rotation interface
cropLayoutView.rotateClockwise();
  • If you need to mirror the image horizontally , Call the image horizontal mirror interface
cropLayoutView.flipImageHorizontally();
  •   If you need to mirror the image vertically , Call the image vertical mirror interface
cropLayoutView.flipImageVertically();
  •   If you need to crop a fixed scale image , Call the following interface
cropLayoutView.setAspectRatio(ratioX, ratioY);
  •   If you need to crop an image of any scale , Call the following interface
cropLayoutView.setFixedAspectRatio(false);
  •   If you need to crop a rectangular or elliptical image , Call the following interface
// rectangular 
cropLayoutView.setCropShape(CropLayoutView.CropShape.RECTANGLE);
// ellipse 
cropLayoutView.setCropShape(CropLayoutView.CropShape.OVAL);

 3、 Adjust the size of the image to be cropped , Get the... Of the cropped image bitmap Format .

Bitmap croppedImage = cropLayoutView.getCroppedImage();

Image theme tag service

Development steps

Image theme tag service development steps are as follows , I need you to provide pictures and related parameters , Get the corresponding return value .

1、 Import image theme tag service pack .

import com.huawei.hms.image.vision.*;

2、 Get image theme tag service instance .

// obtain ImageVisionImpl object 
ImageVisionImpl imageVisionAPI = ImageVision.getInstance(this);

3、 Service initialization , Consistent with the filter service , See also Development steps Related description in .

4、 Building parameter objects .

parameter list

type

M/O( Mandatory / Optional )

explain

requestJson

JSONObject

M

Image processing request parameters .

imageBitmap

Bitmap

M

Images to be identified .

requestJson Field information :

parameter list

type

M/O( Mandatory / Optional )

explain

requestId

String

O

The business offers the request ID.

taskJson

JSONObject

M

Specific business request information .

authJson

JSONObject

M

Authentication parameters .

Image theme tag of taskJson The field information is as follows :

parameter list

type

M/O( Mandatory / Optional )

explain

language

String

M

Specify the label language :

    • cn: For Chinese
    • en: Representative English

needObjectList

boolean

O

Specify whether you want to return the list of subjects in the picture first .

explain

When using the image theme tag service , You need to guarantee that token It works , Otherwise, the service cannot be used .token See also token Access method .

Image theme tag service requestJson Example :

{
"requestId":"",
"taskJson":{"language":"cn"},
"authJson":{"projectId":"projectIdTest","appId":"appIdTest","authApiKey":"authApiKeyTest","clientSecret":"CSecretTest","clientId":"CIdTest","token":"tokenTest"}
}

5、 Image theme tag service gets results .

You are calling analyzeImageThemeTag Interface , You need to input the image to be processed Bitmap etc. ( See building parameter objects ). This feature requires networking , If not connected , The error code is returned .

// Get the return value of the image recognition result 
new Thread(new Runnable() {
@Override
public void run() {
ImageVisionResult result = imageVisionAPI.analyzeImageThemeTag(requestJson, imageBitmap);
}
}).start();

result Return value :

parameter list

type

M/O( Mandatory / Optional )

explain

resultCode

int

M

Service status code .

response

JSONObject

O

The result returned by the service .

response Return value :

parameter list

type

M/O( Mandatory / Optional )

explain

resultCode

int

M

Return result code .

serviceId

String

O

Back to service ID.

requestId

String

O

Return request ID.

tags

JSONArray

O

Return tag results , See the table below .

objectList

JSONArray

O

If the incoming needObjectList by true, Then the main body detection result is returned .

JSONArray tags:

parameter list

type

M/O( Mandatory / Optional )

explain

tagName

String

O

Tag name .

tagConfidence

float

O

Degree of confidence ( Value 0-1.0, The larger the value , The more likely the conjecture is to be correct ).

JSONArray objectList:

parameter list

type

M/O( Mandatory / Optional )

explain

type

int

O

The type to which the subject belongs , Value {0,1 ... 5,6}.

    • 0 representative OTHER
    • 1 representative GOODS
    • 2 representative FOOD
    • 3 representative FURNITURE
    • 4 representative PLANT
    • 5 representative PLACE
    • 6 representative FACE

possibility

Float

O

Degree of confidence ( Value 0-1.0, The larger the value , The more likely the conjecture is to be correct ).

box

JSONObject

O

Subject location information .

 JSONObject box:

parameter list

type

M/O( Mandatory / Optional )

explain

width

double

O

The width of the main area .

height

double

O

The height of the main area .

center_x

double

O

The horizontal axis coordinates of the center position of the main body .

center_y

double

O

The vertical axis coordinates of the center position of the main body .

explain : Because the interface involves network requests , You need to open the child thread to call the interface .

6、 Out of Service .

When the theme tag effect is no longer needed , Call the interface to stop the service ,stopCode by 0 when , Successful implementation .

Decals

Development steps

The implementation of all interfaces of Decal word service is based on StickerLayout, It's a custom View,import The package name of is com.huawei.hms.image.vision.sticker.StickerLayout, It can be directly integrated into the layout Of XML in .

1、 Get StickerLayout object .

adopt findViewById Get StickerLayout After the object , You can call specific logical methods .

StickerLayout mStickerLayout = findViewById(R.id.sticker_container);

2、 Provide resource files .

See stickers and signature Design and generate stickers 、 Flower resources .

3、 Add stickers and words .

call addSticker Interface , Add stickers and words , Pass in the resource file path and file name , If the return code is 0, Then it is added successfully , The maximum number of decals can be added 50 individual .

Request interface parameter description :

parameter list

type

M/O( Mandatory / Optional )

explain

rootPath

String

M

Resource file root directory .

fileName

String

M

If it's a sticker , The specified file name is required , Others can be passed on null.

  Code example :

addSticker(rootPath + " stickers 1", "sticker_10_editable.png");
addSticker(rootPath + " signature 4", null);

5、 Update the text of huazi .

You can edit the flowery text , Through the callback method setStickerLayoutListener(new StickerLayout.StickerLayoutListener()) Get the flowered text editing object textEditInfo,textEditInfo call setText, Edit the text , Last mStickerLayout call updateStickerText Method to update .

// obtain TextEditInfo object 
mStickerLayout.setStickerLayoutListener(new StickerLayout.StickerLayoutListener() {
@Override
public void onStickerLayoutClick() {
}
@Override
public void onStickerTouch(int index) {
}
@Override
public void onTextEdit(TextEditInfo textEditInfo) {
StickerActivity.this.textEditInfo = textEditInfo;
}
@Override
public void needDisallowInterceptTouchEvent(boolean isNeed) {
}
});
// Update the pattern text to " Pattern test " word 
textEditInfo.setText(" Pattern test ");
mStickerLayout.updateStickerText(textEditInfo);

>> Visit the official website of Huawei image service , Learn more about
>> Get Huawei image service development guidance document
>> Huawei HMS Core Official forum
>> Huawei image service open source warehouse address :GitHubGitee

Click the attention on the right side of the avatar in the upper right corner , Get to know the latest technology of Huawei mobile service for the first time ~

Please bring the original link to reprint ,thank
Similar articles