Skip to content

Commit

Permalink
Add draw mask for BitMapWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
xyang16 committed Oct 9, 2022
1 parent 22add9b commit 3b13604
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.android.tools.build:gradle:7.2.2'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public Image fromNDArray(NDArray array) {
return new BitMapWrapper(bitmap);
}

@Override
public Image fromPixels(int[] pixels, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
return new BitMapWrapper(bitmap);
}

static class BitMapWrapper implements Image {
private Bitmap bitmap;

Expand All @@ -141,10 +147,39 @@ public int getHeight() {

/** {@inheritDoc} */
@Override
public Object getWrappedImage() {
public Bitmap getWrappedImage() {
return bitmap;
}

/** {@inheritDoc} */
@Override
public BitMapWrapper resize(int width, int height, boolean copy) {
if (!copy && bitmap.getWidth() == width && bitmap.getHeight() == height) {
return this;
}
return new BitMapWrapper(Bitmap.createScaledBitmap(bitmap, width, height, true));
}

/** {@inheritDoc} */
@Override
public Image getMask(int[][] mask) {
int w = mask[0].length;
int h = mask.length;
BitMapWrapper resized = resize(w, h, true);
Bitmap img = resized.getWrappedImage();
int[] pixels = new int[w * h];
int index = 0;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
if (mask[y][x] != 0) {
pixels[index] = img.getPixel(x, y);
}
index++;
}
}
return new BitMapWrapper(Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888));
}

/** {@inheritDoc} */
@Override
public Image getSubImage(int x, int y, int w, int h) {
Expand Down Expand Up @@ -271,6 +306,22 @@ public void drawJoints(Joints joints) {
oldBitmap.recycle();
}

/** {@inheritDoc} */
@Override
public void drawImage(Image overlay, boolean resize) {
if (!(overlay.getWrappedImage() instanceof Bitmap)) {
throw new IllegalArgumentException("Only Bitmap allowed");
}
if (resize) {
overlay = overlay.resize(getWidth(), getHeight(), false);
}
Bitmap target = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(target);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawBitmap((Bitmap) overlay.getWrappedImage(), 0, 0, null);
bitmap = target;
}

private int randomColor() {
return Color.rgb(
RandomUtils.nextInt(255), RandomUtils.nextInt(255), RandomUtils.nextInt(255));
Expand Down
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
djl_version=0.19.0
djl_version=0.20.0
pytorch_version=1.12.1

0 comments on commit 3b13604

Please sign in to comment.