This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.
Camera and barcode
Capture a photo — Camera.shot()
Camera cam = new Camera();
cam.cameraType = Camera.CameraType.PICKER; // ⚠️ no default — always set it
cam.captureMode = Camera.Preview.PHOTO;
new Thread(() -> {
String caminho = cam.shot(); // file path, or null if cancelled
if (caminho == null) return;
Component.onUI(() -> {
try { imageView.image(new Image(caminho)); Component.repaint(); }
catch (Exception e) { Debug.log("erro: " + e); }
});
}).start();
⚠️
click()no longer exists — the capture method isshot()(it returns aStringwith the path ornullif the user cancels).
Configuration
| Field | Values |
|---|---|
cameraType |
DEFAULT (built-in camera) · PICKER (PhotoPicker) · GALLERY (gallery) — no default |
captureMode |
Preview.PHOTO · MUTED_VIDEO · AUDIO_VIDEO |
videoQuality |
AVQuality.HIGH/MEDIUM/LOW |
resolutionWidth/Height (static) |
resolution (always in LANDSCAPE) |
Read a barcode — CameraBarcode.readBarcode(Image)
Reads 1D and 2D (QR) from an already-captured image (via ZXing):
Image img = new Image(caminho);
com.google.zxing.Result r = CameraBarcode.readBarcode(img); // null if no code was found
if (r != null) {
String codigo = r.getText();
Debug.log("Código: " + codigo);
}
Real limitations
Camera.shot()opens native UI — call it off the paint thread and return withonUI(...).CameraBarcode.readBarcodeis NOT thread-safe — do not call it from several threads at once.nullreturns are normal (cancelled / no code found) — always check.- On Mac ARM the capture may return
null(platform limitation).
Best practices
- Always set
cameraType(there is no default). - Handle the
nullreturn fromshot()and fromreadBarcode(...). - To only pick from the gallery, use the PhotoPicker.
Common mistakes
- Calling
click()(doesn’t exist) instead ofshot(). - Not setting
cameraType. - Calling
readBarcodeconcurrently (it is not thread-safe).
See also
Was this page helpful?Thanks for the feedback!