當前位置:首頁 » 編程語言 » 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);
}
}
}

熱點內容
戰地5默認是什麼伺服器 發布:2025-09-18 17:59:32 瀏覽:296
安卓變ios系統主題怎麼弄 發布:2025-09-18 17:54:07 瀏覽:875
linux出口ip 發布:2025-09-18 17:51:57 瀏覽:936
androidbitmap使用 發布:2025-09-18 17:49:20 瀏覽:230
數字日期加密 發布:2025-09-18 17:43:46 瀏覽:495
網吧電腦顯示未連接上桌面伺服器 發布:2025-09-18 17:37:17 瀏覽:693
電腦壓縮文件怎麼解壓 發布:2025-09-18 17:27:59 瀏覽:383
資料庫數據類型表 發布:2025-09-18 17:11:56 瀏覽:27
java如何生產執行bat腳本 發布:2025-09-18 16:53:25 瀏覽:558
湖北的伺服器地址是多少 發布:2025-09-18 16:51:09 瀏覽:490