
正文
文件流转图片java代码 文件流转图片java代码怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java读取RTF文件里的所有图片,并将所有图片保存成图片文件 求代码
public int nextInt(int n) {
if (n = 0)
throw new IllegalArgumentException("n must be positive");
if ((n -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) 31);
int bits, val;
do {
bits = next(31);
val = bits % n;
} while (bits - val + (n-1) 0);
return val;
}
相关问答
Q1: Java怎么做到把图片转换成流存入数据库,然后怎么再把图片显示出来。
oracle如下
数据库中提供文件流转图片java代码了两种字段类型 Blob 和 Clob 用于存储大型字符串或二进制数据(如图片)。
Blob 采用单字节存储文件流转图片java代码,适合保存二进制数据文件流转图片java代码,如图片文件。
Clob 采用多字节存储,适合保存大型文本数据。
首先创建一个空 Blob/Clob 字段,再从这个空 Blob/Clob字段获取游标,例如下面的代码文件流转图片java代码:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通过oralce.sql.BLOB/CLOB.empty_lob()构造空Blob/Clob对象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次对读出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 将二进制数据写入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 将字符串写入Clob
resClob.putString( 1 , " this is a clob " );
// 再将Blob/Clob字段更新到数据库
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();
Q2: 从文件中读取图片和写入图片到文件里的java代码是什么?
首先导入各种需要文件流转图片java代码的包文件流转图片java代码:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
读取图片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根据文件流转图片java代码你实际情况改文件路径吧
array[0] = image;
图片读出来了。
如果文件流转图片java代码你有一个Image对象文件流转图片java代码,想把它写入文件可以这样做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "png", new File("f:\\test.png"));
Q3: java pdf转图片问题
搜索添加spire.pdf.jar文件为依赖,pdf转图片代码如下:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.spire.pdf.PdfDocument;
import javax.imageio.ImageIO;
public class toImage {
public static void main(String[] args) throws IOException {
//加载PDF文件
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Sample.pdf");
//保存PDF的每一页到图片
BufferedImage image;
for (int i = 0; i doc.getPages().getCount(); i++) {
image = doc.saveAsImage(i);
File file = new File( String.format("ToImage-img-%d.png", i));
ImageIO.write(image, "PNG", file);
}
doc.close();
}
}
Q4: 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; i pdffile.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();
}
}
});
}
}
Q5: 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));
boolean success = convert.convert();
System.out.println("文本转图片:" + (success ? "成功" : "失败"));
}
}
文本转图片类 TextToImage.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
/**
* 文本转图片类
* @author YY2924 2014/11/18
* @version 1.0
*/
public class TextToImage {
/** 文本文件 */
private File textFile;
/** 图片文件 */
private File imageFile;
/** 图片 */
private BufferedImage image;
/** 图片宽度 */
private final int IMAGE_WIDTH = 400;
/** 图片高度 */
private final int IMAGE_HEIGHT = 600;
/** 图片类型 */
private final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
/**
* 构造函数
* @param textFile 文本文件
* @param imageFile 图片文件
*/
public TextToImage(File textFile,File imageFile){
this.textFile = textFile;
this.imageFile = imageFile;
this.image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE);
}
/**
* 将文本文件里文字,写入到图片中保存
* @return boolean true,写入成功;false,写入失败
*/
public boolean convert() {
//读取文本文件
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(textFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
//获取图像上下文
Graphics g = createGraphics(image);
String line;
//图片中文本行高
final int Y_LINEHEIGHT = 15;
int lineNum = 1;
try {
while((line = reader.readLine()) != null){
g.drawString(line, 0, lineNum * Y_LINEHEIGHT);
lineNum++;
}
g.dispose();
//保存为jpg图片
FileOutputStream fos = new FileOutputStream(imageFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 获取到图像上下文
* @param image 图片
* @return Graphics
*/
private Graphics createGraphics(BufferedImage image){
Graphics g = image.createGraphics();
g.setColor(Color.WHITE); //设置背景色
g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);//绘制背景
g.setColor(Color.BLACK); //设置前景色
g.setFont(new Font("微软雅黑", Font.PLAIN, 12)); //设置字体
return g;
}
}
特别注意:程序中使用到了com.sun.image.codec.jpeg.JPEGImageEncoder和 com.sun.image.codec.jpeg.JPEGCodec ,这 两个是sun的专用API,Eclipse会报错。
解决办法:
Eclipse软件,Windows-Preferences-Java-Complicer-Errors/Warnings,Deprecated and restricted API-Forbidden reference 改为 Warnning。
如果还是报错,在工程上build path,先移除JRE System Library,然后再添加JRE System Library。
关于文件流转图片java代码和文件流转图片java代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






