⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagemerger.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.esri.solutions.jitk.web.tasks.export.image;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.util.List;
import java.util.Iterator;
import com.esri.adf.web.data.WebScaleBar;
import com.esri.adf.web.data.WebMap;

/**
 *
 * This class contains methods using Java Advanced Imaging (JAI)
 * to add multiple image files to one output image file.
 * The operation being used is called "composite".
 *
 * Supported input types are:
 * BMP,TIFF,JPEG,PNG,PNM, GIF(only with JAI 1.1, not with JAI 1.1.1)
 * Supported output types are:
 * BMP,TIFF,JPEG,PNG,PNM
 *
 * In order to run these methods the JAI 1.1 jars jai_core.java, jai_codec.jar
 * and mlibwrapper_jai.jar have to be added to the classpath.
 * The files mlib_jai.dll and mlib_jai_mmx.dll have to be added to the JRE/bin directory on NT.
 * On SUN libmlib_jai.so and libmlib_jai_vis.so have to be added to the jre/lib/sparc/ directory.
 * All these files can be found on the SUN Javasoft web site.
 *
 * JAI requires a graphic display.
 * This could potentially be by-passed with PJA from Etak, but there's a bug with an
 * underlying method on SUN Solaris. Therefore it's not being used here.
 * JDK 1.4 will not need a graphic display anymore, run your application with this property: -Djava.awt.headless=true
 *
 * If 2 images with transparent backgrounds get added, then the resulting background is black.
 *
 * Known issues:
 * - GIF transparency (e.g. background color is transparent) does not work - with JAI 1.1
 *
 * Copyright Geography Network Team, ESRI Inc., Redlands, CA
 */
@SuppressWarnings("unused")
public class ImageMerger extends java.lang.Object {
	private static Logger logger = LogManager.getLogger(ImageMerger.class.getName());

    private String[] _imageList;
    private double[] _transFactors;

    public ImageMerger() {
    }

	private String getUnits(int u){
		switch(u){
			case WebScaleBar.UNITS_FEET: return "Feet";
			case WebScaleBar.UNITS_KILOMETERS: return "Kilometers";
			case WebScaleBar.UNITS_METERS: return "Meters";
			case WebScaleBar.UNITS_MILES: return "Miles";
			default: return "";
		}
	}

	private int[] getDrawParameters(
		WebMap webMap, 
		WebScaleBar webScaleBar
	){
		int[] params = new int[2];
		int width = webMap.getWidth();
		int height = webMap.getHeight();
		int sd = (int)webScaleBar.getScreenDistance();
		int md = (int)webScaleBar.getMapDistance();
		// default: POSITION_BOTTOM_LEFT
		int zero = (int)((width / 15) + (sd / 2));
		int top = (int)(height * 19 / 20);
		if (webScaleBar.getPosition().equalsIgnoreCase(WebScaleBar.POSITION_BOTTOM_RIGHT)){
			zero = (int)((width * 13 / 15) - (sd / 2));
		}
		if (webScaleBar.getPosition().equalsIgnoreCase(WebScaleBar.POSITION_TOP_LEFT)){
			top = (int)(height / 20);
		}
		if (webScaleBar.getPosition().equalsIgnoreCase(WebScaleBar.POSITION_TOP_RIGHT)){
			zero = (int)((width * 13 / 15) - (sd / 2));
			top = (int)(height / 20);
		}
		params[0] = zero;
		params[1] = top;
		return params;
	}

	private void drawAlternating(
		WebMap webMap,
		WebScaleBar webScaleBar,
		java.awt.Graphics2D g2d
	){
		int[] params = getDrawParameters(webMap, webScaleBar);
		int sd = (int)webScaleBar.getScreenDistance();
		int md = (int)webScaleBar.getMapDistance();
		String units = getUnits(webScaleBar.getUnits());
		int zero = params[0];
		int top = params[1];
		int dh = g2d.getFont().getSize();
		int m = zero;
		int q = (int)(sd * 0.25);
		int h = (int)(sd * 0.5);
		int t = (int)(sd * 0.75);
		g2d.drawRect(m - sd + q, top, q, dh - 1);
		g2d.drawRect(m - sd + t, top, q, dh - 1);
		g2d.fillRect(m - sd, top, q, dh);
		g2d.fillRect(m - sd + h, top, q, dh);
		g2d.fillRect(m, top, sd, dh);
		String lbl = String.valueOf(md);
//		String hLbl = String.valueOf(md / 2);
		String zLbl = "0";
		g2d.drawString(lbl, m - sd, top - 2);
		g2d.drawString(zLbl, m, top - 2);
		g2d.drawString(lbl + " " + units, m + sd - (int)(g2d.getFont().getSize() * lbl.length() / 2), top - 2);
	}

	void drawDoubleAlternating(
		WebMap webMap,
		WebScaleBar webScaleBar,
		java.awt.Graphics2D g2d
	){
		int[] params = getDrawParameters(webMap, webScaleBar);
		int zero = params[0];
		int top = params[1];
		int sd = (int)webScaleBar.getScreenDistance();
		int md = (int)webScaleBar.getMapDistance();
		String units = getUnits(webScaleBar.getUnits());
		int dh = g2d.getFont().getSize();
		int m = zero;
		int q = (int)(sd * 0.25);
		int h = (int)(sd * 0.5);
		int t = (int)(sd * 0.75);
		dh = (int)(dh / 2);
		//		int ht = bottom - top;
		g2d.drawRect(m - sd + q, top, q, dh);
		g2d.drawRect(m - sd + t, top, q, dh);
		g2d.fillRect(m - sd, top, q, dh);
		g2d.fillRect(m - sd + h, top, q, dh);
		g2d.fillRect(m, top, sd, dh);
		g2d.fillRect(m - sd + q, top + dh, q, dh);
		g2d.fillRect(m - sd + t, top + dh, q, dh);
		g2d.drawRect(m - sd, top + dh, q, dh);
		g2d.drawRect(m - sd + h, top + dh, q, dh);
		g2d.drawRect(m, top + dh, sd, dh);
		String lbl = String.valueOf(md);
		//		String hLbl = String.valueOf(md / 2);
		String zLbl = "0";
		g2d.drawString(lbl, m - sd, top - 2);
		g2d.drawString(zLbl, m, top - 2);
		g2d.drawString(lbl + " " + units, m + sd - (int)(g2d.getFont().getSize() * lbl.length() / 2), top - 2);
	}

	void drawSingleDivision(
		WebMap webMap,
		WebScaleBar webScaleBar,
		java.awt.Graphics2D g2d
	){
		int[] params = getDrawParameters(webMap, webScaleBar);
		int zero = params[0];
		int top = params[1];
		int sd = (int)webScaleBar.getScreenDistance();
		int md = (int)webScaleBar.getMapDistance();
		String units = getUnits(webScaleBar.getUnits());
		int dh = g2d.getFont().getSize();
		int m = zero;
		g2d.fillRect(m, top, sd, dh);
		String lbl = String.valueOf(md);
		String zLbl = "0";
		g2d.drawString(zLbl, m, top - 2);
		g2d.drawString(lbl + " " + units, m + sd - (int)(g2d.getFont().getSize() * lbl.length() / 2), top - 2);
	}

	void drawSteppedScaleLine(
		WebMap webMap,
		WebScaleBar webScaleBar,
		java.awt.Graphics2D g2d
	){
		int[] params = getDrawParameters(webMap, webScaleBar);
		int zero = params[0];
		int top = params[1];
		int sd = (int)webScaleBar.getScreenDistance();
		int md = (int)webScaleBar.getMapDistance();
		String units = getUnits(webScaleBar.getUnits());
		int dh = g2d.getFont().getSize();
		int m = zero;
		int q = (int)(sd * 0.25);
		int h = (int)(sd * 0.5);
		int t = (int)(sd * 0.75);

		int[] x = new int[12];
		int[] y = new int[12];

		x[0] = x[1] = m - sd;
		x[2] = x[3] = m - sd + q;
		x[4] = x[5] = m - sd + h;
		x[6] = x[7] = m - sd + t; 
		x[8] = x[9] = m;
		x[10] = x[11] = m + sd;

		y[0] = y[3] = y[4] = y[7] = y[8] = y[11] = top + dh;
		y[1] = y[2] = y[5] = y[6] = y[9] = y[10] = top;

		g2d.drawPolyline(x, y, 12);
		String lbl = String.valueOf(md);
		String zLbl = "0";
		g2d.drawString(lbl, m - sd, top - 2);
		g2d.drawString(zLbl, m, top - 2);
		g2d.drawString(lbl + " " + units, m + sd - (int)(g2d.getFont().getSize() * lbl.length() / 2), top - 2);
	}


    /**
    * Merge images from multiple files into one image file.
    * Input variables:
    * imagesList: list of input images. This can contain URLs or a full paths.
    *             The first image in the list is the one at the very bottom of the output image.
    *             The last image in the list is the one at the very top of the output image
    * transparencyFactors: list for transparency factors of all images in the imageList.
    *                      The values range from 0.0 (fully transparent) to 1.0 (opaque).
    *                      If transparencyFactors is null, the first image in the list
    *                      is not transparent and the others are 50% transparent
    * outputFileName: full path name to the output image
    * outputFileType: supported types are BMP, JPEG, PNG, TIFF and PNM
    * debug: if set to true it will write debug messages to standard output
    */

    public void mergeImages(
        List<byte[]> imageList,
        String outputFileName,
        String outputFileType,
		WebMap webMap,
		WebScaleBar webScaleBar
    ) throws java.net.MalformedURLException, java.io.IOException, java.io.FileNotFoundException, Exception {
//		double[] transparencyFactors;
        javax.media.jai.PlanarImage srcBottom = null;
        javax.media.jai.PlanarImage srcTop = null;
        double alphaBottom = 1;
        String bottomImageExtension = "";
        String topImageExtension = "";

		Iterator<byte[]> it = imageList.iterator();
		boolean firstImage = true;
		while (it.hasNext()){
			byte[] imageData = (byte[])it.next();
			if (firstImage){
				srcBottom = readSourceImage(imageData);
				alphaBottom = 1.0;//transparencyFactors[0];
				firstImage = false;
			}
			if (!firstImage){
				srcTop = readSourceImage(imageData);
				if (!it.hasNext()){
					int width = srcBottom.getWidth();
					int height = srcBottom.getHeight();
					java.awt.image.SampleModel sm = srcTop.getSampleModel();
					java.awt.image.ColorModel cm = srcTop.getColorModel();
					javax.media.jai.TiledImage tiledImage = new javax.media.jai.TiledImage(0, 0, width, height, 0, 0, sm, cm);
					java.awt.image.Raster ras = srcTop.getData();
					java.awt.image.WritableRaster transImage = ras.createCompatibleWritableRaster(0, 0, width, height);

					if (webScaleBar.isScaleBarAvailable()){
						java.awt.image.BufferedImage bufImg = new java.awt.image.BufferedImage(cm, transImage, true, null);
						java.awt.Font font = new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 10);
						java.awt.Graphics2D g2d = bufImg.createGraphics();
						g2d.setColor(java.awt.Color.BLACK);
						g2d.setFont(font);

						if (webScaleBar.getType().equalsIgnoreCase(WebScaleBar.TYPE_ALTERNATING))
							drawAlternating(webMap, webScaleBar, g2d);
						if (webScaleBar.getType().equalsIgnoreCase(WebScaleBar.TYPE_DOUBLE_ALTERNATING))
							drawDoubleAlternating(webMap, webScaleBar, g2d);
						if (webScaleBar.getType().equalsIgnoreCase(WebScaleBar.TYPE_SINGLE_DIVISION))
							drawSingleDivision(webMap, webScaleBar, g2d);
						if (webScaleBar.getType().equalsIgnoreCase(WebScaleBar.TYPE_STEPPED_SCALE_LINE))
							drawSteppedScaleLine(webMap, webScaleBar, g2d);
					}

					int x = width - srcTop.getWidth();
					transImage.setDataElements(x, 0, ras);
					tiledImage.setData(transImage);
					srcTop = tiledImage;
				}
                // get alpha images
				if (srcBottom.getNumBands() < 3 && srcBottom.getNumBands() > 4){
					java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();

					javax.media.jai.ColorCube colorMap = javax.media.jai.ColorCube.BYTE_496;
					javax.media.jai.KernelJAI[] ditherMask = javax.media.jai.KernelJAI.DITHER_MASK_443;
					pb.addSource(srcBottom).add(colorMap).add(ditherMask);
					srcBottom = javax.media.jai.JAI.create("ordereddither", pb);
				}
				if (srcTop.getNumBands() < 3 && srcTop.getNumBands() > 4){
					java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();

					javax.media.jai.ColorCube colorMap = javax.media.jai.ColorCube.BYTE_496;
					javax.media.jai.KernelJAI[] ditherMask = javax.media.jai.KernelJAI.DITHER_MASK_443;
					pb.addSource(srcTop).add(colorMap).add(ditherMask);
					srcTop = javax.media.jai.JAI.create("ordereddither", pb);
				}
                javax.media.jai.PlanarImage afaBottom= calculateAlphaImage(srcBottom,alphaBottom,bottomImageExtension,true);
//                javax.media.jai.PlanarImage afaTop = calculateAlphaImage(srcTop,transparencyFactors[0],topImageExtension,false);
                javax.media.jai.PlanarImage afaTop = calculateAlphaImage(srcTop,1.0,topImageExtension,false);
                // get images with no alpha value
                srcBottom = checkImageBands(srcBottom);
                srcTop = checkImageBands(srcTop);
                // do the merge
                javax.media.jai.PlanarImage outImage = mergeImages(srcTop,srcBottom,afaTop,afaBottom);

                if (it.hasNext()) {
                    // we need to merge more images
                    srcBottom = outImage;
                    alphaBottom = 1;
                    bottomImageExtension = "";
                } else {
                    // save output image
                    saveImage(outImage,outputFileName,outputFileType, true);
                }
			}
		}
	}

⌨️ 快捷键说明

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