「java实现照片转换」java实现word转图片
本篇文章给大家谈谈java实现照片转换,以及java实现word转图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
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);
}
}
}
Java中如何把图片转换成二进制流
Java中将图片转为二进制流只需要使用FileImageInputStream取得图片文件,然后使用ByteArrayOutputStream 写入到二进制流中即可,下面是详细代码:
//图片到byte数组
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
另外,如果需要将byte[]存回图片或转为String,则:
//byte数组到图片
public void byte2image(byte[] data,String path){
if(data.length3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
public String byte2string(byte[] data){
if(data==null||data.length=1) return "0x";
if(data.length200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for(int k=0;kdata.length;k++){
buf[k] = data[k]0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(int k=0;kbuf.length;k++){
if(buf[k]16) sb.append("0"+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x"+sb.toString().toUpperCase();
}
如何用JAVA转换图像格式
关于图像转换的方式,实际上操作的是图像的字节流。我的工作中遇到过将bmp文件压缩为jpg以便于网络传输的课题。所以我这里重点介绍bmp转为jpg的一个方法。
实际上,我更喜欢使用以前sun公司内部使用的api提供的转换方法,这里使用到了两个很重要的类:
com.sun.image.codec.jpeg.JPEGCodec
com.sun.image.codec.jpeg.JPEGImageEncoder
需要注意的是,它们所属的一个jar包不存在于编译目录下,但存在于运行目录下,所以我们首先需要在jre文件下找到rt.jar并导入进来以使得编译通过。
我改写了网上的一个转换代码,所得代码如下:
此外,原sun公司开源的jar包jai_corec_1.1.3.jar也提供了图片格式的转码方式,这里也提供了转码方式,仅供参考:
备注:亲自尝试,当从jpg转bmp时会转很久很久时间(看不到尽头),转得的文件可以很大,所以建议不要使用。
关于java实现照片转换和java实现word转图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-25,除非注明,否则均为
原创文章,转载请注明出处。