public abstract class Capture
- Object
- 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();
Constructors
public Capture() |
Methods
Inherited methods
Constructor details
Capture
public Capture()Method details
hasCamera
public static boolean hasCamera()Returns
capturePhoto
public static void capturePhoto(ActionListener<ActionEvent> response)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
responseActionListener<ActionEvent>- a callback Object to retrieve the file path
Throws
RuntimeException- if this feature failed or unsupported on the platform
capturePhoto
public static String capturePhoto()Returns
captureAudio
public static String captureAudio()Returns
captureAudio
public static String captureAudio(MediaRecorderBuilder recordingOptions)Returns
captureVideo
public static String captureVideo()Returns
captureVideo
public static String captureVideo(VideoCaptureConstraints constraints)com.codename1.ui.events.ActionListener) only a
blocking version that holds the EDT.Returns
capturePhoto
public static String capturePhoto(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 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
widthint- the target width for the image if possible, some platforms don’t support scaling. To maintain aspect ratio set to -1
heightint- the target height for the image if possible, some platforms don’t support scaling. To maintain aspect ratio set to -1
Returns
captureAudio
public static void captureAudio(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 is saved to a file on the device.
use this in the actionPerformed to retrieve the file path String path = (String) evt.getSource();
Parameters
responseActionListener<ActionEvent>- 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
recorderOptionsMediaRecorderBuilder- Not documented.
responseActionListener<ActionEvent>- a callback Object to retrieve the file path
Throws
RuntimeException- if this feature failed or unsupported on the platform
captureVideo
public static void captureVideo(VideoCaptureConstraints constraints, ActionListener<ActionEvent> response)VideoCaptureConstraints#isSupported() and VideoCaptureConstraints#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
constraintsVideoCaptureConstraints- The constraints to use for the video capture.
responseActionListener<ActionEvent>- a callback Object to retrieve the file path
captureVideo
public static void captureVideo(ActionListener<ActionEvent> response)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
responseActionListener<ActionEvent>- a callback Object to retrieve the file path
Throws
RuntimeException- if this feature failed or unsupported on the platform