上代码:
maven依赖
javax.media
jai-core
1.1.3
javax.media
jai_codec
1.1.3
javax.media
jai_imageio
1.1
读取图片:
File file = new File(path);
//读取tif图片
ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next();
FileImageInputStream inputStream = new FileImageInputStream(file);
reader.setInput(inputStream);
return reader.read(0);
绘制图片:
//将结果画出来
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(new FileImageOutputStream(new File("./output.tif")));
writer.write(originalImage);
获取图像属性值:
image.getRGB(x, y);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int minX = originalImage.getMinX();
int minY = originalImage.getMinY();
注意事项:打jar包后无法运行,需要修改MANIFEST
或者修改maven打包参数
Solution
Include the following lines in your MANIFEST.MF file:
Specification-Title: Java Advanced Imaging Image I/O Tools
Specification-Version: 1.1
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: com.sun.media.imageio
Implementation-Version: 1.1
Implementation-Vendor: Sun Microsystems, Inc.
The values for each property can be anything (I used my specific application/version/company), as long as all six are defined.
maven打包参数
org.apache.maven.plugins
maven-assembly-plugin
2.5.5
com.fly.image.Processor
true
true
MyCompany
MyCompany
jar-with-dependencies
一个Tif多图按照类型权重取最大值程序:
package com.fly.image;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.spi.IIORegistry;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Processor {
static ArrayList dataList = new ArrayList<>(1024);
static ArrayList imageList = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in ));
System.out.println("请输入要处理的图片数目,按回车确认:");
int n = Integer.parseInt(scan.readLine());
for (int i = 0; i < n; i++) {
System.out.println("输入第 " + (i + 1) + " 张图片的路径,按回车确认:");
String path = scan.readLine();
System.out.println("输入第 " + (i + 1) + " 张图片的权重,按回车确认:");
double weight = Double.parseDouble(scan.readLine());
BufferedImage image = readTifFile(path);
imageList.add(new ImageWithWeight(image, weight));
}
scan.close();
if (imageList.isEmpty()) {
System.out.println("未发现图片,退出...");
return;
}
BufferedImage originalImage = imageList.get(0).image;
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int minX = originalImage.getMinX();
int minY = originalImage.getMinY();
int imageSize = width * height;
System.out.println("图片参数: width=" + width + ",height=" + height + ".");
//初始化数组
dataList.ensureCapacity(imageSize);
//开始读取图片像素,将像素信息放入数组中
for (int i = minX; i < width; i++) {
for (int j = minY; j < height; j++) {
PixelInfoList pixelInfoList = new PixelInfoList(i, j);
for (ImageWithWeight iw : imageList) {
//获取像素颜色值
int pixel = iw.getPixel(i, j);
PixelType pixelType = new PixelType(pixel, iw.weight);
pixelInfoList.list.add(pixelType);
}
dataList.add(pixelInfoList);
}
}
HashMap map = new HashMap<>();
//开始计算各个像素的加权最优值
dataList.forEach(pixelInfo -> {
map.clear();
pixelInfo.list.forEach(pixel -> map.compute(pixel.type, (k, v) -> v == null ? pixel.weight : v + pixel.weight));
List> collect = map.entrySet()
.stream()
.sorted((a, b) -> a.getValue() - b.getValue() > 0 ? -1 : 1)
.collect(Collectors.toList());
originalImage.setRGB(pixelInfo.x, pixelInfo.y, collect.get(0).getKey());
});
//将结果画出来
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(new FileImageOutputStream(new File("./output.tif")));
writer.write(originalImage);
}
//读取tif文件
private static BufferedImage readTifFile(String path) throws IOException {
File file = new File(path);
//读取tif图片
ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next();
FileImageInputStream inputStream = new FileImageInputStream(file);
reader.setInput(inputStream);
return reader.read(0);
}
}
/************************************************************/
package com.fly.image;
public class PixelType {
int type;
double weight;
public PixelType(int type, double weight) {
this.type = type;
this.weight = weight;
}
@Override
public String toString() {
return "PixelType{" +
"type=" + type +
", weight=" + weight +
'}';
}
}
/********************************************************************/
package com.fly.image;
import java.util.ArrayList;
public class PixelInfoList {
ArrayList list = new ArrayList<>();
int x;
int y;
public PixelInfoList(int x, int y) {
this.x = x;
this.y = y;
}
public PixelInfoList() {
}
}
/*********************************************************/
package com.fly.image;
import java.awt.image.BufferedImage;
public class ImageWithWeight {
BufferedImage image;
double weight;
public ImageWithWeight(BufferedImage image, double weight) {
this.image = image;
this.weight = weight;
}
public int getPixel(int x, int y) {
return this.image.getRGB(x, y);
}
@Override
public String toString() {
return "ImageWithWeight{" +
"image=" + image +
", weight=" + weight +
'}';
}
}
maven配置
4.0.0
com.fly
image
1.0-SNAPSHOT
jar
org.apache.maven.plugins
maven-compiler-plugin
1.8
org.apache.maven.plugins
maven-assembly-plugin
2.5.5
com.fly.image.Processor
true
true
MyCompany
MyCompany
jar-with-dependencies
javax.media
jai-core
1.1.3
javax.media
jai_codec
1.1.3
javax.media
jai_imageio
1.1