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);
}
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();
}
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();
}
}
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;
}
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);
}
Aggregations