# Class Capture

java.lang.Object
com.codename1.capture.Capture

public abstract class Capture extends Object

`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
  Constructor
  Description
  `Capture()`

- ## Method Summary

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

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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`: 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

    ###### Since

    7.0

  - ### 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.

    ###### Parameters

    - `constraints`

    ###### Returns

    A video file location or null if the user canceled.

    ###### Since

    7.0

  - ### 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`: 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

    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`: 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()` 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`: The constraints to use for the video capture.

    - `response`: a callback Object to retrieve the file path

    ###### Since

    7.0

  - ### 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`: 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)