Class Capture
Capture can "capture" media files from the device e.g. record audio, video and snap photos.
Notice that files returned by this class are potentially temporary files and might be
deleted by the OS in the future.
The code below demonstrates the capturing of a photo thru this API:
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);
}
});
The code below demonstrates capturing and playing back audio files using this API:
Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);
FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
for(String file : fs.listFiles(recordingsDir)) {
MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(recordingsDir + file, false);
m.play();
} catch(IOException err) {
Log.e(err);
}
});
hi.add(mb);
}
hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
try {
String file = Capture.captureAudio();
if(file != null) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
String fileName =sd.format(new Date());
String filePath = recordingsDir + fileName;
Util.copy(fs.openInputStream(file), fs.openOutputStream(filePath));
MultiButton mb = new MultiButton(fileName);
mb.addActionListener((e) -> {
try {
Media m = MediaManager.createMedia(filePath, false);
m.play();
} catch(IOException err) {
Log.e(err);
}
});
hi.add(mb);
hi.revalidate();
}
} catch(IOException err) {
Log.e(err);
}
});
} catch(IOException err) {
Log.e(err);
}
hi.show();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringCapture the audio, blocking version that holds the EDT; alternatively you can use the Media API.static StringcaptureAudio(MediaRecorderBuilder recordingOptions) Capture the audio, blocking version that holds the EDT; alternatively you can use the Media API.static voidcaptureAudio(MediaRecorderBuilder recorderOptions, ActionListener<ActionEvent> response) This method tries to invoke the device native hardware to capture audio.static voidcaptureAudio(ActionListener<ActionEvent> response) This method tries to invoke the device native hardware to capture audio.static StringInvokes the camera and takes a photo synchronously while blocking the EDTstatic StringcapturePhoto(int width, int height) Invokes the camera and takes a photo synchronously while blocking the EDT, the sample below demonstrates a simple usage and applying a mask to the resultstatic voidcapturePhoto(ActionListener<ActionEvent> response) This method tries to invoke the device native camera to capture images.static StringSame as captureVideo only a blocking version that holds the EDTstatic StringcaptureVideo(VideoCaptureConstraints constraints) Same ascom.codename1.ui.events.ActionListener)only a blocking version that holds the EDT.static voidcaptureVideo(VideoCaptureConstraints constraints, ActionListener<ActionEvent> response) Captures video with some constraints, like width, height, and max length.static voidcaptureVideo(ActionListener<ActionEvent> response) This method tries to invoke the device native camera to capture video.static booleanReturns true if the device has camera false otherwise.
-
Constructor Details
-
Capture
public Capture()
-
-
Method Details
-
hasCamera
public static boolean hasCamera()Returns true if the device has camera false otherwise.
Returns
true if the device has a camera
-
capturePhoto
This method tries to invoke the device native camera to capture images. The method returns immediately and the response will be sent asynchronously to the given ActionListener Object The image is saved as a jpeg to a file on the device.
use this in the actionPerformed to retrieve the file path String path = (String) evt.getSource();
if evt returns null the image capture was canceled by the user.
Parameters
response: a callback Object to retrieve the file path
Throws
RuntimeException: if this feature failed or unsupported on the platform
-
capturePhoto
Invokes the camera and takes a photo synchronously while blocking the EDT
Returns
the photo file location or null if the user canceled
-
captureAudio
Capture the audio, blocking version that holds the EDT; alternatively you can use the Media API.
Returns
the audio file location or null if the user canceled
-
captureAudio
Capture the audio, blocking version that holds the EDT; alternatively you can use the Media API.
Returns
the audio file location or null if the user canceled
Since
7.0
-
captureVideo
Same as captureVideo only a blocking version that holds the EDT
Returns
the photo file location or null if the user canceled
-
captureVideo
Same as
com.codename1.ui.events.ActionListener)only a blocking version that holds the EDT.Parameters
constraints
Returns
A video file location or null if the user canceled.
Since
7.0
-
capturePhoto
Invokes the camera and takes a photo synchronously while blocking the EDT, the sample below demonstrates a simple usage and applying a mask to the result
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); } });Parameters
-
width: the target width for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1 -
height: the target height for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
Returns
the photo file location or null if the user canceled
-
-
captureAudio
This method tries to invoke the device native hardware to capture audio. The method returns immediately and the response will be sent asynchronously to the given ActionListener Object The audio is saved to a file on the device.
use this in the actionPerformed to retrieve the file path String path = (String) evt.getSource();
Parameters
response: a callback Object to retrieve the file path
Throws
RuntimeException: if this feature failed or unsupported on the platform
-
captureAudio
public static void captureAudio(MediaRecorderBuilder recorderOptions, ActionListener<ActionEvent> response) This method tries to invoke the device native hardware to capture audio. The method returns immediately and the response will be sent asynchronously to the given ActionListener Object The audio record settings are specified in the recorderOptions parameter.
use this in the actionPerformed to retrieve the file path. String path = (String) evt.getSource();
Parameters
response: a callback Object to retrieve the file path
Throws
RuntimeException: if this feature failed or unsupported on the platform
Since
7.0
-
captureVideo
public static void captureVideo(VideoCaptureConstraints constraints, ActionListener<ActionEvent> response) Captures video with some constraints, like width, height, and max length. Video constraints may not be supported on all platforms. Use
VideoCaptureConstraints#isSupported()andVideoCaptureConstraints#isSizeSupported()to check whether constraints are supported on the current platform. If constraints are not supported, then, in the worst case, this will fall back to just use#captureVideo(com.codename1.ui.events.ActionListener), i.e. capture with no constraints.Parameters
-
constraints: The constraints to use for the video capture. -
response: a callback Object to retrieve the file path
Since
7.0
-
-
captureVideo
This method tries to invoke the device native camera to capture video. The method returns immediately and the response will be sent asynchronously to the given ActionListener Object The video is saved to a file on the device.
use this in the actionPerformed to retrieve the file path String path = (String) evt.getSource();
Parameters
response: a callback Object to retrieve the file path
Throws
RuntimeException: if this feature failed or unsupported on the platform
See also
- #captureVideo(com.codename1.capture.VideoCaptureConstraints, com.codename1.ui.events.ActionListener)
-