导航:首页 > 文字图片 > java文字转图片

java文字转图片

发布时间:2022-08-27 22:27:46

❶ java在生成图片的时候,让文字竖排展示,如何实现

packagehonest.imageio;

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;

importjavax.imageio.ImageIO;

/**
*图片操作类
*
*@author
*
*/
publicclassImageUtil{

privateBufferedImageimage;
privateintwidth;//图片宽度
privateintheight;//图片高度

publicImageUtil(intwidth,intheight){

this.width=width;
this.height=height;
image=newBufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
}

/**
*创建一个含有指定颜色字符串的图片
*
*@parammessage
*字符串
*@paramfontSize
*字体大小
*@paramcolor
*字体颜色
*@return图片
*/
publicBufferedImagedrawString(Stringmessage,intfontSize,Colorcolor){
Graphicsg=image.getGraphics();
g.setColor(color);
Fontf=newFont("宋体",Font.BOLD,fontSize);
g.setFont(f);
intlen=message.length();
g.drawString(message,(width-fontSize*len)/2,
(height+(int)(fontSize/1.5))/2);
g.dispose();
returnimage;
}

/**
*缩放图片
*
*@paramscaleW
*水平缩放比例
*@paramscaleY
*垂直缩放比例
*@return
*/
publicBufferedImagescale(doublescaleW,doublescaleH){
width=(int)(width*scaleW);
height=(int)(height*scaleH);

BufferedImagenewImage=newBufferedImage(width,height,
image.getType());
Graphicsg=newImage.getGraphics();
g.drawImage(image,0,0,width,height,null);
g.dispose();
image=newImage;
returnimage;
}

/**
*旋转90度旋转
*
*@return对应图片
*/
publicBufferedImagerotate(){
BufferedImagedest=newBufferedImage(height,width,
BufferedImage.TYPE_INT_ARGB);
for(inti=0;i<width;i++)
for(intj=0;j<height;j++){
dest.setRGB(height-j-1,i,image.getRGB(i,j));
}
image=dest;
returnimage;
}

/**
*合并两个图像
*
*@paramanotherImage
*另一张图片
*@return合并后的图片,如果两张图片尺寸不一致,则返回null
*/
publicBufferedImagemergeImage(BufferedImageanotherImage){

intw=anotherImage.getWidth();
inth=anotherImage.getHeight();
if(w!=width||h!=height){
returnnull;
}

for(inti=0;i<w;i++){
for(intj=0;j<h;j++){
intrgb1=image.getRGB(i,j);
intrgb2=anotherImage.getRGB(i,j);

Colorcolor1=newColor(rgb1);
Colorcolor2=newColor(rgb2);

//如果该位置两张图片均没有字体经过,则跳过
//如果跳过,则最后将会是黑色背景
if(color1.getRed()+color1.getGreen()+color1.getBlue()
+color2.getRed()+color2.getGreen()
+color2.getBlue()==0){
continue;
}

Colorcolor=newColor(
(color1.getRed()+color2.getRed())/2,
(color1.getGreen()+color2.getGreen())/2,
(color1.getBlue()+color2.getBlue())/2);
image.setRGB(i,j,color.getRGB());
}
}
returnimage;
}

/**
*保存图片intrgb1=image.getRGB(i,j);intrgb2=anotherImage.getRGB(i,j);
*rgb2=rgb1&rgb2;image.setRGB(height-i,j,rgb2);
*
*@paramfilePath
*图片路径
*/
publicvoidsave(StringfilePath){
try{
ImageIO.write(image,"png",newFile(filePath));
}catch(IOExceptione){
e.printStackTrace();
}
}

/**
*得到对应的图片
*
*@return
*/
publicBufferedImagegetImage(){
returnimage;
}
}

❷ java中怎么将word文档怎么生成图片

public class CreateWordDemo
{

public void createDocContext(String file)
throws DocumentException,IOException {

//
设置纸张大小

Document document = new
Document(PageSize.A4);

//
建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new
FileOutputStream(file));

document.open();

//
设置中文字体

BaseFont bfChinese =
BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);

//
标题字体风格

Font titleFont = new Font(bfChinese, 12,
Font.BOLD);

//
正文字体风格

Font contextFont = new Font(bfChinese, 10,
Font.NORMAL);

Paragraph title = new
Paragraph("标题");

//
设置标题格式对齐方式

title.setAlignment(Element.ALIGN_CENTER);

title.setFont(titleFont);

document.add(title);

String contextString =
"iText是一个能够快速产生PDF文件的java类库。"

+ " \n"//
换行
+
"iText的java类对于那些要产生包含文本,"

+ "表格,图形的只读文档是很有用的。它的类库尤其与java
Servlet有很好的给合。"

+
"使用iText与PDF能够使你正确的控制Servlet的输出。";

Paragraph context = new
Paragraph(contextString);

//
正文格式左对齐

context.setAlignment(Element.ALIGN_LEFT);

context.setFont(contextFont);

//
离上一段落(标题)空的行数

context.setSpacingBefore(5);

//
设置第一行空的列数

context.setFirstLineIndent(20);

document.add(context);

//
利用类FontFactory结合Font和Color可以设置各种各样字体样式Paragraph underline = new Paragraph("下划线的实现",
FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18,
Font.UNDERLINE, new Color(0, 0,
255)));

document.add(underline);

// 设置 Table
表格

Table aTable = new
Table(3);

int width[] = { 25, 25, 50
};

aTable.setWidths(width);//
设置每列所占比例

aTable.setWidth(90); // 占页面宽度
90%

aTable.setAlignment(Element.ALIGN_CENTER);//
居中显示

aTable.setAlignment(Element.ALIGN_MIDDLE);//
纵向居中显示

aTable.setAutoFillEmptyCells(true); //
自动填满

aTable.setBorderWidth(1); //
边框宽度

aTable.setBorderColor(new Color(0, 125, 255)); //
边框颜色

aTable.setPadding(2);//
衬距,看效果就知道什么意思了

aTable.setSpacing(3);//
即单元格之间的间距

aTable.setBorder(2);//
边框
//
设置表头Cell haderCell = new
Cell("表格表头");

haderCell.setHeader(true);

haderCell.setColspan(3);

aTable.addCell(haderCell);

aTable.endHeaders();

Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,
Color.GREEN);

Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_TOP);

cell.setBorderColor(new Color(255, 0,
0));

cell.setRowspan(2);

aTable.addCell(cell);

aTable.addCell(new
Cell("#1"));

aTable.addCell(new
Cell("#2"));

aTable.addCell(new
Cell("#3"));

aTable.addCell(new
Cell("#4"));

Cell cell3 = new Cell(new Phrase("一行三列数据",
fontChinese));

cell3.setColspan(3);

cell3.setVerticalAlignment(Element.ALIGN_CENTER);

aTable.addCell(cell3);

document.add(aTable);

document.add(new
Paragraph("\n"));

//
添加图片 Image.getInstance即可以放路径又可以放二进制字节流

Image img =
Image.getInstance("d:\\img01800.jpg");

img.setAbsolutePosition(0,
0);

img.setAlignment(Image.RIGHT);//
设置图片显示位置

img.scaleAbsolute(60, 60);//
直接设定显示尺寸

//
img.scalePercent(50);//表示显示的大小为原尺寸的50%

// img.scalePercent(25,
12);//图像高宽的显示比例

//
img.setRotation(30);//图像旋转一定角度

document.add(img);

document.close();

}public static void main(String[] args)
{

CreateWordDemo word = new
CreateWordDemo();

String file =
"d:/demo1.doc";

try
{

word.createDocContext(file);

} catch (DocumentException e)
{

e.printStackTrace();

} catch (IOException e)
{

e.printStackTrace();

}

}
}

❸ java文字转图片再保存到本地

看 BufferedImage 的例子。

❹ 用java怎么将word文档转成图片格式

使用 java 操作 openoffice 实现格式转换 辛苦了几天。 不敢独享成果

首先,要安装 openoffice (废话- -)
创建JAVA项目的时候 需要至少以下4个包

juh.jar,jurt.jar,ridl.jar,unoil.jar
以下是路径
..\OpenOffice.org 2.3\program\classes

我只实现了我要的功能,就是把一种文件格式转成另一种

Java代码
package testh;

import java.io.*;
import com.sun.star.uno.UnoRuntime;

public class testcls {

public static void readdoc(String paths, String savepaths)
{
File d = new File(paths);
//取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
String pathss = new String("");

//对当前目录下面所有文件进行检索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//取得文件类型
filetype = filename.substring((filename.length() - 3), filename.length());

//判断是否为doc文件
if(filetype.equals("doc"))
{
System.out.println("当前正在检索....");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
String fname=filename.substring(0, (filename.length() - 4));
System.out.println("检索到文件"+fname);
try
{
//指定文件路径和名称
String path = savepaths+fname+".html";
File outfilename = new File(path);

/** *//**
* 检查文件是否存在.
* @throws IOException
*
*/

if (!outfilename.exists()) {
System.err.println("目标路径无同名文件,开始转换");

System.out.print("正在转换文件:"+fname);
Dump(paths,fname,savepaths);

}
else
{
System.out.print("文件已存在,放弃创建,请处理存在文件后再运行...\n");
continue;
}

RandomAccessFile mm = null;

}
catch(Exception e)
{
e.printStackTrace();
}
}

}

}

}

public static void Dump(String FilePath,String FileName,String OutPath)
{
com.sun.star.uno.XComponentContext xContext = null;

try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");

// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();

Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);

com.sun.star.frame.XComponentLoader xCompLoader =
(com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class, oDesktop);

java.io.File sourceFile = new java.io.File(FilePath+FileName+".doc");//读取的文件
StringBuffer sLoadUrl = new StringBuffer("file:///");
sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));

sourceFile = new java.io.File(OutPath+FileName+".html");//输出的文件
System.out.print(OutPath+" "+FileName);
StringBuffer sSaveUrl = new StringBuffer("file:///");
sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));

com.sun.star.beans.PropertyValue[] propertyValue =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Hidden";
propertyValue[0].Value = new Boolean(true);

Object oDocToStore = xCompLoader.loadComponentFromURL(
sLoadUrl.toString(), "_blank", 0, propertyValue );
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );

propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = new Boolean(true);
propertyValue[1] = new com.sun.star.beans.PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "HTML (StarWriter)";//你一定发现了,把这里改成其他参数,可以保存为不同的文件 MS Word 97,writer_pdf_Export
xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );

System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
sSaveUrl + "\"\n");

com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
oDocToStore );

if (xCloseable != null ) {
xCloseable.close(false);
} else
{
com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, oDocToStore );
xComp.dispose();
}
System.out.println("document closed!");
}
catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
}

public static void main(String args[]) throws Exception
{
String paths = new String("c:\\a\\");
String savepaths = new String ("f:\\");
readdoc(paths,savepaths);

}

}

❺ java中文字图片读取保存问题

JTextPane类的对象可以显示文本、图片和超链接等,在创建了一个JTextPane类的纯文本格式对象后,可以向里面填写入文字(英文或汉字等)、插入超链接或图片,但无法通过getText()方法获得其中除文字以外的内容,也即无法保存包含文字、图片和超链接的完全对象内容,而如果一开始创建的是JTextPane类的HTML格式对象,则虽然可以保存文字的样式(颜色和字体、字号等)但无法再通过insertIcon()方法插入、显示图片,更不能直接保存了,考虑到的解决思路:①通过手工在HTML文件中插入<img src="02.gif">语句来实现对图片的保存,可是这样比较不容易实现,需要将图片保存在特定位置,才可以在下次打开JTextPane类对象是显示出图片;②创建JTextPane类的纯文本对象,在插入图片、超链接、设置字体样式或其它任何对JTextPane类的对象进行的格式设置时均记录下设置的位置和内容,并和文本内容一起保存到硬盘文件中,下次读取时,按照记录重新恢复原内容,实现的过程更复杂;③将JTextPane对象通过对象序列化的方式在硬盘中保存为一个文件,下次读取时再反序列化为完整对象。
最后,选择了最好实现的第三种方法,序列化了JTextPane对象,表现出来的效果不错。不过,在实现过程中未考虑程序的运行效率,现在也不太清楚,这样的处理方式是不是比较没有效率哩?

❻ PDF(或OFFICE文件)转高清图片(JAVA编程实现)

这么牛我去去去去去去去去去去去去去去去

❼ java文本文件转化为图片文件怎么弄

文件在计算机中都是以二进制保存的,但系统是以文件头来区分各种文件格式的。

也就是说,仅仅更改后缀名是不行的。


按照你说想的,可以这么来做:

1、读取txt文本的每一行

2、创建BufferedImage图片,然后在图片上画读取到的文本


下面给出示例程序:


测试类 TextToImageExample.java

importjava.io.File;
importjava.util.Scanner;

/**
*文本转图片测试类
*@authorYY29242014/11/18
*@version1.0
*/
publicclassTextToImageExample{
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
System.out.print("输入TXT文本名称(例如:D:/java.txt):");
StringtextFileName=in.nextLine();
System.out.print("输入保存的图片名称(例如:D:/java.jpg):");
StringimageFileName=in.nextLine();

TextToImageconvert=newTextToImage(newFile(textFileName),newFile(imageFileName));
booleansuccess=convert.convert();
System.out.println("文本转图片:"+(success?"成功":"失败"));
}
}


文本转图片类 TextToImage.java

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.IOException;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
importcom.sun.image.codec.jpeg.JPEGCodec;

/**
*文本转图片类
*@authorYY29242014/11/18
*@version1.0
*/
publicclassTextToImage{
/**文本文件*/
privateFiletextFile;
/**图片文件*/
privateFileimageFile;

/**图片*/
privateBufferedImageimage;
/**图片宽度*/
privatefinalintIMAGE_WIDTH=400;
/**图片高度*/
privatefinalintIMAGE_HEIGHT=600;
/**图片类型*/
privatefinalintIMAGE_TYPE=BufferedImage.TYPE_INT_RGB;

/**
*构造函数
*@paramtextFile文本文件
*@paramimageFile图片文件
*/
publicTextToImage(FiletextFile,FileimageFile){
this.textFile=textFile;
this.imageFile=imageFile;
this.image=newBufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,IMAGE_TYPE);
}

/**
*将文本文件里文字,写入到图片中保存
*@returnbooleantrue,写入成功;false,写入失败
*/
publicbooleanconvert(){

//读取文本文件
BufferedReaderreader=null;
try{
reader=newBufferedReader(newFileReader(textFile));
}catch(FileNotFoundExceptione){
e.printStackTrace();
returnfalse;
}

//获取图像上下文
Graphicsg=createGraphics(image);
Stringline;
//图片中文本行高
finalintY_LINEHEIGHT=15;
intlineNum=1;
try{
while((line=reader.readLine())!=null){
g.drawString(line,0,lineNum*Y_LINEHEIGHT);
lineNum++;
}
g.dispose();

//保存为jpg图片
FileOutputStreamfos=newFileOutputStream(imageFile);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
fos.close();
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}

/**
*获取到图像上下文
*@paramimage图片
*@returnGraphics
*/
privateGraphicscreateGraphics(BufferedImageimage){
Graphicsg=image.createGraphics();
g.setColor(Color.WHITE);//设置背景色
g.fillRect(0,0,IMAGE_WIDTH,IMAGE_HEIGHT);//绘制背景
g.setColor(Color.BLACK);//设置前景色
g.setFont(newFont("微软雅黑",Font.PLAIN,12));//设置字体
returng;
}


}

特别注意:程序中使用到了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及OCR开发批量图片内容转文字

可以到云脉OCR SDK开发者平台下载文档识别API接口文档啊,开发支持Java、C++、C、 object pascal及objective-C等多种语言!可以用接入的OCR识别功能轻松实现批量图片内容转文字的操作

❾ java 文字旋转

旋转?怎么个旋转法。。。? 把文字的东西转换成图片然后旋转图片好了。。

阅读全文

与java文字转图片相关的资料

热点内容
美术花盆和花的图片简单 浏览:143
水果怎么摆好看图片 浏览:537
千与千寻人物图片大全 浏览:241
委屈漫画图片女孩 浏览:402
背景图片简约可爱清新 浏览:927
图片狼抱着一个女孩 浏览:170
文档中图片如何快捷设置大小 浏览:255
貂蝉去衣服图片大全 浏览:91
美女背影高清壁纸图片全屏 浏览:59
图片如何设置标题 浏览:807
漂亮文字动态图片大全 浏览:64
七天打卡表可爱图片 浏览:35
波波头发型效果图片 浏览:659
图片插入word中无法选中怎么办 浏览:220
大叔洗衣服的图片 浏览:444
Word里图片置顶如何加空格 浏览:201
蒋丞图片高清动漫 浏览:303
放美女和平精英图片 浏览:386
黑鼻子可爱图片 浏览:854
word转换pdf时图片丢失 浏览:290