當前位置:首頁 » 編程語言 » java圖片轉換

java圖片轉換

發布時間: 2023-07-14 16:02:33

A. 利用java實現圖片翻轉的代碼

重載渲染控制項的paintComponent(Graphics
g)方法.
設你當前圖像實例為img,已初始化,需要旋轉的角度為ang
public
void
paintComponent(Graphics
g){
super.paintCompoent(g);
Graphics2D
g2d
=
(Graphics2D)g;
g2d.rotate(-angle);
g2d.drawImage(img,0,0,this.getWidth(),this.getHeight(),null);
}
Graphics,Graphics2D
類中有對當前描繪環境進行仿射變換的方法,包括translate,scale,rotate,也可以直接設置仿射變換矩陣,利用這點就可以根據所需要的實現方式來進行描繪.

B. java把圖片轉換成二進制流

public static void main(String[] args) throws Exception {

File file = new File("d:\L.jpg");//圖片

FileInputStream fis = new FileInputStream(file);//把圖片變成流

FileOutputStream fos = new FileOutputStream(new File("E:\L.jpg")); //把圖片流寫入E盤

byte[] read = new byte[1024]; //每次讀取的位元組 可以自己定義 256 512 1024 2048 等。。。

int len = 0;

while((len = fis.read(read))!= -1){ //讀取變成流的圖片

fos.write(read,0,len);//寫入圖片

}

fis.close();//關閉輸入流

fos.close();//關閉輸出流

}

C. 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);
}
}
}

熱點內容
oracle定義存儲過程 發布:2025-02-08 16:54:35 瀏覽:147
mac玩飢荒要什麼配置 發布:2025-02-08 16:52:18 瀏覽:680
androidattributeset 發布:2025-02-08 16:51:23 瀏覽:422
c語言調用函數返回值 發布:2025-02-08 16:51:19 瀏覽:786
有壓縮錢嗎 發布:2025-02-08 16:34:01 瀏覽:517
折紙手工解壓小方塊 發布:2025-02-08 16:32:45 瀏覽:254
php與運算符 發布:2025-02-08 16:32:45 瀏覽:764
如何用伺服器搭建懸賞平台 發布:2025-02-08 16:29:53 瀏覽:280
ftp伺服器破解版 發布:2025-02-08 16:28:41 瀏覽:523
mysql配置訪問ip 發布:2025-02-08 16:22:49 瀏覽:116