1. 怎麼實現用java 把tif格式的圖片轉換成jpg
用photoshop打開,另存為jpg格式就可以了。注意很多tif圖片都是cmyk四色的(印刷格式),如果想把圖片應用於網路上,要轉換成rgb三色的。方法就是打開後,點擊圖像—〉模式—〉rgb顏色。
2. java 實現 tif圖片(多頁的)轉換成jpg
多頁單個tif文件轉換為多個jpg文件
需要官方的一些包支持(具體參考源碼),上網找找即可。
源碼:
-------------------------
import java.io.*;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.JPEGEncodeParam;
import java.awt.image.RenderedImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;
import java.awt.image.renderable.ParameterBlock;
public class MultiPageRead {
public static void main(String[] args) throws IOException {
new MultiPageRead().doitJAI();
}
public void doitJAI() throws IOException {
FileSeekableStream ss = new FileSeekableStream("./zhaoming.tif");
TIFFDecodeParam param0 = null;
TIFFEncodeParam param = new TIFFEncodeParam();
JPEGEncodeParam param1 = new JPEGEncodeParam();
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, param0);
int count = dec.getNumPages();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(false); // Intel
System.out.println("This TIF has " + count + " image(s)");
for (int i = 0; i < count; i++) {
RenderedImage page = dec.decodeAsRenderedImage(i);
File f = new File("./fk_" + i + ".jpg");
System.out.println("Saving " + f.getCanonicalPath());
ParameterBlock pb = new ParameterBlock();
pb.addSource(page);
pb.add(f.toString());
pb.add("JPEG");
pb.add(param1);
//JAI.create("filestore",pb);
RenderedOp r = JAI.create("filestore",pb);
r.dispose();
//RenderedOp op = JAI.create("filestore", page, "./zhaoming_" + i + ".jpg", "JPEG", param1);
}
}
}
3. 關於通過java實現圖片格式的轉換
import java.io.*; import java.awt.*; import java.awt.image.*;
import java.awt.Graphics; import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;
public class ImageCut {
/**
* 縮放圖像
* @param srcImageFile源圖像文件地址
* @param result縮放後的圖像地址
* @param scale縮放比例
* @param flag縮放選擇:true 放大; false 縮小;
*/
public static void scale(String srcImageFile, String result, int scale,
boolean flag) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件
int width = src.getWidth(); // 得到源圖寬
int height = src.getHeight(); // 得到源圖長
if (flag) {// 放大
width = width * scale;
height = height * scale;
} else {// 縮小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 繪制縮小後的圖
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 輸出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 圖像切割
* @param srcImageFile源圖像地址
* @param descDir切片目標文件夾
* @param destWidth目標切片寬度
* @param destHeight目標切片高度
*/
public static void cut(String srcImageFile, String descDir, int destWidth,
int destHeight) {
try {
Image img;
ImageFilter cropFilter; // 讀取源圖像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源圖寬度
int srcHeight = bi.getWidth(); // 源圖高度
if (srcWidth > destWidth && srcHeight > destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
destWidth = 200; // 切片寬度
destHeight = 150; // 切片高度
int cols = 0; // 切片橫向數量
int rows = 0; // 切片縱向數量
// 計算切片的橫向和縱向數量
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) Math.floor(srcWidth / destWidth) + 1;
}
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) Math.floor(srcHeight / destHeight) + 1;
}
// 循環建立切片
// 改進的想法:是否可用多線程加快切割速度
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 四個參數分別為圖像起點坐標和寬高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * 200, i * 150,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 繪制縮小後的圖
g.dispose();
// 輸出為文件
ImageIO.write(tag, "JPEG", new File(descDir
+ "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 圖像類型轉換GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
public static void convert(String source, String result) {
try {
File f = new File(source);
f.canRead();
f.canWrite();
BufferedImage src = ImageIO.read(f);
ImageIO.write(src, "JPG", new File(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 彩色轉為黑白
public static void gray(String source, String result) {
try {
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//cut("e:/1.jpg", "e:/t/", 200, 150);
}
}
import java.io.*; import java.awt.*; import java.awt.image.*;
import java.awt.Graphics; import java.awt.color.ColorSpace;
import javax.imageio.ImageIO;
public class ImageCut {
/**
* 縮放圖像
* @param srcImageFile源圖像文件地址
* @param result縮放後的圖像地址
* @param scale縮放比例
* @param flag縮放選擇:true 放大; false 縮小;
*/
public static void scale(String srcImageFile, String result, int scale,
boolean flag) {
try {
BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件
int width = src.getWidth(); // 得到源圖寬
int height = src.getHeight(); // 得到源圖長
if (flag) {// 放大
width = width * scale;
height = height * scale;
} else {// 縮小
width = width / scale;
height = height / scale;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 繪制縮小後的圖
g.dispose();
ImageIO.write(tag, "JPEG", new File(result));// 輸出到文件流
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 圖像切割
* @param srcImageFile源圖像地址
* @param descDir切片目標文件夾
* @param destWidth目標切片寬度
* @param destHeight目標切片高度
*/
public static void cut(String srcImageFile, String descDir, int destWidth,
int destHeight) {
try {
Image img;
ImageFilter cropFilter; // 讀取源圖像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源圖寬度
int srcHeight = bi.getWidth(); // 源圖高度
if (srcWidth > destWidth && srcHeight > destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
destWidth = 200; // 切片寬度
destHeight = 150; // 切片高度
int cols = 0; // 切片橫向數量
int rows = 0; // 切片縱向數量
// 計算切片的橫向和縱向數量
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) Math.floor(srcWidth / destWidth) + 1;
}
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) Math.floor(srcHeight / destHeight) + 1;
}
// 循環建立切片
// 改進的想法:是否可用多線程加快切割速度
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 四個參數分別為圖像起點坐標和寬高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * 200, i * 150,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 繪制縮小後的圖
g.dispose();
// 輸出為文件
ImageIO.write(tag, "JPEG", new File(descDir
+ "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 圖像類型轉換GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
public static void convert(String source, String result) {
try {
File f = new File(source);
f.canRead();
f.canWrite();
BufferedImage src = ImageIO.read(f);
ImageIO.write(src, "JPG", new File(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 彩色轉為黑白
public static void gray(String source, String result) {
try {
BufferedImage src = ImageIO.read(new File(source));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
src = op.filter(src, null);
ImageIO.write(src, "JPEG", new File(result));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//cut("e:/1.jpg", "e:/t/", 200, 150);
}
}
參考文獻:http://www.cqtarena.com
4. java 8位灰度圖的灰度值怎麼獲取
publicintfilterRGB(intx,inty,intrgb){
inta=rgb&0xff000000;//將最高位(24-31)的信息(alpha通道)存儲到a變數
intr=(rgb>>16)&0xff;//取出次高位(16-23)紅色分量的信息
intg=(rgb>>8)&0xff;//取出中位(8-15)綠色分量的信息
intb=rgb&0xff;//取出低位(0-7)藍色分量的信息
rgb=(r*77+g*151+b*28)>>8;//NTSCluma,算出灰度值
returna|(rgb<<16)|(rgb<<8)|rgb;//將灰度值送入各個顏色分量
}