Search in sources :

Example 1 with EncodedImage

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

the class ImageIO method save.

/**
 * Saves an image object to the given format
 *
 * @param img the image object
 * @param response resulting image output will be written to this stream
 * @param format the format for the image either FORMAT_PNG or FORMAT_JPEG
 * @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that
 * this isn't implemented in all platforms.
 */
public void save(Image img, OutputStream response, String format, float quality) throws IOException {
    if (img instanceof EncodedImage) {
        EncodedImage i = (EncodedImage) img;
        save(new ByteArrayInputStream(i.getImageData()), response, format, i.getWidth(), i.getHeight(), quality);
    } else {
        if (img.getImage() == null) {
            Image img2 = Image.createImage(img.getWidth(), img.getHeight(), 0);
            Graphics g = img2.getGraphics();
            g.drawImage(img, 0, 0);
            saveImage(img2, response, format, quality);
        } else {
            saveImage(img, response, format, quality);
        }
    }
}
Also used : Graphics(com.codename1.ui.Graphics) ByteArrayInputStream(java.io.ByteArrayInputStream) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage)

Example 2 with EncodedImage

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

the class EditableResources method saveXMLFile.

private void saveXMLFile(File xml, File resourcesDir) throws IOException {
    // disable override for the duration of the save so stuff from the override doesn't
    // get into the main resource file
    File overrideFileBackup = overrideFile;
    EditableResources overrideResourceBackup = overrideResource;
    overrideResource = null;
    overrideFile = null;
    try {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xml), "UTF-8"));
        String[] resourceNames = getResourceNames();
        Arrays.sort(resourceNames, String.CASE_INSENSITIVE_ORDER);
        bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
        bw.write("<resource majorVersion=\"" + MAJOR_VERSION + "\" minorVersion=\"" + MINOR_VERSION + "\" useXmlUI=\"" + xmlUI + "\">\n");
        for (int iter = 0; iter < resourceNames.length; iter++) {
            String xResourceName = xmlize(resourceNames[iter]);
            // write the magic number
            byte magic = getResourceType(resourceNames[iter]);
            switch(magic) {
                case MAGIC_TIMELINE:
                case MAGIC_ANIMATION_LEGACY:
                case MAGIC_IMAGE_LEGACY:
                case MAGIC_INDEXED_IMAGE_LEGACY:
                    magic = MAGIC_IMAGE;
                    break;
                case MAGIC_THEME_LEGACY:
                    magic = MAGIC_THEME;
                    break;
                case MAGIC_FONT_LEGACY:
                    magic = MAGIC_FONT;
                    break;
            }
            switch(magic) {
                case MAGIC_IMAGE:
                    Object o = getResourceObject(resourceNames[iter]);
                    if (!(o instanceof MultiImage)) {
                        o = null;
                    }
                    bw.write("    <image name=\"" + xResourceName + "\" ");
                    com.codename1.ui.Image image = getImage(resourceNames[iter]);
                    MultiImage mi = (MultiImage) o;
                    int rType = getImageType(image, mi);
                    switch(rType) {
                        // PNG file
                        case 0xf1:
                        // JPEG File
                        case 0xf2:
                            if (image instanceof EncodedImage) {
                                byte[] data = ((EncodedImage) image).getImageData();
                                writeToFile(data, new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                            } else {
                                FileOutputStream fo = new FileOutputStream(new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                                BufferedImage buffer = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
                                buffer.setRGB(0, 0, image.getWidth(), image.getHeight(), image.getRGB(), 0, image.getWidth());
                                ImageIO.write(buffer, "png", fo);
                                fo.close();
                            }
                            break;
                        // SVG
                        case 0xf5:
                        // multiimage with SVG
                        case 0xf7:
                            SVG s = (SVG) image.getSVGDocument();
                            writeToFile(s.getSvgData(), new File(resourcesDir, normalizeFileName(resourceNames[iter])));
                            if (s.getBaseURL() != null && s.getBaseURL().length() > 0) {
                                bw.write("baseUrl=\"" + s.getBaseURL() + "\" ");
                            }
                            bw.write("type=\"svg\" ");
                            break;
                        case 0xF6:
                            File multiImageDir = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                            multiImageDir.mkdirs();
                            for (int imageIter = 0; imageIter < mi.getDpi().length; imageIter++) {
                                File f = null;
                                switch(mi.getDpi()[imageIter]) {
                                    case Display.DENSITY_4K:
                                        f = new File(multiImageDir, "4k.png");
                                        break;
                                    case Display.DENSITY_2HD:
                                        f = new File(multiImageDir, "2hd.png");
                                        break;
                                    case Display.DENSITY_560:
                                        f = new File(multiImageDir, "560.png");
                                        break;
                                    case Display.DENSITY_HD:
                                        f = new File(multiImageDir, "hd.png");
                                        break;
                                    case Display.DENSITY_VERY_HIGH:
                                        f = new File(multiImageDir, "veryhigh.png");
                                        break;
                                    case Display.DENSITY_HIGH:
                                        f = new File(multiImageDir, "high.png");
                                        break;
                                    case Display.DENSITY_MEDIUM:
                                        f = new File(multiImageDir, "medium.png");
                                        break;
                                    case Display.DENSITY_LOW:
                                        f = new File(multiImageDir, "low.png");
                                        break;
                                    case Display.DENSITY_VERY_LOW:
                                        f = new File(multiImageDir, "verylow.png");
                                        break;
                                }
                                writeToFile(mi.getInternalImages()[imageIter].getImageData(), f);
                            }
                            bw.write("type=\"multi\" ");
                            break;
                        // Timeline
                        case MAGIC_TIMELINE:
                            File timeline = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                            DataOutputStream timelineOut = new DataOutputStream(new FileOutputStream(timeline));
                            writeTimeline(timelineOut, (Timeline) image);
                            timelineOut.close();
                            bw.write("type=\"timeline\" ");
                            break;
                        // Fail this is the wrong data type
                        default:
                            throw new IOException("Illegal type while creating image: " + Integer.toHexString(rType));
                    }
                    bw.write(" />\n");
                    continue;
                case MAGIC_THEME:
                    Hashtable<String, Object> theme = getTheme(resourceNames[iter]);
                    theme.remove("name");
                    bw.write("    <theme name=\"" + xResourceName + "\">\n");
                    ArrayList<String> setOfKeys = new ArrayList<String>(theme.keySet());
                    Collections.sort(setOfKeys);
                    for (String key : setOfKeys) {
                        if (key.startsWith("@")) {
                            if (key.endsWith("Image")) {
                                bw.write("        <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
                            } else {
                                bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            }
                            continue;
                        }
                        // if this is a simple numeric value
                        if (key.endsWith("Color")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("align") || key.endsWith("textDecoration")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + ((Number) theme.get(key)).shortValue() + "\" />\n");
                            continue;
                        }
                        // if this is a short numeric value
                        if (key.endsWith("transparency")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("opacity")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // if this is a padding or margin then we will have the 4 values as bytes
                        if (key.endsWith("padding") || key.endsWith("margin")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // padding and or margin type
                        if (key.endsWith("Unit")) {
                            byte[] b = (byte[]) theme.get(key);
                            bw.write("        <val key=\"" + key + "\" value=\"" + b[0] + "," + b[1] + "," + b[2] + "," + b[3] + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("border")) {
                            Border border = (Border) theme.get(key);
                            if (border instanceof RoundBorder) {
                                RoundBorder rb = (RoundBorder) border;
                                bw.write("        <border key=\"" + key + "\" type=\"round\" " + "roundBorderColor=\"" + rb.getColor() + "\" " + "opacity=\"" + rb.getOpacity() + "\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "shadowMM=\"" + rb.isShadowMM() + "\" " + "rectangle=\"" + rb.isRectangle() + "\" />\n");
                                continue;
                            }
                            if (border instanceof RoundRectBorder) {
                                RoundRectBorder rb = (RoundRectBorder) border;
                                bw.write("        <border key=\"" + key + "\" type=\"roundRect\" " + "strokeColor=\"" + rb.getStrokeColor() + "\" " + "strokeOpacity=\"" + rb.getStrokeOpacity() + "\" " + "strokeThickness=\"" + rb.getStrokeThickness() + "\" " + "strokeMM=\"" + rb.isStrokeMM() + "\" " + "shadowSpread=\"" + rb.getShadowSpread() + "\" " + "shadowOpacity=\"" + rb.getShadowOpacity() + "\" " + "shadowX=\"" + rb.getShadowX() + "\" " + "shadowY=\"" + rb.getShadowY() + "\" " + "shadowBlur=\"" + rb.getShadowBlur() + "\" " + "topOnlyMode=\"" + rb.isTopOnlyMode() + "\" " + "bottomOnlyMode=\"" + rb.isBottomOnlyMode() + "\" " + "cornerRadius=\"" + rb.getCornerRadius() + "\" " + "bezierCorners=\"" + rb.isBezierCorners() + "\" />\n");
                                continue;
                            }
                            int type = Accessor.getType(border);
                            switch(type) {
                                case BORDER_TYPE_EMPTY:
                                    bw.write("        <border key=\"" + key + "\" type=\"empty\" />\n");
                                    continue;
                                case BORDER_TYPE_LINE:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"line\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_UNDERLINE:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"underline\" millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"underline\"  millimeters=\"" + Accessor.isMillimeters(border) + "\" thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ROUNDED:
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"rounded\" " + "thickness=\"" + Accessor.getThickness(border) + "\" arcW=\"" + Accessor.getArcWidth(border) + "\" arcH=\"" + Accessor.getArcHeight(border) + "\" color=\"" + Accessor.getColorA(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ETCHED_RAISED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_ETCHED_LOWERED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"etchedLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_BEVEL_LOWERED:
                                    // use theme colors?
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelLowered\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
                                    }
                                    continue;
                                case BORDER_TYPE_BEVEL_RAISED:
                                    if (Accessor.isThemeColors(border)) {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" />\n");
                                    } else {
                                        bw.write("        <border key=\"" + key + "\" type=\"bevelRaised\" " + "thickness=\"" + Accessor.getThickness(border) + "\" color=\"" + Accessor.getColorA(border) + "\" colorB=\"" + Accessor.getColorB(border) + "\" colorC=\"" + Accessor.getColorC(border) + "\" colorD=\"" + Accessor.getColorD(border) + "\" />\n");
                                    }
                                    continue;
                                // case BORDER_TYPE_IMAGE_SCALED:
                                case BORDER_TYPE_IMAGE:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        int resourceCount = 0;
                                        for (int counter = 0; counter < images.length; counter++) {
                                            if (images[counter] != null && findId(images[counter], true) != null) {
                                                resourceCount++;
                                            }
                                        }
                                        if (resourceCount != 2 && resourceCount != 3 && resourceCount != 8 && resourceCount != 9) {
                                            System.out.println("Odd resource count for image border: " + resourceCount);
                                            resourceCount = 2;
                                        }
                                        switch(resourceCount) {
                                            case 2:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" />\n");
                                                break;
                                            case 3:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[4], true) + "\" " + "i3=\"" + findId(images[8], true) + "\" />\n");
                                                break;
                                            case 8:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" />\n");
                                                break;
                                            case 9:
                                                bw.write("        <border key=\"" + key + "\" type=\"image\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" " + "i4=\"" + findId(images[3], true) + "\" " + "i5=\"" + findId(images[4], true) + "\" " + "i6=\"" + findId(images[5], true) + "\" " + "i7=\"" + findId(images[6], true) + "\" " + "i8=\"" + findId(images[7], true) + "\" " + "i9=\"" + findId(images[8], true) + "\" />\n");
                                                break;
                                        }
                                        continue;
                                    }
                                case BORDER_TYPE_IMAGE_HORIZONTAL:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        bw.write("        <border key=\"" + key + "\" type=\"imageH\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
                                        continue;
                                    }
                                case BORDER_TYPE_IMAGE_VERTICAL:
                                    {
                                        Image[] images = Accessor.getImages(border);
                                        bw.write("        <border key=\"" + key + "\" type=\"imageV\" " + "i1=\"" + findId(images[0], true) + "\" " + "i2=\"" + findId(images[1], true) + "\" " + "i3=\"" + findId(images[2], true) + "\" />\n");
                                        continue;
                                    }
                            }
                            continue;
                        }
                        // if this is a font
                        if (key.endsWith("font")) {
                            com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
                            // is this a new font?
                            boolean newFont = f instanceof EditorFont;
                            if (newFont) {
                                bw.write("        <font key=\"" + key + "\" type=\"named\" " + "name=\"" + findId(f) + "\" />\n");
                            } else {
                                if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
                                    EditorTTFFont ed = (EditorTTFFont) f;
                                    String fname;
                                    String ffName;
                                    if (((EditorTTFFont) f).getNativeFontName() != null) {
                                        fname = ((EditorTTFFont) f).getNativeFontName();
                                        ffName = fname;
                                    } else {
                                        fname = ed.getFontFile().getName();
                                        ffName = ((java.awt.Font) ed.getNativeFont()).getPSName();
                                    }
                                    bw.write("        <font key=\"" + key + "\" type=\"ttf\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" " + "name=\"" + fname + "\" " + "family=\"" + ffName + "\" " + "sizeSettings=\"" + ed.getSizeSetting() + "\" " + "actualSize=\"" + ed.getActualSize() + "\" />\n");
                                } else {
                                    bw.write("        <font key=\"" + key + "\" type=\"system\" " + "face=\"" + f.getFace() + "\" " + "style=\"" + f.getStyle() + "\" " + "size=\"" + f.getSize() + "\" />\n");
                                }
                            }
                            continue;
                        }
                        // if this is a background image
                        if (key.endsWith("bgImage")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + findId(theme.get(key), true) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("scaledImage")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        if (key.endsWith("derive")) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // if this is a background gradient
                        if (key.endsWith("bgGradient")) {
                            Object[] gradient = (Object[]) theme.get(key);
                            bw.write("        <gradient key=\"" + key + "\" color1=\"" + gradient[0] + "\"" + " color2=\"" + gradient[1] + "\"" + " posX=\"" + gradient[2] + "\"" + " posY=\"" + gradient[3] + "\"" + " radius=\"" + gradient[4] + "\" />\n");
                            continue;
                        }
                        if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
                            bw.write("        <val key=\"" + key + "\" value=\"" + theme.get(key) + "\" />\n");
                            continue;
                        }
                        // thow an exception no idea what this is
                        throw new IOException("Error while trying to read theme property: " + key);
                    }
                    bw.write("    </theme>\n");
                    continue;
                case MAGIC_FONT:
                    File legacyFont = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                    DataOutputStream legacyFontOut = new DataOutputStream(new FileOutputStream(legacyFont));
                    saveFont(legacyFontOut, false, resourceNames[iter]);
                    legacyFontOut.close();
                    bw.write("    <legacyFont name=\"" + xResourceName + "\" />\n");
                    continue;
                case MAGIC_DATA:
                    {
                        File dataFile = new File(resourcesDir, normalizeFileName(resourceNames[iter]));
                        DataOutputStream dataFileOut = new DataOutputStream(new FileOutputStream(dataFile));
                        InputStream i = getData(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        dataFileOut.write(data);
                        dataFileOut.close();
                        bw.write("    <data name=\"" + xResourceName + "\" />\n");
                        continue;
                    }
                case MAGIC_UI:
                    {
                        File uiXML = new File(resourcesDir, resourceNames[iter] + ".ui");
                        UIBuilderOverride u = new UIBuilderOverride();
                        com.codename1.ui.Container cnt = u.createContainer(this, resourceNames[iter]);
                        FileOutputStream fos = new FileOutputStream(uiXML);
                        writeUIXml(cnt, fos);
                        fos.close();
                        File ui = new File(resourcesDir, resourceNames[iter]);
                        DataOutputStream uiOut = new DataOutputStream(new FileOutputStream(ui));
                        InputStream i = getUi(resourceNames[iter]);
                        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
                        int val = i.read();
                        while (val != -1) {
                            outArray.write(val);
                            val = i.read();
                        }
                        byte[] data = outArray.toByteArray();
                        uiOut.write(data);
                        uiOut.close();
                        bw.write("    <ui name=\"" + xResourceName + "\" />\n");
                        continue;
                    }
                case MAGIC_L10N:
                    // we are getting the theme which allows us to acces the l10n data
                    bw.write("    <l10n name=\"" + xResourceName + "\">\n");
                    Hashtable<String, Object> l10n = getTheme(resourceNames[iter]);
                    for (String locale : l10n.keySet()) {
                        bw.write("        <lang name=\"" + locale + "\">\n");
                        Hashtable<String, String> current = (Hashtable<String, String>) l10n.get(locale);
                        for (String key : current.keySet()) {
                            String val = current.get(key);
                            bw.write("            <entry key=\"" + xmlize(key) + "\" value=\"" + xmlize(val) + "\" />\n");
                        }
                        bw.write("        </lang>\n");
                    }
                    bw.write("    </l10n>\n");
                    continue;
                default:
                    throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
            }
        }
        bw.write("</resource>\n");
        bw.close();
    } finally {
        overrideFile = overrideFileBackup;
        overrideResource = overrideResourceBackup;
    }
}
Also used : SVG(com.codename1.impl.javase.SVG) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) LegacyFont(com.codename1.ui.util.xml.LegacyFont) EditorTTFFont(com.codename1.ui.EditorTTFFont) EditorFont(com.codename1.ui.EditorFont) BufferedWriter(java.io.BufferedWriter) EditorTTFFont(com.codename1.ui.EditorTTFFont) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Image(com.codename1.ui.Image) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncodedImage(com.codename1.ui.EncodedImage) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) AnimationObject(com.codename1.ui.animations.AnimationObject) EditorFont(com.codename1.ui.EditorFont) RoundBorder(com.codename1.ui.plaf.RoundBorder) File(java.io.File) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 3 with EncodedImage

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

the class ResourceEditorView method launchOptiPngActionPerformed.

private void launchOptiPngActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_launchOptiPngActionPerformed
    if (loadedResources != null && configureOptiPNG()) {
        final ProgressMonitor pm = new ProgressMonitor(mainPanel, "Processing Images", "", 0, loadedResources.getImageResourceNames().length);
        new Thread() {

            public void run() {
                String node = Preferences.userNodeForPackage(ResourceEditorView.class).get("optiPng", null);
                int prog = 0;
                for (String imageName : loadedResources.getImageResourceNames()) {
                    if (pm.isCanceled()) {
                        pm.close();
                        return;
                    }
                    pm.setProgress(prog);
                    prog++;
                    pm.setNote(imageName);
                    Object image = loadedResources.getImage(imageName);
                    if (image instanceof com.codename1.ui.EncodedImage) {
                        if (loadedResources.getResourceObject(imageName) != image) {
                            // multi-image...
                            EditableResources.MultiImage multi = (EditableResources.MultiImage) loadedResources.getResourceObject(imageName);
                            EditableResources.MultiImage n = new EditableResources.MultiImage();
                            EncodedImage[] arr = new EncodedImage[multi.getInternalImages().length];
                            for (int iter = 0; iter < multi.getInternalImages().length; iter++) {
                                EncodedImage current = optimize(multi.getInternalImages()[iter], node);
                                if (current != null) {
                                    arr[iter] = current;
                                } else {
                                    arr[iter] = multi.getInternalImages()[iter];
                                }
                            }
                            n.setInternalImages(arr);
                            n.setDpi(multi.getDpi());
                            loadedResources.setMultiImage(imageName, n);
                        } else {
                            EncodedImage current = optimize((EncodedImage) image, node);
                            if (current != null) {
                                loadedResources.setImage(imageName, current);
                            }
                        }
                    }
                }
                pm.close();
            }
        }.start();
    }
}
Also used : ProgressMonitor(javax.swing.ProgressMonitor) AnimationObject(com.codename1.ui.animations.AnimationObject) EncodedImage(com.codename1.ui.EncodedImage) EditableResources(com.codename1.ui.util.EditableResources) EncodedImage(com.codename1.ui.EncodedImage)

Example 4 with EncodedImage

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

the class PulsateEditor method updateTimeline.

private void updateTimeline() {
    AnimationObject[] anim = new AnimationObject[get(frames) * 2 - 1];
    internalImages = new EncodedImage[get(frames)];
    int small = Math.min(get(smallSize), get(largeSize));
    int large = Math.max(get(smallSize), get(largeSize));
    int dur = get(duration);
    int segment = dur / anim.length;
    Motion calculator = Motion.createSplineMotion(small, large, dur / 2);
    float ratioOfLargest = ((float) large) / 100.0f;
    int timelineWidth = (int) (ratioOfLargest * ((float) sourceImage.getWidth()));
    int timelineHeight = (int) (ratioOfLargest * ((float) sourceImage.getHeight()));
    for (int iter = 0; iter < internalImages.length; iter++) {
        calculator.setCurrentMotionTime(segment * iter);
        int percentAtTime = calculator.getValue();
        float ratioAtTime = ((float) percentAtTime) / 100.0f;
        int currentWidth = (int) (sourceImage.getWidth() * ratioAtTime);
        int currentHeight = (int) (sourceImage.getHeight() * ratioAtTime);
        internalImages[iter] = EncodedImage.create(scale(sourceImage, currentWidth, currentHeight));
        anim[iter] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
        anim[iter].setStartTime(segment * iter);
        anim[iter].setEndTime(segment * iter + segment);
        // peek at the next frame to calculate the diff
        calculator.setCurrentMotionTime(segment * iter + segment);
        int percentAtNextFrame = calculator.getValue();
        float ratioAtNextFrame = ((float) percentAtNextFrame) / 100.0f;
        int nextWidth = (int) (sourceImage.getWidth() * ratioAtNextFrame);
        int nextHeight = (int) (sourceImage.getHeight() * ratioAtNextFrame);
        anim[iter].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentHeight, nextHeight);
        anim[iter].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, currentWidth, nextWidth);
        anim[iter].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - nextWidth) / 2);
        anim[iter].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * iter, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - nextHeight) / 2);
        // create the "inverse" shrinking animation object
        if (iter > 0) {
            int nextOffset = anim.length - iter;
            anim[nextOffset] = AnimationObject.createAnimationImage(internalImages[iter], (timelineWidth - currentWidth) / 2, (timelineHeight - currentHeight) / 2);
            anim[nextOffset].setStartTime(segment * nextOffset);
            if (iter == 1) {
                // this resolves any rounding errors that might have occured in the creation of the frames
                anim[nextOffset].setEndTime(dur);
            } else {
                anim[nextOffset].setEndTime(segment * nextOffset + segment);
            }
            // peek at the previous frame to calculate the diff
            calculator.setCurrentMotionTime(segment * iter - segment);
            int percentAtPreviousFrame = calculator.getValue();
            float ratioAtPreviousFrame = ((float) percentAtPreviousFrame) / 100.0f;
            int previousWidth = (int) (sourceImage.getWidth() * ratioAtPreviousFrame);
            int previousHeight = (int) (sourceImage.getHeight() * ratioAtPreviousFrame);
            anim[nextOffset].defineHeight(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentHeight, previousHeight);
            anim[nextOffset].defineWidth(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, currentWidth, previousWidth);
            anim[nextOffset].defineMotionX(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineWidth - currentWidth) / 2, (timelineWidth - previousWidth) / 2);
            anim[nextOffset].defineMotionY(AnimationObject.MOTION_TYPE_SPLINE, segment * nextOffset, segment, (timelineHeight - currentHeight) / 2, (timelineHeight - previousHeight) / 2);
        }
    }
    currentImage = Timeline.createTimeline(dur, anim, new com.codename1.ui.geom.Dimension(timelineWidth, timelineHeight));
    previewLabel.setIcon(currentImage);
    previewLabel.repaint();
    preview.repaint();
}
Also used : Motion(com.codename1.ui.animations.Motion) AnimationObject(com.codename1.ui.animations.AnimationObject)

Example 5 with EncodedImage

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

the class AddAndScaleMultiImage method generate.

public void generate(File[] files, EditableResources res) {
    for (File f : files) {
        try {
            BufferedImage bi = ImageIO.read(f);
            EditableResources.MultiImage newImage = new EditableResources.MultiImage();
            int[] DPIS = new int[] { com.codename1.ui.Display.DENSITY_VERY_LOW, com.codename1.ui.Display.DENSITY_LOW, com.codename1.ui.Display.DENSITY_MEDIUM, com.codename1.ui.Display.DENSITY_HIGH, com.codename1.ui.Display.DENSITY_VERY_HIGH, com.codename1.ui.Display.DENSITY_HD, com.codename1.ui.Display.DENSITY_560, com.codename1.ui.Display.DENSITY_2HD, com.codename1.ui.Display.DENSITY_4K };
            EncodedImage[] images = new EncodedImage[DPIS.length];
            int imageCount = 0;
            JSpinner[] ws = { veryLowWidth, lowWidth, mediumWidth, highWidth, veryHighWidth, hdWidth, hd560Width, hd2Width, hd4kWidth };
            JSpinner[] hs = { veryLowHeight, lowHeight, mediumHeight, highHeight, veryHighHeight, hdHeight, hd560Height, hd2Height, hd4kHeight };
            if (squareImages.isSelected()) {
                hs = ws;
            }
            for (int iter = 0; iter < ws.length; iter++) {
                images[iter] = createScale(bi, ws[iter], hs[iter]);
                if (images[iter] != null) {
                    imageCount++;
                }
            }
            if (imageCount > 0) {
                int offset = 0;
                EncodedImage[] result = new EncodedImage[imageCount];
                int[] resultDPI = new int[imageCount];
                for (int iter = 0; iter < images.length; iter++) {
                    if (images[iter] != null) {
                        result[offset] = images[iter];
                        resultDPI[offset] = DPIS[iter];
                        offset++;
                    }
                }
                newImage.setDpi(resultDPI);
                newImage.setInternalImages(result);
                String destName = f.getName();
                int count = 1;
                while (res.containsResource(destName)) {
                    destName = f.getName() + " " + count;
                }
                res.setMultiImage(destName, newImage);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error reading file: " + f, "IO Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) JSpinner(javax.swing.JSpinner) File(java.io.File) EditableResources(com.codename1.ui.util.EditableResources)

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