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

熱點內容
300人用什麼電腦伺服器好 發布:2025-07-02 08:47:42 瀏覽:901
52好壓縮 發布:2025-07-02 08:24:16 瀏覽:246
javahttp發送http請求 發布:2025-07-02 08:17:05 瀏覽:226
美國編譯的青少經典書第三輯 發布:2025-07-02 08:16:59 瀏覽:949
阿里雲伺服器強制重啟 發布:2025-07-02 08:14:55 瀏覽:663
sql的procedure 發布:2025-07-02 08:14:54 瀏覽:819
拼多多腳本定製 發布:2025-07-02 08:14:12 瀏覽:304
2018新款雅閣什麼配置有檔把 發布:2025-07-02 08:09:12 瀏覽:8
新手搭建Linux伺服器的難度 發布:2025-07-02 08:09:00 瀏覽:730
安卓系統哪個適合小孩子用 發布:2025-07-02 08:06:03 瀏覽:41