📄 imagefuser.java
字号:
/*
COPYRIGHT 1992-2004 ESRI
TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the
Copyright Laws of the United States.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: contracts@esri.com
*/
package com.esri.aims.mtier.model.util;
/**
* <p>ImageFuser</p>
* <p>Description: Fuses multiple images toghether with transparencies. Intended for use with the</p>
* <p>ArcIMS JavaConnector</p>
* <p>Environmental Systems Research Institute</p>
* <p>Example:
* String[] images = new String[2];
* images[0] = "C:/arcims/output/world.jpg";
* images[1] = "C:/arcims/output/world_mountains.jpg";
*
* double[] transparencies = new double[2];
* transparencies[0] = 1.0;
* transparencies[1] = 0.5;
*
* ImageFuser imageFuser = new ImageFuser();
* imageFuser.fuseImages(images, transparencies, "C:/arcims/output/world_fused.jpg", "PNG");
* </p>
*/
import javax.media.jai.RenderedOp;
public class ImageFuser {
/**
* Constructs an instance of the ImageFuser class.
*/
public ImageFuser() {
}
/**
* Fuses a String array of images into one image. The output image is rendered based on the provided transparency values.
* The first image in the list always has a transparency of 1. Subsequent images in the list need to have one transparency value
* in the transparencies array for each image in the images array.
* @param images String array containing either internet, or filesystem locations of images.
* @param transparencies double array containing a transparency value for each image in the images array.
* @param outputFile path and file of new file to be generated.
* @param outputType type of image file to generate. JPG, TIFF, PNG.
* @throws java.net.MalformedURLException If URL to image in images array is not functional.
* @throws java.io.IOException
* @throws java.io.FileNotFoundException if image file in images array is not found.
* @throws Exception general exception information.
*/
public static void fuseImages(String[] images, double[] transparencies, String outputFile, String outputType)
throws java.net.MalformedURLException, java.io.IOException, java.io.FileNotFoundException, Exception{
javax.media.jai.PlanarImage bottom = null;
javax.media.jai.PlanarImage top = null;
double aBottom = 1;
String bottomImageExtension = null;
String topImageExtension = null;
for(int i=0; i<images.length; i++){
if(i == 0){
bottom = readSourceImage(images[i]);
aBottom = 1.0;
bottomImageExtension = images[i].substring(images[i].length() - 3, images[i].length());
}
else{
top = readSourceImage(images[i]);
topImageExtension = images[i].substring(images[i].length() - 3, images[i].length());
}
if(i > 0){
javax.media.jai.PlanarImage afaBottom = calculateAlphaImage(bottom, aBottom, bottomImageExtension);
javax.media.jai.PlanarImage afaTop = calculateAlphaImage(top,transparencies[i],topImageExtension);
javax.media.jai.PlanarImage outImage = fuseImages(top, bottom, afaTop, afaBottom);
if(i < images.length-1){
bottom = outImage;
aBottom = 1;
bottomImageExtension = "";
}
else{
saveImage(outImage,outputFile,outputType);
}
}
}
}
//Calcuates the alpha value of a PlanarImage, based on the provided transparency. Returns a PlanarImage with the
//calculated alpha values.
private static javax.media.jai.PlanarImage calculateAlphaImage(javax.media.jai.PlanarImage src, double transparency, String imageExtension) throws Exception{
javax.media.jai.PlanarImage afa = null;
int alpha = (int)(transparency*255);
Byte[] bandValues = new Byte[3];
bandValues[0] = new Byte((byte)alpha);
bandValues[1] = new Byte((byte)alpha);
bandValues[2] = new Byte((byte)alpha);
java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();
pb.add((float)src.getWidth());
pb.add((float)src.getHeight());
pb.add(bandValues);
return afa = javax.media.jai.JAI.create("constant",pb,null);
}
//Reads in image sources from both internet locations, and filesystems. Returns a PlanarImage of read image.
private static javax.media.jai.PlanarImage readSourceImage(String fileName) throws java.net.MalformedURLException, java.io.IOException{
javax.media.jai.PlanarImage src = null;
if(fileName.startsWith("http://")){
java.net.URL imageURL = new java.net.URL(fileName);
java.net.URLConnection con = imageURL.openConnection();
java.io.InputStream imageStream = com.sun.media.jai.codec.SeekableStream.wrapInputStream(con.getInputStream(), true);
src = javax.media.jai.JAI.create("stream", imageStream);
}
else
src = javax.media.jai.JAI.create("fileload", fileName);
if(fileName.substring(fileName.length() - 3, fileName.length()).equalsIgnoreCase("png") && src.getNumBands() == 1){
com.sun.media.jai.codec.SeekableStream is = null;
if(fileName.startsWith("http://")){
java.net.URL imageUrl = new java.net.URL(fileName);
java.net.URLConnection con = imageUrl.openConnection();
is = com.sun.media.jai.codec.SeekableStream.wrapInputStream(con.getInputStream(), true);
}
else
is = new com.sun.media.jai.codec.FileSeekableStream(fileName);
com.sun.media.jai.codec.PNGDecodeParam dec = new com.sun.media.jai.codec.PNGDecodeParam();
dec.setExpandPalette(true);
com.sun.media.jai.codec.ImageDecoder decoder = com.sun.media.jai.codec.ImageCodec.createImageDecoder("PNG",is,dec);
java.awt.image.RenderedImage im = decoder.decodeAsRenderedImage();
src = javax.media.jai.PlanarImage.wrapRenderedImage(im);
}
return src;
}
//Used internally to save the PlanarImage out to a file system.
private static void saveImage(javax.media.jai.PlanarImage src, String fileName, String type) throws java.io.FileNotFoundException, java.io.IOException{
if(type.equalsIgnoreCase("PNG8")){
type = "PNG";
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(src).add(colorMap).add(ditherMask);
src = javax.media.jai.JAI.create("ordereddither",pb);
}
else if(type.equalsIgnoreCase("JPG")){
type = "JPEG";
}
else if(type.equalsIgnoreCase("TIF")){
type = "TIFF";
}
else if(type.equalsIgnoreCase("TIFF") || type.equalsIgnoreCase("JPEG") || type.equalsIgnoreCase("PNG")){
type = "JPEG";
}
javax.media.jai.JAI.create("filestore",src,fileName,type,null);
}
//Used internally to fuse the images together into one PlanarImage.
private static javax.media.jai.PlanarImage fuseImages(javax.media.jai.PlanarImage srcTop, javax.media.jai.PlanarImage srcBottom, javax.media.jai.PlanarImage afaTop, javax.media.jai.PlanarImage afaBottom){
java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();
pb.addSource(srcTop);
pb.addSource(srcBottom);
pb.add(afaTop);
pb.add(afaBottom);
pb.add(new Boolean(false));
pb.add(javax.media.jai.operator.CompositeDescriptor.NO_DESTINATION_ALPHA);
return javax.media.jai.JAI.create("composite",pb,null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -