-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHANGELOG: Introduce Depth Wallpaper
- Loading branch information
Showing
15 changed files
with
708 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
app/src/main/java/it/dhd/oxygencustomizer/utils/BitmapSubjectSegmenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package it.dhd.oxygencustomizer.utils; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Color; | ||
|
||
import com.google.android.gms.common.moduleinstall.ModuleAvailabilityResponse; | ||
import com.google.android.gms.common.moduleinstall.ModuleInstall; | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallClient; | ||
import com.google.android.gms.common.moduleinstall.ModuleInstallRequest; | ||
import com.google.android.gms.tasks.OnSuccessListener; | ||
import com.google.mlkit.vision.common.InputImage; | ||
import com.google.mlkit.vision.segmentation.subject.SubjectSegmentation; | ||
import com.google.mlkit.vision.segmentation.subject.SubjectSegmenter; | ||
import com.google.mlkit.vision.segmentation.subject.SubjectSegmenterOptions; | ||
|
||
import java.nio.FloatBuffer; | ||
|
||
public class BitmapSubjectSegmenter { | ||
final SubjectSegmenter mSegmenter; | ||
final Context mContext; | ||
public BitmapSubjectSegmenter(Context context) | ||
{ | ||
mContext = context; | ||
mSegmenter = SubjectSegmentation.getClient( | ||
new SubjectSegmenterOptions.Builder() | ||
.enableForegroundConfidenceMask() | ||
.build()); | ||
|
||
downloadModelIfNeeded(); | ||
} | ||
|
||
public void downloadModelIfNeeded() | ||
{ | ||
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(mContext); | ||
|
||
moduleInstallClient | ||
.areModulesAvailable(mSegmenter) | ||
.addOnSuccessListener( | ||
response -> { | ||
if (!response.areModulesAvailable()) { | ||
moduleInstallClient | ||
.installModules( | ||
ModuleInstallRequest.newBuilder() | ||
.addApi(mSegmenter) | ||
.build()); | ||
} | ||
}); | ||
} | ||
|
||
public void checkModelAvailability(OnSuccessListener<ModuleAvailabilityResponse> resultListener) | ||
{ | ||
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(mContext); | ||
|
||
moduleInstallClient.areModulesAvailable(mSegmenter).addOnSuccessListener(resultListener); | ||
} | ||
|
||
public void segmentSubject(Bitmap inputBitmap, SegmentResultListener listener) | ||
{ | ||
int transparentColor = Color.alpha(Color.TRANSPARENT); | ||
|
||
Bitmap resultBitmap = inputBitmap.copy(Bitmap.Config.ARGB_8888, true); | ||
|
||
mSegmenter.process(InputImage.fromBitmap(inputBitmap, 0)) | ||
.addOnSuccessListener(subjectSegmentationResult -> { | ||
FloatBuffer mSubjectMask = subjectSegmentationResult.getForegroundConfidenceMask(); | ||
|
||
resultBitmap.setHasAlpha(true); | ||
for(int y = 0; y < inputBitmap.getHeight(); y++) | ||
{ | ||
for(int x = 0; x < inputBitmap.getWidth(); x++) | ||
{ | ||
//noinspection DataFlowIssue | ||
if(mSubjectMask.get() < .5f) | ||
{ | ||
resultBitmap.setPixel(x, y, transparentColor); | ||
} | ||
} | ||
} | ||
|
||
inputBitmap.recycle(); | ||
listener.onSuccess(resultBitmap); | ||
}) | ||
.addOnFailureListener(e -> { | ||
inputBitmap.recycle(); | ||
listener.onFail(); | ||
}); | ||
|
||
} | ||
|
||
public interface SegmentResultListener{ | ||
void onSuccess(Bitmap result); | ||
void onFail(); | ||
} | ||
} |
Oops, something went wrong.