Search in sources :

Example 6 with EncodedImage

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

the class Import9Patch method create9Patch.

// GEN-LAST:event_okButtonActionPerformed
private void create9Patch(List<BufferedImage> biList, List<Integer> densities) {
    List<com.codename1.ui.EncodedImage> topLeftCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> topRightCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> bottomLeftCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> bottomRightCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> centerCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> topImageCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> bottomImageCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> leftImageCodenameOne = new ArrayList<EncodedImage>();
    List<com.codename1.ui.EncodedImage> rightImageCodenameOne = new ArrayList<EncodedImage>();
    for (BufferedImage bi : biList) {
        int left = 0;
        for (int x = 0; x < bi.getWidth(); x++) {
            int pixel = bi.getRGB(x, 0);
            if ((pixel & 0xff000000) == 0xff000000) {
                break;
            }
            left++;
        }
        int right = 0;
        for (int x = bi.getWidth() - 1; x > 0; x--) {
            int pixel = bi.getRGB(x, 0);
            if ((pixel & 0xff000000) == 0xff000000) {
                break;
            }
            right++;
        }
        int top = 0;
        for (int y = 0; y < bi.getHeight(); y++) {
            int pixel = bi.getRGB(0, y);
            if ((pixel & 0xff000000) == 0xff000000) {
                break;
            }
            top++;
        }
        int bottom = 0;
        for (int y = bi.getHeight() - 1; y > 0; y--) {
            int pixel = bi.getRGB(0, y);
            if ((pixel & 0xff000000) == 0xff000000) {
                break;
            }
            bottom++;
        }
        bi = bi.getSubimage(1, 1, bi.getWidth() - 2, bi.getHeight() - 2);
        top--;
        bottom--;
        left--;
        right--;
        BufferedImage topLeft = bi.getSubimage(0, 0, left, top);
        BufferedImage topRight = bi.getSubimage(bi.getWidth() - right, 0, right, top);
        BufferedImage bottomLeft = bi.getSubimage(0, bi.getHeight() - bottom, left, bottom);
        BufferedImage bottomRight = bi.getSubimage(bi.getWidth() - right, bi.getHeight() - bottom, right, bottom);
        BufferedImage center = bi.getSubimage(left, top, bi.getWidth() - right - left, bi.getHeight() - bottom - top);
        BufferedImage topImage = bi.getSubimage(left, 0, bi.getWidth() - left - right, top);
        BufferedImage bottomImage = bi.getSubimage(left, bi.getHeight() - bottom, bi.getWidth() - left - right, bottom);
        BufferedImage leftImage = bi.getSubimage(0, top, left, bi.getHeight() - top - bottom);
        BufferedImage rightImage = bi.getSubimage(bi.getWidth() - right, top, right, bi.getHeight() - top - bottom);
        topLeftCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(topLeft)));
        topRightCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(topRight)));
        bottomLeftCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(bottomLeft)));
        bottomRightCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(bottomRight)));
        centerCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(center)));
        topImageCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(topImage)));
        bottomImageCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(bottomImage)));
        leftImageCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(leftImage)));
        rightImageCodenameOne.add(com.codename1.ui.EncodedImage.create(ImageBorderCuttingWizard.toPng(rightImage)));
    }
    String prefix = (String) uiidCombo.getSelectedItem() + ".";
    switch(styleState.getSelectedIndex()) {
        case 1:
            prefix += "sel#";
            break;
        case 2:
            prefix += "press#";
            break;
        case 3:
            prefix += "dis#";
            break;
    }
    com.codename1.ui.EncodedImage topLeftCodenameOneE = storeImage(topLeftCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage topRightCodenameOneE = storeImage(topRightCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage bottomLeftCodenameOneE = storeImage(bottomLeftCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage bottomRightCodenameOneE = storeImage(bottomRightCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage centerCodenameOneE = storeImage(centerCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage topImageCodenameOneE = storeImage(topImageCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage bottomImageCodenameOneE = storeImage(bottomImageCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage leftImageCodenameOneE = storeImage(leftImageCodenameOne, densities, prefix + " ");
    com.codename1.ui.EncodedImage rightImageCodenameOneE = storeImage(rightImageCodenameOne, densities, prefix + " ");
    com.codename1.ui.plaf.Border b = com.codename1.ui.plaf.Border.createImageScaledBorder(topImageCodenameOneE, bottomImageCodenameOneE, leftImageCodenameOneE, rightImageCodenameOneE, topLeftCodenameOneE, topRightCodenameOneE, bottomLeftCodenameOneE, bottomRightCodenameOneE, centerCodenameOneE);
    Hashtable newTheme = new Hashtable(res.getTheme(theme));
    newTheme.put(prefix + "border", b);
    res.setTheme(theme, newTheme);
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) EncodedImage(com.codename1.ui.EncodedImage)

Example 7 with EncodedImage

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

the class Util method readObject.

/**
 * <p>Reads an object from the stream, notice that this is the inverse of the
 * {@link #writeObject(java.lang.Object, java.io.DataOutputStream)}.</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 input the source input stream
 * @throws IOException thrown by the stream
 */
public static Object readObject(DataInputStream input) throws IOException {
    try {
        if (!input.readBoolean()) {
            return null;
        }
        String type = input.readUTF();
        if ("int".equals(type)) {
            return new Integer(input.readInt());
        }
        if ("byte".equals(type)) {
            return new Byte(input.readByte());
        }
        if ("short".equals(type)) {
            return new Short(input.readShort());
        }
        if ("long".equals(type)) {
            return new Long(input.readLong());
        }
        if ("float".equals(type)) {
            return new Float(input.readFloat());
        }
        if ("double".equals(type)) {
            return new Double(input.readDouble());
        }
        if ("bool".equals(type)) {
            return new Boolean(input.readBoolean());
        }
        if ("String".equals(type)) {
            return input.readUTF();
        }
        if ("Date".equals(type)) {
            return new Date(input.readLong());
        }
        if ("ObjectArray".equals(type)) {
            Object[] v = new Object[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = readObject(input);
            }
            return v;
        }
        if ("ByteArray".equals(type)) {
            byte[] v = new byte[input.readInt()];
            input.readFully(v);
            return v;
        }
        if ("LongArray".equals(type)) {
            long[] v = new long[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readLong();
            }
            return v;
        }
        if ("ShortArray".equals(type)) {
            short[] v = new short[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readShort();
            }
            return v;
        }
        if ("DoubleArray".equals(type)) {
            double[] v = new double[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readDouble();
            }
            return v;
        }
        if ("FloatArray".equals(type)) {
            float[] v = new float[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readFloat();
            }
            return v;
        }
        if ("IntArray".equals(type)) {
            int[] v = new int[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readInt();
            }
            return v;
        }
        if ("java.util.Vector".equals(type)) {
            Vector v = new Vector();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.addElement(readObject(input));
            }
            return v;
        }
        if ("java.util.Hashtable".equals(type)) {
            Hashtable v = new Hashtable();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("java.util.Collection".equals(type)) {
            Collection v = new ArrayList();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.add(readObject(input));
            }
            return v;
        }
        if ("java.util.Map".equals(type)) {
            Map v = new HashMap();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("EncodedImage".equals(type)) {
            int width = input.readInt();
            int height = input.readInt();
            boolean op = input.readBoolean();
            byte[] data = new byte[input.readInt()];
            input.readFully(data);
            return EncodedImage.create(data, width, height, op);
        }
        Class cls = (Class) externalizables.get(type);
        if (cls != null) {
            Object o = cls.newInstance();
            if (o instanceof Externalizable) {
                Externalizable ex = (Externalizable) o;
                ex.internalize(input.readInt(), input);
                return ex;
            } else {
                PropertyBusinessObject pb = (PropertyBusinessObject) o;
                pb.getPropertyIndex().asExternalizable().internalize(input.readInt(), input);
                return pb;
            }
        }
        throw new IOException("Object type not supported: " + type);
    } catch (InstantiationException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    } catch (IllegalAccessException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Vector(java.util.Vector) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Date(java.util.Date) Collection(java.util.Collection) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Externalizable(com.codename1.io.Externalizable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with EncodedImage

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

the class FaceBookAccess method getPictureAndWait.

/**
 * Gets the picture of the given facebook object id
 *
 * @param id the object id to query
 * @param toScale picture dimension or null
 * @return the picture
 */
public EncodedImage getPictureAndWait(String id, Dimension toScale) {
    ImageDownloadService im = new ImageDownloadService(getImageURL(id, toScale), (ActionListener) null);
    NetworkManager.getInstance().addToQueueAndWait(im);
    return im.getResult();
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService)

Example 9 with EncodedImage

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

the class FileEncodedImage method create.

/**
 * Creates an encoded image that maps to a local file thus allowing to
 * seamlessly fetch files as needed. This only works reasonably well for very small
 * files. This version of the method creates the file from an input stream
 *
 * @param fileName the name of the file
 * @param i input stream from which to create the file
 * @param width the width of the file or -1 if unknown (notice that this will improve performance)
 * @param height the height of the file or -1 if unknown (notice that this will improve performance)
 * @return image that will load the file seamlessly
 */
public static FileEncodedImage create(String fileName, InputStream i, int width, int height) throws IOException {
    EncodedImage e = EncodedImage.create(i);
    FileEncodedImage f = new FileEncodedImage(fileName, width, height, true);
    f.data = e.getImageData();
    OutputStream o = FileSystemStorage.getInstance().openOutputStream(fileName);
    o.write(f.data);
    o.close();
    return f;
}
Also used : OutputStream(java.io.OutputStream) EncodedImage(com.codename1.ui.EncodedImage)

Example 10 with EncodedImage

use of com.codename1.ui.EncodedImage 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;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReplaceableImage(com.codename1.components.ReplaceableImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(com.codename1.ui.Image) ReplaceableImage(com.codename1.components.ReplaceableImage) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage) ImageIO(com.codename1.ui.util.ImageIO)

Aggregations

EncodedImage (com.codename1.ui.EncodedImage)21 IOException (java.io.IOException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Image (com.codename1.ui.Image)6 AnimationObject (com.codename1.ui.animations.AnimationObject)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Hashtable (java.util.Hashtable)5 Map (java.util.Map)5 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)4 ImageIO (com.codename1.ui.util.ImageIO)4 ConnectionRequest (com.codename1.io.ConnectionRequest)3 ArrayList (java.util.ArrayList)3 InfiniteProgress (com.codename1.components.InfiniteProgress)2 MapObject (com.codename1.googlemaps.MapContainer.MapObject)2 Externalizable (com.codename1.io.Externalizable)2 JSONParser (com.codename1.io.JSONParser)2 Coord (com.codename1.maps.Coord)2