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 文字旋轉
旋轉?怎麼個旋轉法。。。? 把文字的東西轉換成圖片然後旋轉圖片好了。。