- Perform initialization by using the VisionBase static class, to obtain a connection to the service.
VisionBase.init(MainActivity.this, new ConnectionCallback(){
@Override
public void onServiceConnect(){
Log.i(LOG_TAG, "onServiceConnect");
}
@Override
public void onServiceDisconnect(){
Log.i(LOG_TAG, "onServiceDisconnect");
}
});
- Define the mImageSegmentation instance, and use the context of this app as the input parameter.
ImageSegmentation mImageSegmentation = new ImageSegmentation(mContext);
- Configure the parameters.
SegConfiguration mSegmentationConfiguration = new SegConfiguration.Builder()
.setProcessMode(VisionConfiguration.MODE_IN)
.setSegmentationType(SegConfiguration.TYPE_PORTRAIT_SEGMENTATION_VIDEO)
.setOutputType(SegConfiguration.OUTPUT_TYPE_BYTEARRAY)
.build();
mImageSegmentation.setConfiguration(mSegmentationConfiguration);
- Define VisionImageMetadata. Images cannot be input for video portrait segmentation. The default rotation angle is 270°.
VisionImageMetadata.Builder builder = new VisionImageMetadata.Builder();
builder.setFormat(17);/NV21 format
builder.setHeight(mHeight);
builder.setWidth(mWidth);
builder.setRotation(mRotation);
VisionImageMetadata metadata = builder.build();
- Set the byte array to be portrait-segmented.
VisionImage image = VisionImage.fromByteArray(mybytes, metadata);
- Download the plug-in in lazyload mode. Call getAvailability() to check whether the plug-in is required. If yes, call loadPlugin() to download the plug-in.
int availability = imageSegmentation.getAvailability();
if (availability == HwHiAIResultCode.AIRESULT_PLUGIN_PENDING_UPDATE) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
LoadPluginCallback cb = new LoadPluginCallback(lock, condition);
imageSegmentation.loadPlugin(cb);
lock.lock();
try {
condition.await(90, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Log.e(TAG, e.getMessage());
} finally {
lock.unlock();
}
}
- Call doSegmentation to perform segmentation.
ImageResult srt = new ImageResult();
int rltCode = mImageSegmentation.doSegmentation(image, srt, null);
- Call getBytes to obtain the segmentation result.
byte[] values = srt.getByteArray();
- Generate a Bitmap for saving the segmentation result.
private static final int A_CHANNEL_PIXEL_MASK = 0xFF;
private static final int A_CHANNEL_RIGHT_SHIFT_INDEX = 24;
private static final int HUMAN_VALUE_THRESHOLD = 50;
private static final int HUMAN_PIXEL_VALUE = 0xFF0000FF;
private static final int BACKGROUND_PIXEL_VALUE = 0xFF000000;
int pixels[] = new int[mWidth * mHeight];
int out_pixels[] = new int[mWidth * mHeight];
for (int i = 0; i < pixels.length; ++i) {
pixels[i] = values[i];
}
for (int pixelIdx = 0; pixelIdx < mWidth * mHeight; pixelIdx++) {
int pixelValue = pixels[pixelIdx];
if (((pixelValue >> A_CHANNEL_RIGHT_SHIFT_INDEX) & A_CHANNEL_PIXEL_MASK) < HUMAN_VALUE_THRESHOLD ) {
out_pixels[pixelIdx] = BACKGROUND_PIXEL_VALUE;
} else {
out_pixels[pixelIdx] = HUMAN_PIXEL_VALUE;
}
}
Bitmap segmentedBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
setgmentedBitmap.setPixels(out_pixels, 0, mWidth, 0, 0, mWidth, mHeight);
saveMyBitmap(filename, segmentedBitmap);