Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created February 28, 2016 12:23
Show Gist options
  • Save codenameone/b18c37dfcc7de752e0e6 to your computer and use it in GitHub Desktop.
Save codenameone/b18c37dfcc7de752e0e6 to your computer and use it in GitHub Desktop.
Example for using the Image masking and camera capture API's in Codename One
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Rounder", new BorderLayout());
Label picture = new Label("", "Container");
hi.add(BorderLayout.CENTER, picture);
hi.getUnselectedStyle().setBgColor(0xff0000);
hi.getUnselectedStyle().setBgTransparency(255);
Style s = UIManager.getInstance().getComponentStyle("TitleCommand");
Image camera = FontImage.createMaterial(FontImage.MATERIAL_CAMERA, s);
hi.getToolbar().addCommandToRightBar("", camera, (ev) -> {
try {
int width = Display.getInstance().getDisplayWidth();
Image capturedImage = Image.createImage(Capture.capturePhoto(width, -1));
Image roundMask = Image.createImage(width, capturedImage.getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, width, width, 0, 360);
Object mask = roundMask.createMask();
capturedImage = capturedImage.applyMask(mask);
picture.setIcon(capturedImage);
hi.revalidate();
} catch(IOException err) {
Log.e(err);
}
});
@codenameone
Copy link
Author

Sample usage of Image & Capture.

From the Codename One project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment