use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.
the class PulsateEditor method pulsateWizard.
public void pulsateWizard(EditableResources res, JComponent parent) {
File[] f = ResourceEditorView.showOpenFileChooser("Image", ".png", ".jpg", ".jpeg", ".gif");
if (f != null && f.length > 0) {
try {
timelineName.setText(f[0].getName());
sourceImage = ImageIO.read(f[0]);
updateTimeline();
timer = new javax.swing.Timer(130, new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
if (previewLabel.animate()) {
previewLabel.repaint();
preview.repaint();
}
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
int val = JOptionPane.showConfirmDialog(parent, this, "Edit Effect", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
timer.stop();
if (val == JOptionPane.OK_OPTION) {
store(res, currentImage, timelineName.getText() + ": TL ");
for (EncodedImage img : internalImages) {
store(res, img, timelineName.getText() + ": Fr ");
}
}
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(parent, "Error in reading image file", "File Read Error", JOptionPane.ERROR_MESSAGE);
}
}
}
use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.
the class ImageDownloadService method readResponse.
/**
* {@inheritDoc}
*/
protected void readResponse(InputStream input) throws IOException {
int imageScaleWidth = -1, imageScaleHeight = -1;
if (fastScale) {
if (toScale != null) {
imageScaleWidth = toScale.getWidth();
imageScaleHeight = toScale.getHeight();
} else {
if (placeholder != null) {
imageScaleWidth = placeholder.getWidth();
imageScaleHeight = placeholder.getHeight();
}
}
}
if (cacheImages) {
if (destinationFile != null) {
result = FileEncodedImage.create(destinationFile, input, imageScaleWidth, imageScaleHeight);
} else {
EncodedImage e = EncodedImage.create(input);
if (maintainAspectRatio) {
float actualW = e.getWidth();
float actualH = e.getHeight();
float r2 = Math.min(((float) imageScaleWidth) / actualW, ((float) imageScaleHeight) / actualH);
imageScaleWidth = (int) (actualW * r2);
imageScaleHeight = (int) (actualH * r2);
}
// workaround for http://stackoverflow.com/questions/35347353/label-image-scale-issue-in-codename-one-library-3-3/35354605
if (imageScaleWidth > -1 || imageScaleHeight > -1) {
e = e.scaledEncoded(imageScaleWidth, imageScaleHeight);
}
result = StorageImage.create(cacheId, e.getImageData(), imageScaleWidth, imageScaleHeight, keep);
// if the storage has failed create the image from the stream
if (result == null) {
result = e;
}
}
} else {
result = EncodedImage.create(input);
}
}
use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.
the class Util method writeObject.
/**
* <p>Writes an object to the given output stream, notice that it should be externalizable or one of
* the supported types.</p>
*
* <p>
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
* </p>
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
*
* @param o the object to write which can be null
* @param out the destination output stream
* @throws IOException thrown by the stream
*/
public static void writeObject(Object o, DataOutputStream out) throws IOException {
if (o == null) {
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
if (o instanceof Externalizable) {
Externalizable e = (Externalizable) o;
out.writeUTF(e.getObjectId());
out.writeInt(e.getVersion());
e.externalize(out);
return;
}
if (o instanceof PropertyBusinessObject) {
Externalizable e = ((PropertyBusinessObject) o).getPropertyIndex().asExternalizable();
out.writeUTF(e.getObjectId());
out.writeInt(e.getVersion());
e.externalize(out);
return;
}
if (o instanceof Vector) {
Vector v = (Vector) o;
out.writeUTF("java.util.Vector");
int size = v.size();
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
writeObject(v.elementAt(iter), out);
}
return;
}
if (o instanceof Collection) {
Collection v = (Collection) o;
out.writeUTF("java.util.Collection");
int size = v.size();
out.writeInt(size);
for (Object cur : v) {
writeObject(cur, out);
}
return;
}
if (o instanceof Hashtable) {
Hashtable v = (Hashtable) o;
out.writeUTF("java.util.Hashtable");
out.writeInt(v.size());
Enumeration k = v.keys();
while (k.hasMoreElements()) {
Object key = k.nextElement();
writeObject(key, out);
writeObject(v.get(key), out);
}
return;
}
if (o instanceof Map) {
Map v = (Map) o;
out.writeUTF("java.util.Map");
out.writeInt(v.size());
for (Object key : v.keySet()) {
writeObject(key, out);
writeObject(v.get(key), out);
}
return;
}
if (o instanceof String) {
String v = (String) o;
out.writeUTF("String");
out.writeUTF(v);
return;
}
if (o instanceof Date) {
Date v = (Date) o;
out.writeUTF("Date");
out.writeLong(v.getTime());
return;
}
if (o instanceof Integer) {
Integer v = (Integer) o;
out.writeUTF("int");
out.writeInt(v.intValue());
return;
}
if (o instanceof Long) {
Long v = (Long) o;
out.writeUTF("long");
out.writeLong(v.longValue());
return;
}
if (o instanceof Byte) {
Byte v = (Byte) o;
out.writeUTF("byte");
out.writeByte(v.byteValue());
return;
}
if (o instanceof Short) {
Short v = (Short) o;
out.writeUTF("short");
out.writeShort(v.shortValue());
return;
}
if (o instanceof Float) {
Float v = (Float) o;
out.writeUTF("float");
out.writeFloat(v.floatValue());
return;
}
if (o instanceof Double) {
Double v = (Double) o;
out.writeUTF("double");
out.writeDouble(v.doubleValue());
return;
}
if (o instanceof Boolean) {
Boolean v = (Boolean) o;
out.writeUTF("bool");
out.writeBoolean(v.booleanValue());
return;
}
if (o instanceof EncodedImage) {
out.writeUTF("EncodedImage");
EncodedImage e = (EncodedImage) o;
out.writeInt(e.getWidth());
out.writeInt(e.getHeight());
out.writeBoolean(e.isOpaque());
byte[] b = e.getImageData();
out.writeInt(b.length);
out.write(b);
return;
}
if (instanceofObjArray(o)) {
Object[] v = (Object[]) o;
out.writeUTF("ObjectArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
writeObject(v[iter], out);
}
return;
}
if (instanceofByteArray(o)) {
byte[] v = (byte[]) o;
out.writeUTF("ByteArray");
int size = v.length;
out.writeInt(size);
out.write(v);
return;
}
if (instanceofShortArray(o)) {
short[] v = (short[]) o;
out.writeUTF("ShortArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeShort(v[iter]);
}
return;
}
if (instanceofDoubleArray(o)) {
double[] v = (double[]) o;
out.writeUTF("DoubleArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeDouble(v[iter]);
}
return;
}
if (instanceofFloatArray(o)) {
float[] v = (float[]) o;
out.writeUTF("FloatArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeFloat(v[iter]);
}
return;
}
if (instanceofIntArray(o)) {
int[] v = (int[]) o;
out.writeUTF("IntArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeInt(v[iter]);
}
return;
}
if (instanceofLongArray(o)) {
long[] v = (long[]) o;
out.writeUTF("LongArray");
int size = v.length;
out.writeInt(size);
for (int iter = 0; iter < size; iter++) {
out.writeLong(v[iter]);
}
return;
}
throw new IOException("Object type not supported: " + o.getClass().getName() + " value: " + o);
}
use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.
the class EncodedImage method createFromRGB.
/**
* Tries to create an encoded image from RGB which is more efficient,
* however if this fails it falls back to regular RGB image. This method
* is slower than creating an RGB image (not to be confused with the RGBImage class which is
* something ENTIRELY different!).
*
* @param argb an argb array
* @param width the width for the image
* @param height the height for the image
* @param jpeg uses jpeg format internally which is opaque and could be faster/smaller
* @return an image which we hope is an encoded image
*/
public static Image createFromRGB(int[] argb, int width, int height, boolean jpeg) {
Image i = Image.createImage(argb, width, height);
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format;
if (jpeg) {
if (!io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
return i;
}
format = ImageIO.FORMAT_JPEG;
} else {
if (!io.isFormatSupported(ImageIO.FORMAT_PNG)) {
return i;
}
format = ImageIO.FORMAT_PNG;
}
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(i, bo, format, 0.9f);
EncodedImage enc = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
enc.width = width;
enc.height = height;
if (jpeg) {
enc.opaque = true;
enc.opaqueChecked = true;
}
enc.cache = Display.getInstance().createSoftWeakRef(i);
return enc;
} catch (IOException err) {
Log.e(err);
}
}
return i;
}
use of com.codename1.ui.EncodedImage in project CodenameOne by codenameone.
the class EncodedImage method createFromImage.
/**
* Converts an image to encoded image
* @param i image
* @param jpeg true to try and set jpeg, will do a best effort but this isn't guaranteed
* @return an encoded image or null
*/
public static EncodedImage createFromImage(Image i, boolean jpeg) {
if (i instanceof EncodedImage) {
return ((EncodedImage) i);
}
ImageIO io = ImageIO.getImageIO();
if (io != null) {
String format;
if (jpeg) {
if (!io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
format = ImageIO.FORMAT_PNG;
} else {
format = ImageIO.FORMAT_JPEG;
}
} else {
if (!io.isFormatSupported(ImageIO.FORMAT_PNG)) {
format = ImageIO.FORMAT_JPEG;
} else {
format = ImageIO.FORMAT_PNG;
}
}
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
io.save(i, bo, format, 0.9f);
EncodedImage enc = EncodedImage.create(bo.toByteArray());
Util.cleanup(bo);
enc.width = i.getWidth();
enc.height = i.getHeight();
if (format == ImageIO.FORMAT_JPEG) {
enc.opaque = true;
enc.opaqueChecked = true;
}
enc.cache = Display.getInstance().createSoftWeakRef(i);
return enc;
} catch (IOException err) {
Log.e(err);
}
}
return null;
}
Aggregations