use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class CodenameOneImplementation method openGallery.
/**
* Opens the device gallery
* The method returns immediately and the response will be sent asynchronously
* to the given ActionListener Object
*
* use this in the actionPerformed to retrieve the file path
* String path = (String) evt.getSource();
*
* @param response a callback Object to retrieve the file path
* @param type one of the following GALLERY_IMAGE, GALLERY_VIDEO, GALLERY_ALL
* @throws RuntimeException if this feature failed or unsupported on the platform
*/
public void openGallery(final ActionListener response, int type) {
final Dialog d = new Dialog("Select a picture");
d.setLayout(new BorderLayout());
FileTreeModel model = new FileTreeModel(true);
if (type == Display.GALLERY_IMAGE) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
} else if (type == Display.GALLERY_VIDEO) {
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
} else if (type == Display.GALLERY_ALL) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
}
FileTree t = new FileTree(model) {
protected Button createNodeComponent(final Object node, int depth) {
if (node == null || !getModel().isLeaf(node)) {
return super.createNodeComponent(node, depth);
}
Hashtable t = (Hashtable) Storage.getInstance().readObject("thumbnails");
if (t == null) {
t = new Hashtable();
}
final Hashtable thumbs = t;
final Button b = super.createNodeComponent(node, depth);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
response.actionPerformed(new ActionEvent(node, ActionEvent.Type.Other));
d.dispose();
}
});
final ImageIO imageio = ImageIO.getImageIO();
if (imageio != null) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
byte[] data = (byte[]) thumbs.get(node);
if (data == null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
imageio.save(FileSystemStorage.getInstance().openInputStream((String) node), out, ImageIO.FORMAT_JPEG, b.getIcon().getWidth(), b.getIcon().getHeight(), 1);
data = out.toByteArray();
thumbs.put(node, data);
Storage.getInstance().writeObject("thumbnails", thumbs);
} catch (IOException ex) {
Log.e(ex);
}
}
Image im = Image.createImage(data, 0, data.length);
b.setIcon(im);
}
});
}
return b;
}
};
d.addComponent(BorderLayout.CENTER, t);
d.placeButtonCommands(new Command[] { new Command("Cancel") });
Command c = d.showAtPosition(2, 2, 2, 2, true);
if (c != null) {
response.actionPerformed(null);
}
}
use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class Capture method capturePhoto.
/**
* <p>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</p>
* <script src="https://gist.github.com/codenameone/b18c37dfcc7de752e0e6.js"></script>
* <img src="https://www.codenameone.com/img/developer-guide/graphics-image-masking.png" alt="Picture after the capture was complete and the resulted image was rounded. The background was set to red so the rounding effect will be more noticeable" />
*
* @param width the target width for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @param height the target height for the image if possible, some platforms don't support scaling. To maintain aspect ratio set to -1
* @return the photo file location or null if the user canceled
*/
public static String capturePhoto(int width, int height) {
CallBack c = new CallBack();
if ("ios".equals(Display.getInstance().getPlatformName()) && (width != -1 || height != -1)) {
// workaround for threading issues in iOS https://github.com/codenameone/CodenameOne/issues/2246
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
if (c.url == null) {
return null;
}
ImageIO scale = Display.getInstance().getImageIO();
if (scale != null) {
try {
String path = c.url.substring(0, c.url.indexOf(".")) + "s" + c.url.substring(c.url.indexOf("."));
OutputStream os = FileSystemStorage.getInstance().openOutputStream(path);
scale.save(c.url, os, ImageIO.FORMAT_JPEG, width, height, 1);
Util.cleanup(os);
FileSystemStorage.getInstance().delete(c.url);
return path;
} catch (IOException ex) {
Log.e(ex);
}
}
} else {
c.targetWidth = width;
c.targetHeight = height;
capturePhoto(c);
Display.getInstance().invokeAndBlock(c);
}
return c.url;
}
use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class CloudImageProperty method propertyValue.
/**
* {@inheritDoc}
*/
public Object propertyValue(CloudObject obj, String propertyName) {
final String key = (String) obj.getObject(idProperty);
if (key == null) {
return placeholderImage;
}
Image image = (Image) getCache().get(key);
if (image == null) {
ReplaceableImage r = inProgress.get(key);
if (r != null) {
return r;
}
final ReplaceableImage rp = ReplaceableImage.create(placeholderImage);
inProgress.put(key, rp);
ConnectionRequest cr = new ConnectionRequest() {
private EncodedImage e;
protected void readResponse(InputStream input) throws IOException {
e = EncodedImage.create(input);
;
if (e.getWidth() != placeholderImage.getWidth() || e.getHeight() != placeholderImage.getHeight()) {
ImageIO io = ImageIO.getImageIO();
if (io != null) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(new ByteArrayInputStream(e.getImageData()), bo, ImageIO.FORMAT_JPEG, placeholderImage.getWidth(), placeholderImage.getHeight(), 0.9f);
e = EncodedImage.create(bo.toByteArray());
}
}
}
protected void postResponse() {
rp.replace(e);
getCache().put(key, e);
inProgress.remove(key);
}
};
cr.setPost(false);
cr.setUrl(CloudStorage.getInstance().getUrlForCloudFileId(key));
NetworkManager.getInstance().addToQueue(cr);
return rp;
}
return image;
}
use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class EncodedImage method scaledEncoded.
/**
* Performs scaling using ImageIO to generate an encoded Image
* @param width the width of the image, -1 to scale based on height and preserve aspect ratio
* @param height the height of the image, -1 to scale based on width and preserve aspect ratio
* @return new encoded image
*/
public EncodedImage scaledEncoded(int width, int height) {
if (width == getWidth() && height == getHeight()) {
return this;
}
if (width < 0) {
float ratio = ((float) height) / ((float) getHeight());
width = Math.max(1, (int) (getWidth() * ratio));
} else {
if (height < 0) {
float ratio = ((float) width) / ((float) getWidth());
height = Math.max(1, (int) (getHeight() * ratio));
}
}
try {
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format = ImageIO.FORMAT_PNG;
if (isOpaque() || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
if (io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
format = ImageIO.FORMAT_JPEG;
}
}
if (io.isFormatSupported(format)) {
// do an image IO scale which is more efficient
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(new ByteArrayInputStream(getImageData()), bo, format, width, height, 0.9f);
EncodedImage img = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
img.opaque = opaque;
img.opaqueChecked = opaqueChecked;
if (width > -1 && height > -1) {
img.width = width;
img.height = height;
}
return img;
}
}
} catch (IOException err) {
// normally this shouldn't happen but this will keep falling back to the existing scaled code
Log.e(err);
}
return null;
}
use of com.codename1.ui.util.ImageIO in project CodenameOne by codenameone.
the class TestUtils method screenshotTest.
/**
* The screenshot test takes a screenshot of the screen and compares it to
* a prior screenshot, if both are 100% identical the test passes. If not
* the test fails.<br>
* If this is the first time the test is run then the screenshot is taken
* and saved under the given name in the devices storage. The test passes
* for this case but a warning is printed to the console. The name will have
* .png appended to it so it will be identified.<br>
* This test will only work on devices that support the ImageIO API with PNG
* file format.
*
* @param screenshotName the name to use for the storage, must be unique!
* @return true if the screenshots are identical or no prior screenshot exists
* or if the test can't be run on this device. False if a screenshot exists and
* it isn't 100% identical.
*/
public static boolean screenshotTest(String screenshotName) {
if (verbose) {
log("screenshotTest(" + screenshotName + ")");
}
try {
ImageIO io = ImageIO.getImageIO();
if (io == null || !io.isFormatSupported(ImageIO.FORMAT_PNG)) {
log("screenshot test skipped due to no image IO support for PNG format");
return true;
}
Image mute = Image.createImage(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
Display.getInstance().getCurrent().paint(mute.getGraphics());
screenshotName = screenshotName + ".png";
if (Storage.getInstance().exists(screenshotName)) {
int[] rgba = mute.getRGBCached();
Image orig = Image.createImage(Storage.getInstance().createInputStream(screenshotName));
int[] origRgba = orig.getRGBCached();
orig = null;
for (int iter = 0; iter < rgba.length; iter++) {
if (rgba[iter] != origRgba[iter]) {
log("screenshots do not match at offset " + iter + " saving additional image under " + screenshotName + ".fail");
io.save(mute, Storage.getInstance().createOutputStream(screenshotName + ".fail"), ImageIO.FORMAT_PNG, 1);
return false;
}
}
} else {
io.save(mute, Storage.getInstance().createOutputStream(screenshotName), ImageIO.FORMAT_PNG, 1);
}
return true;
} catch (IOException err) {
log(err);
return false;
}
}
Aggregations