public abstract class Capture

  1. Object
  2. 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

public static boolean hasCamera()Returns true if the device has camera false otherwise.
public static void capturePhoto(ActionListener<ActionEvent> response)This method tries to invoke the device native camera to capture images.
public static String capturePhoto()Invokes the camera and takes a photo synchronously while blocking the EDT
public static String captureAudio()Capture the audio, blocking version that holds the EDT; alternatively you can use the Media API.
public static String captureAudio(MediaRecorderBuilder recordingOptions)Capture the audio, blocking version that holds the EDT; alternatively you can use the Media API.
public static String captureVideo()Same as captureVideo only a blocking version that holds the EDT
public static String captureVideo(VideoCaptureConstraints constraints)Same as com.codename1.ui.events.ActionListener) only a blocking version that holds the EDT.
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
public static void captureAudio(ActionListener<ActionEvent> response)This method tries to invoke the device native hardware to capture audio.
public static void captureAudio(MediaRecorderBuilder recorderOptions, ActionListener<ActionEvent> response)This method tries to invoke the device native hardware to capture audio.
public static void captureVideo(VideoCaptureConstraints constraints, ActionListener<ActionEvent> response)Captures video with some constraints, like width, height, and max length.
public static void captureVideo(ActionListener<ActionEvent> response)This method tries to invoke the device native camera to capture video.

Inherited methods

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

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

response ActionListener<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()
Invokes the camera and takes a photo synchronously while blocking the EDT

Returns

the photo file location or null if the user canceled

captureAudio

public static String 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

public static String captureAudio(MediaRecorderBuilder recordingOptions)
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

captureVideo

public static String captureVideo()
Same as captureVideo only a blocking version that holds the EDT

Returns

the photo file location or null if the user canceled

captureVideo

public static String captureVideo(VideoCaptureConstraints constraints)
Same as com.codename1.ui.events.ActionListener) only a blocking version that holds the EDT.

Returns

A video file location or null if the user canceled.

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

width int
the target width for the image if possible, some platforms don’t support scaling. To maintain aspect ratio set to -1
height int
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

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

response ActionListener<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

recorderOptions MediaRecorderBuilder
Not documented.
response ActionListener<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)
Captures video with some constraints, like width, height, and max length. Video constraints may not be supported on all platforms. Use 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

constraints VideoCaptureConstraints
The constraints to use for the video capture.
response ActionListener<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

response ActionListener<ActionEvent>
a callback Object to retrieve the file path

Throws

RuntimeException
if this feature failed or unsupported on the platform