Search in sources :

Example 61 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class GameCanvasImplementation method capturePhoto.

public void capturePhoto(ActionListener response) {
    captureResponse = response;
    final Form current = Display.getInstance().getCurrent();
    final Form cam = new Form();
    cam.setScrollable(false);
    cam.setTransitionInAnimator(CommonTransitions.createEmpty());
    cam.setTransitionOutAnimator(CommonTransitions.createEmpty());
    cam.setLayout(new BorderLayout());
    cam.show();
    String platform = System.getProperty("microedition.platform");
    MMAPIPlayer p = null;
    if (platform != null && platform.indexOf("Nokia") >= 0) {
        try {
            p = MMAPIPlayer.createPlayer("capture://image", null);
        } catch (Throwable e) {
        // Ignore all exceptions for image capture, continue with video capture...
        }
    }
    if (p == null) {
        try {
            p = MMAPIPlayer.createPlayer("capture://video", null);
        } catch (Exception e) {
            // The Nokia 2630 throws this if image/video capture is not supported
            throw new RuntimeException("Image/video capture not supported on this phone");
        }
    }
    final MMAPIPlayer player = p;
    MIDPVideoComponent video = new MIDPVideoComponent(player, canvas);
    video.play();
    video.setVisible(true);
    cam.addCommand(new com.codename1.ui.Command("Cancel") {

        public void actionPerformed(ActionEvent evt) {
            if (player != null) {
                player.cleanup();
            }
            captureResponse.actionPerformed(null);
            current.showBack();
        }
    });
    final ActionListener l = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            try {
                cam.removeAll();
                VideoControl cnt = (VideoControl) player.nativePlayer.getControl("VideoControl");
                byte[] pic = cnt.getSnapshot("encoding=jpeg&width=" + current.getWidth() + "&height=" + current.getHeight());
                String imagePath = getOutputMediaFile() + ".jpg";
                OutputStream out = null;
                try {
                    if (pic != null) {
                        out = FileSystemStorage.getInstance().openOutputStream(imagePath);
                        out.write(pic);
                    }
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    System.out.println("failed to store picture to " + imagePath);
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                        player.cleanup();
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
                captureResponse.actionPerformed(new ActionEvent(imagePath));
                current.showBack();
            } catch (Throwable ex) {
                ex.printStackTrace();
                System.out.println("failed to take picture");
                current.showBack();
            }
        }
    };
    cam.addGameKeyListener(Display.GAME_FIRE, l);
    Container cn = new Container(new BorderLayout()) {

        public void pointerReleased(int x, int y) {
            l.actionPerformed(null);
        }
    };
    cn.setFocusable(true);
    cn.addComponent(BorderLayout.CENTER, video);
    cam.addComponent(BorderLayout.CENTER, cn);
    cam.revalidate();
// cam.addPointerReleasedListener(l);
}
Also used : Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) ActionListener(com.codename1.ui.events.ActionListener) VideoControl(javax.microedition.media.control.VideoControl)

Example 62 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class BarCodeScanner method startScaning.

private void startScaning(ScanResult callback) {
    this.callback = callback;
    try {
        // Add the listener for scan and cancel
        Container cmdContainer = new Container(new FlowLayout(Component.CENTER));
        Button scanButton = new Button(new Command("Scan") {

            public void actionPerformed(ActionEvent evt) {
                cameraForm.repaint();
                if (snapshotThread != null) {
                    snapshotThread.continueRun();
                }
            }
        });
        Button cancelButton = new Button(new Command("Cancel") {

            public void actionPerformed(ActionEvent evt) {
                if (snapshotThread != null) {
                    snapshotThread.stop();
                    cancelScan();
                }
            }
        });
        cmdContainer.addComponent(scanButton);
        cmdContainer.addComponent(cancelButton);
        cameraForm = new Form();
        cameraForm.setScrollable(false);
        cameraForm.setLayout(new BorderLayout());
        cameraForm.addComponent(BorderLayout.CENTER, media.getVideoComponent());
        cameraForm.addComponent(BorderLayout.SOUTH, cmdContainer);
    } catch (Exception e) {
        // throw new AppException("Image/video capture not supported on this phone", e).setCode(97);
        e.printStackTrace();
    }
    startScan();
}
Also used : Container(com.codename1.ui.Container) FlowLayout(com.codename1.ui.layouts.FlowLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) Command(com.codename1.ui.Command) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 63 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class Component method deinitializeImpl.

/**
 * Cleansup the initialization flags in the hierachy, notice that paint calls might
 * still occur after deinitilization mostly to perform transitions etc.
 * <p>However interactivity, animation and event tracking code can and probably
 * should be removed by this method.
 */
void deinitializeImpl() {
    if (isInitialized()) {
        hideNativeOverlay();
        paintLockRelease();
        setInitialized(false);
        setDirtyRegion(null);
        Style stl = getStyle();
        Image i = stl.getBgImage();
        if (i != null) {
            i.unlock();
        } else {
            Border b = stl.getBorder();
            if (b != null) {
                b.unlock();
            }
        }
        Painter p = stl.getBgPainter();
        if (p instanceof BGPainter) {
            ((BGPainter) p).radialCache = null;
        }
        deinitialize();
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Border(com.codename1.ui.plaf.Border)

Example 64 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class Component method paintLock.

/**
 * This method locks the component so it will always paint the given image
 * instead of running through its paint logic. This is useful when running
 * transitions that might be quite expensive on the device. A lock should
 * be released using paintLockRelease(), it is implicitly released when
 * a component is deinitialized although a component doesn't need to be initialized
 * to be locked!<br>
 * If the component is not opaque null is always returned!
 * <p>Duplicate calls to this method won't produce duplicate locks, in case of
 * a soft lock the return value will always be null.
 *
 * @param hardLock indicates whether the lock uses a hard or a soft reference to the image
 * @return the image in case of a hard lock
 */
public Image paintLock(boolean hardLock) {
    if (!paintLockEnableChecked) {
        paintLockEnableChecked = true;
        paintLockEnabled = Display.getInstance().getProperty("paintLockEnabled", "true").equals("true");
    }
    if (!paintLockEnabled || !Display.getInstance().areMutableImagesFast()) {
        return null;
    }
    if ((getStyle().getBgTransparency() & 0xff) != 0xff) {
        return null;
    }
    if (paintLockImage == null) {
        paintLockImage = Image.createImage(getWidth(), getHeight());
        int x = getX();
        int y = getY();
        setX(0);
        setY(0);
        paintInternalImpl(((Image) paintLockImage).getGraphics(), false);
        setX(x);
        setY(y);
        if (hardLock) {
            return (Image) paintLockImage;
        } else {
            paintLockImage = Display.getInstance().createSoftWeakRef(paintLockImage);
        }
    } else {
        if (hardLock) {
            return (Image) paintLockImage;
        }
    }
    return null;
}
Also used : Point(com.codename1.ui.geom.Point)

Example 65 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class Component method drawPainters.

private void drawPainters(com.codename1.ui.Graphics g, Component par, Component c, int x, int y, int w, int h) {
    if (flatten && getWidth() > 0 && getHeight() > 0) {
        Image i = (Image) getClientProperty("$FLAT");
        int absX = getAbsoluteX() + getScrollX();
        int absY = getAbsoluteY() + getScrollY();
        if (i == null || i.getWidth() != getWidth() || i.getHeight() != getHeight()) {
            i = Image.createImage(getWidth(), getHeight());
            Graphics tg = i.getGraphics();
            // tg.translate(g.getTranslateX(), g.getTranslateY());
            drawPaintersImpl(tg, par, c, x, y, w, h);
            paintBackgroundImpl(tg);
            putClientProperty("$FLAT", i);
        }
        int tx = g.getTranslateX();
        int ty = g.getTranslateY();
        g.translate(-tx + absX, -ty + absY);
        g.drawImage(i, 0, 0);
        g.translate(tx - absX, ty - absY);
        return;
    }
    drawPaintersImpl(g, par, c, x, y, w, h);
}
Also used : Point(com.codename1.ui.geom.Point)

Aggregations

Image (com.codename1.ui.Image)82 EncodedImage (com.codename1.ui.EncodedImage)46 IOException (java.io.IOException)29 Hashtable (java.util.Hashtable)19 AnimationObject (com.codename1.ui.animations.AnimationObject)18 BufferedImage (java.awt.image.BufferedImage)18 Style (com.codename1.ui.plaf.Style)16 Form (com.codename1.ui.Form)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 Dimension (com.codename1.ui.geom.Dimension)14 FontImage (com.codename1.ui.FontImage)13 BorderLayout (com.codename1.ui.layouts.BorderLayout)13 Timeline (com.codename1.ui.animations.Timeline)12 ActionListener (com.codename1.ui.events.ActionListener)12 Border (com.codename1.ui.plaf.Border)12 Container (com.codename1.ui.Container)11 InputStream (java.io.InputStream)11 Graphics (com.codename1.ui.Graphics)10 Label (com.codename1.ui.Label)10 Button (com.codename1.ui.Button)9