懒得多说,直接代码了。。。。
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi;
public class TiffTest {
public static void main(String[] args) throws IOException {
BufferedImage bi = ImageIO.read(new File("D:/images/00.jpg"));// 通过imageio将图像载入
int h = bi.getHeight();// 获取图像的高
int w = bi.getWidth();// 获取图像的宽
int[][] gray = new int[w][h];
for (int x = 0; x SW) {
int max = new Color(255, 255, 255).getRGB();
nbi.setRGB(x, y, max);
} else {
int min = new Color(0, 0, 0).getRGB();
nbi.setRGB(x, y, min);
}
}
}
TIFFImageWriterSpi tiffws = new TIFFImageWriterSpi();
ImageWriter writer = tiffws.createWriterInstance();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.6");
param.setCompressionQuality(0f);
File outFile = new File("D:/images/二值化后_有压缩.tiff");
ImageOutputStream ios = ImageIO.createImageOutputStream(outFile);
writer.setOutput(ios);
writer.write(null, new IIOImage(nbi, null, null), param);
}
public static int getGray(int rgb) {
Color c = new Color(rgb);
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
int top = (r + g + b) / 3;
return (int) (top);
}
/**
* 自己加周围8个灰度值再除以9,算出其相对灰度值
* @param gray
* @param x
* @param y
* @param w
* @param h
* @return
*/
public static int getAverageColor(int[][] gray, int x, int y, int w, int h) {
int rs = gray[x][y] + (x == 0 ? 255 : gray[x - 1][y]) + (x == 0 || y == 0 ? 255 : gray[x - 1][y - 1])
+ (x == 0 || y == h - 1 ? 255 : gray[x - 1][y + 1]) + (y == 0 ? 255 : gray[x][y - 1])
+ (y == h - 1 ? 255 : gray[x][y + 1]) + (x == w - 1 ? 255 : gray[x + 1][y])
+ (x == w - 1 || y == 0 ? 255 : gray[x + 1][y - 1])
+ (x == w - 1 || y == h - 1 ? 255 : gray[x + 1][y + 1]);
return rs / 9;
}
/**
* 转换JPG图片文件格式为TIFF
* @param srcFile File 要转换格式的原文件
* @param resFile File 转换格式后的目标文件
* @throws IOException 抛出IO异常
*/
public static InputStream convertImageToTIFF() throws IOException {
long a = System.currentTimeMillis();
InputStream fis = pressImage();
ByteArrayOutputStream fos = new ByteArrayOutputStream();
TIFFEncodeParam encodeParam = new TIFFEncodeParam();
encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
JPEGDecodeParam decodeParam = new JPEGDecodeParam();
ImageDecoder decoder = ImageCodec.createImageDecoder("JPEG", fis, decodeParam);
RenderedImage image = decoder.decodeAsRenderedImage();
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", fos, encodeParam);
encoder.encode(image);
InputStream is = new ByteArrayInputStream(fos.toByteArray());
System.out.println("convertImageToTIFF -- "+(System.currentTimeMillis() - a));
return is;
}
/**
* 转换ImageOutputStream 为byte[] */
public static byte[] getBytes(ImageOutputStream ios) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream(255);
long counter = 0;
try {
ios.seek(0);
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
try {
bos.write(ios.readByte());
counter++;
} catch (EOFException e) {
System.out.println("End of Image Stream");
break;
} catch (IOException e) {
System.out.println("Error processing the Image Stream");
break;
}
}
byte[] retValue = bos.toByteArray();
return retValue;
}
/**
* 合并图片 */
public final static InputStream pressImage() {
try {
long a = System.currentTimeMillis();
int x = 30;
int y = 50;
float alpha = 1f;
BufferedImage bufferedImage = createBufferedImage Graphics2D g = bufferedImage.createGraphics();
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(waterImage, x, y, width_1 +100, height_1+100, null); // 水印文件结束
g.drawImage(waterImage, x+600, y, width_1 +100, height_1+100, null); // 水印文件结束
g.drawImage(waterImage, x+1100, y, width_1 +100, height_1+100, null); // 水印文件结束
g.drawImage(waterImage, x, y+600, width_1 +100, height_1+100, null); // 水印文件结束
g.drawImage(waterImage, x+600, y+600, width_1 +100, height_1+100, null); // 水印文件结束
g.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, os);
//File f = new File("D:/workspaceFlex/FlexTiff/src/assets/images/12.jpg");
//ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, f);
InputStream is = new ByteArrayInputStream(os.toByteArray());
System.out.println("pressImage -- "+(System.currentTimeMillis() - a));
return is;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static BufferedImage createBufferedImage() {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File("D:/workspaceFlex/FlexTiff/src/assets/images/570119458887_20120907210013_b.tif"));
} catch (IOException e) {
e.printStackTrace();
}
return bimg;
}
private final static Image waterImage = createBufferedImage();
}