文件流转图片java代码 文件流转图片java代码怎么写( 二 )


File file = new File( String.format("ToImage-img-%d.png", i));
ImageIO.write(image, "PNG", file);
}
doc.close();
}
}
javaweb pdf流转jpg流怎么操作?package pdf;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.SwingUtilities;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToJpg {
public static void setup() throws IOException {
// 加载一个pdf从一个字节缓冲区
File file = new File("D:\\yangliu\\test.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
System.out.println("页数文件流转图片java代码:" + pdffile.getNumPages());
String getPdfFilePath = System.getProperty("user.dir") + "\\pdfPicFile";
System.out.println("getPdfFilePath is :" + getPdfFilePath);
for (int i = 1; ipdffile.getNumPages(); i++) {
// 画第一页到一个图像
PDFPage page = pdffile.getPage(i);
// 获得宽度和高度文件流转图片java代码的文件在默认文件流转图片java代码的变焦
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// 生成图像
Image img = page.getImage(rect.width, rect.height, rect, null,
true, true);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,
null);
// 输出到文件流
FileOutputStream out = new FileOutputStream(getPdfFilePath + "\\"
+ i + ".jpg");
System.out.println("成功保存图片到:"+getPdfFilePath+"\\"+i+".jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(1f, false); //1f是提高生成的图片质量
encoder.setJPEGEncodeParam(param);
encoder.encode(tag); // JPEG编码
out.close();
}
}
public static void main(final String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PdfToJpg.setup();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
java文本文件转化为图片文件怎么弄?文件在计算机中都是以二进制保存的 , 但系统是以文件头来区分各种文件格式的 。
也就是说,仅仅更改后缀名是不行的 。
按照你说想的,可以这么来做:
1、读取txt文本的每一行
2、创建BufferedImage图片,然后在图片上画读取到的文本
下面给出示例程序:
测试类 TextToImageExample.java
import java.io.File;
import java.util.Scanner;
/**
* 文本转图片测试类
* @author YY2924 2014/11/18
* @version 1.0
*/
public class TextToImageExample {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("输入TXT文本名称 (例如: D:/java.txt ):");
String textFileName = in.nextLine();
System.out.print("输入保存的图片名称 (例如: D:/java.jpg):");
String imageFileName = in.nextLine();
TextToImage convert = new TextToImage(new File(textFileName), new File(imageFileName));

推荐阅读