imageserver.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,104 行 · 第 1/3 页

JAVA
1,104
字号
        int ret = 0; // Initialize all the layer bits to zero.        for (int i = layers.length - 1; i >= 0; i--) {            if (layers[i].isVisible()) {                ret = ret | (0x00000001 << i);            }        }        return ret;    }    /**     * Use the ProjectionPainter interface of the layers to create an image.     * This approach avoids some of the timing issues that the thread model of     * the MapBean and Layers that seem to pop up from time to time. They are     * Swing components, you know. They were designed to be part of a GUI. So,     * this is a serialized, safe way to do things. Uses the default background     * set in the ImageServer.     *      * @param proj projection of map.     * @param scaledWidth scaled pixel width of final image. If you don't want     *        it scaled, use -1.     * @param scaledHeight scaled pixel height of final image. If you don't want     *        it scaled, use -1.     * @param includedLayerMask a mask signifying which of the ImageServer     *        layers to use in the image. It's assumed that the called knows     *        which layers are desired. Bit 1 of the mask refers to layer[0],     *        etc. A bit turned on means the layer will be included.     * @return a byte[] representing the formatted image.     */    public byte[] createImage(Projection proj, int scaledWidth,                              int scaledHeight, int includedLayerMask) {        return createImage(proj,                scaledWidth,                scaledHeight,                includedLayerMask,                getBackground());    }    /**     * Use the ProjectionPainter interface of the layers to create an image.     * This approach avoids some of the timing issues that the thread model of     * the MapBean and Layers that seem to pop up from time to time. They are     * Swing components, you know. They were designed to be part of a GUI. So,     * this is a serialized, safe way to do things.     *      * @param proj projection of map.     * @param scaledWidth scaled pixel width of final image. If you don't want     *        it scaled, use -1.     * @param scaledHeight scaled pixel height of final image. If you don't want     *        it scaled, use -1.     * @param includedLayerMask a mask signifying which of the ImageServer     *        layers to use in the image. It's assumed that the called knows     *        which layers are desired. Bit 1 of the mask refers to layer[0],     *        etc. A bit turned on means the layer will be included.     * @param background the background Paint to use for the image, behind the     *        layers.     * @return a byte[] representing the formatted image.     */    public byte[] createImage(Projection proj, int scaledWidth,                              int scaledHeight, int includedLayerMask,                              Paint background) {        Debug.message("imageserver",                "ImageServer: using the new ProjectionPainter interface!  createImage with layer mask.");        if (formatter == null) {            Debug.error("ImageServer.createImage: no formatter set! Can't create image.");            return new byte[0];        }        ImageFormatter imageFormatter = formatter.makeClone();        Graphics graphics = createGraphics(imageFormatter,                proj.getWidth(),                proj.getHeight());        if (graphics == null) {            return new byte[0];        }        ((Proj) proj).drawBackground((Graphics2D) graphics, background);        if (Debug.debugging("imageserver")) {            Debug.output("ImageServer: considering " + layers.length                    + " for image...");        }        if (layers != null) {            for (int i = layers.length - 1; i >= 0; i--) {                if ((includedLayerMask & (0x00000001 << i)) != 0) {                    if (Debug.debugging("imageserver")) {                        Debug.output("ImageServer: image request adding layer graphics from : "                                + layers[i].getName());                    }                    layers[i].renderDataForProjection(proj, graphics);                } else {                    if (Debug.debugging("imageserver")) {                        Debug.output("ImageServer: skipping layer graphics from : "                                + layers[i].getName());                    }                }            }        } else {            if (Debug.debugging("imageserver")) {                Debug.output("ImageServer: no layers available");            }        }        byte[] formattedImage = getFormattedImage(imageFormatter,                scaledWidth,                scaledHeight);        graphics.dispose();        return formattedImage;    }    /**     * Create a java.awt.Graphics to use for an image. The Graphics will affect     * the image contained within the ImageFormatter.     *      * @param formatter the ImageFormatter containing the image.     * @param width the pixel width of the image.     * @param height the pixel height of the image.     */    protected Graphics createGraphics(ImageFormatter formatter, int width,                                      int height) {        java.awt.Graphics graphics = null;        if (formatter != null) {            graphics = formatter.getGraphics(width, height);        } else {            Debug.error("ImageServer.createGraphics: Formatter is null, returning null graphics.");            return null;        }        if (graphics == null) {            Debug.error("ImageServer.createGraphics: NOT able to create Graphics!");            return null;        }        if (Debug.debugging("imageserver")) {            Debug.output("ImageServer.createGraphics: graphics is cool");        }        if (doAntiAliasing && graphics instanceof java.awt.Graphics2D) {            java.awt.Graphics2D g2d = (java.awt.Graphics2D) graphics;            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                    RenderingHints.VALUE_ANTIALIAS_ON);        }        return graphics;    }    /**     * Format the image that is contained in the ImageFormatter, scaling to a     * particular size if the scaledWidth and scaledHeight are greater than 0.     */    protected byte[] getFormattedImage(ImageFormatter formatter,                                       int scaledWidth, int scaledHeight) {        if (Debug.debugging("imageserver")) {            Debug.output("ImageServer: ready to create formatted image.");        }        byte[] formattedImage = null;        // Now, scale the image, if needed...        if (scaledWidth > 0 && scaledHeight > 0) {            formattedImage = formatter.getScaledImageBytes(scaledWidth,                    scaledHeight);        } else {            Debug.message("imageserver",                    "ImageServer: using full scale image (unscaled).");            formattedImage = formatter.getImageBytes();        }        return formattedImage;    }    /**     * Set the layers and image type in the properties.     */    public void setProperties(Properties props) {        setProperties((String) null, props);    }    /**     * Set the layers and image type in the properties. The properties might     * have a prefix in the file.     */    public void setProperties(String prefix, Properties props) {        setProperties(prefix, props, (Hashtable) null);    }    /**     * Set the layers and image type in the properties. The properties might     * have a prefix in the file.     */    public void setProperties(String prefix, Properties props,                              Hashtable instantiatedLayers) {        setPropertyPrefix(prefix);        prefix = PropUtils.getScopedPropertyPrefix(prefix);        layers = getLayers(props, instantiatedLayers);        formatter = getFormatters(props);        doAntiAliasing = PropUtils.booleanFromProperties(props, prefix                + AntiAliasingProperty, false);        background = getBackground(props, prefix + BackgroundProperty);    }    /**     * Determine the background color based on property settings. If the     * property key isn't found, the openmap.BackgroundColor property will be     * used.  If that isn't found, then Color.white will be returned as default.     *      * @param props properties to check     * @param propertyKey first key to check for.     * @return     */    public Paint getBackground(Properties props, String propertyKey) {        String paintString = props.getProperty(propertyKey);        if (paintString == null) {            paintString = props.getProperty(Environment.BackgroundColor);        }        Paint ret = null;        if (paintString != null) {            try {                ret = PropUtils.parseColor(paintString);            } catch (NumberFormatException nfe) {                // Color set to white below...            }        }        if (ret == null) {            ret = Color.white;        }        return ret;    }    /**     * Part of the PropertyConsumer interface. Doesn't do anything yet.     */    public Properties getProperties(Properties props) {        if (props == null) {            props = new Properties();        }        return props;    }    /**     * Part of the PropertyConsumer interface.     */    public Properties getPropertyInfo(Properties list) {        if (list == null) {            list = new Properties();        }        list.put(ImageServerLayersProperty,                "A list of marker names (space-separated) for layer definitions");        list.put(ImageFormattersProperty,                "A list of marker names (space-separated) for ImageFormatter definitions");        list.put(AntiAliasingProperty,                "Whether to use anti-aliasing for the image");        return list;    }    /**     * Part of the PropertyConsumer interface. Set the Properties prefix to use     * to scope the relevant properties passed into the setProperties method.     */    public void setPropertyPrefix(String prefix) {        propertiesPrefix = prefix;    }    /**     * Part of the PropertyConsumer interface. Get the Properties prefix used to     * scope the relevant properties passed into the setProperties method.     */    public String getPropertyPrefix() {        return propertiesPrefix;    }    /**     * Given a integer that represents, bitwise, the layers that you want out of     * the current list held by the ImageServer layer array, return an array of     * those layers.     *      * @param layerMask bit mask for desired layers, bit 0 is layer 0.     * @return layer[]     */    protected synchronized Layer[] getMaskedLayers(int layerMask) {        if (layerMask == 0xFFFFFFFF || layers == null) {            // They all want to be there            Debug.message("imageserver",                    (layers != null ? "ImageServer: image request adding all layers."                            : "ImageServer.getMaskedLayers() null layers"));            return layers;        } else {            // Use the vector as a growable array, and add the layers            // to it that the mask says should be there.            Vector layerVector = new Vector(layers.length);            for (int i = 0; i < layers.length; i++) {                if ((layerMask & (0x00000001 << i)) != 0) {                    layerVector.add(layers[i]);                    if (Debug.debugging("imageserver")) {                        Debug.output("ImageServer: image request adding layer: "                                + layers[i].getName());                    }                }            }            Layer[] imageLayers = new Layer[layerVector.size()];            return (Layer[]) layerVector.toArray(imageLayers);        }    }    /**     * Get the ImageFormatter currently used for the image creation.     *      * @return ImageFormatter.     */    public synchronized ImageFormatter getFormatter() {        return formatter;    }    /**     * Set the ImageFormatter to be used for ImageCreation.     */    public synchronized void setFormatter(ImageFormatter f) {        formatter = f;    }    /**     * Set the default formatter to the one with the given label. The label can     * be retrieved from the ImageFormatter.     *      * @param formatterLabel String for a particular formatter.     * @return true if label matches up with a known formatter, false if no     *         formatter found.     */    public synchronized boolean setFormatter(String formatterLabel) {        ImageFormatter tmpFormatter = (ImageFormatter) imageFormatters.get(formatterLabel.intern());        if (tmpFormatter != null) {            setFormatter(tmpFormatter);            return true;        } else {            return false;        }    }    /**     * Get the Hashtable used to hold the ImageFormatters. The label for each     * one is the lookup for it in the Hashtable.     *      * @return Hashtable of ImageFormatters.     */    public synchronized Hashtable getFormatters() {        return imageFormatters;    }    /**     * Set the ImageFormatter Hashtable to set up the possible choices for image     * formats.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?