① 用java怎麼讀取圖片
思路:使用 java.awt.Image包下的Image可以接收圖片。讀取則使用ImageIO對象。
代碼如下:
/**
* 讀取圖片,首先導入以下的包
*/
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
/**
* 用Image對象來接收圖片
* 路徑根據實際情況修改
*/
Image image = ImageIO.read(new File("c:\\1.png"));
System.out.println(image.getSource());
② 使用java如何實現從圖片中讀取圖片中的文本信息呀
http://blog.csdn.net/problc/article/details/5800093 網上找的,希望是你想要的
③ java 實現圖片的文字識別
摘要圖像識別是目前很熱門的研究領域,涉及的知識很廣,包括資訊理論、模式識別、模糊數學、圖像編碼、內容分類等等。本文僅對使用Java實現了一個簡單的圖像文本二值處理,關於識別並未實現。
步驟
建立文本字元模板二值矩陣
對測試字元進行二值矩陣化處理
代碼
/*
* @(#)StdModelRepository.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package cn.e.ynu.sei.recognition.util;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;/** * Hold character charImgs as standard model repository.
* @author 88250
* @version 1.0.0.0, Mar 20, 2008
*/
public class StdModelRepository {
/** * hold character images
*/ List charImgs = new ArrayList();
/** * default width of a character
*/ static int width = 16 /** * default height of a character
*/ static int height = 28 /** * standard character model matrix
*/ public int[][][] stdCharMatrix = new int[27][width][height];
/** * Default constructor.
*/ public StdModelRepository() {
BufferedImage lowercase = null try {
lowercase = ImageIO.read(new File("lowercase.png"));
} catch (IOException ex) {
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE, null, ex);
}
for (int i = 0 i < 26 i++) {
charImgs.add(lowercase.getSubimage(i * width,
0,
width,
height));
}
for (int i = 0 i < charImgs.size(); i++) {
Image image = charImgs.get(i);
int[] pixels = ImageUtils.getPixels(image,
image.getWidth(null),
image.getHeight(null));
stdCharMatrix[i] = ImageUtils.getSymbolMatrix(pixels, 0).clone();
ImageUtils.displayMatrix(stdCharMatrix[i]);
}
}
}
/*
* @(#)ImageUtils.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package cn.e.ynu.sei.recognition.util;import java.awt.Image;import java.awt.image.PixelGrabber;import java.util.logging.Level;import java.util.logging.Logger;/** * Mainipulation of image data.
* @author 88250
* @version 1.0.0.3, Mar 20, 2008
*/
public class ImageUtils {
/** * Return all of the pixel values of sepecified <code>image< .>* @param image the sepecified image
* @param width width of the image
* @param height height of the image
* @return */ public static int[] getPixels(Image image, int width, int height) {
int[] pixels = new int[width * height];
try {
new PixelGrabber(image, 0, 0, width, height, pixels, 0, width).grabPixels();
} catch (InterruptedException ex) {
Logger.getLogger(ImageUtils.class.getName()).
log(Level.SEVERE, null, ex);
}
return pixels;
}
資源來自:
http://blog.csdn.net/chief1985/article/details/2229572
④ java 如何讀取附加到圖片上的文字
圖片上的文字是沒法讀取的,以為這涉及到圖像處理。非常非常復雜!因為如果你非要讀取圖片上的文字,不是幾行代碼可以搞定的,首相從matlaB開始學,了解什麼是圖像處理。然後再開發相應的jar包。當然,你也可以使用相關的軟體工具,比如識圖軟體,通過讀取軟體的反饋也算是讀取了圖片上的文字
⑤ java有讀取圖片裡面文字的方法嗎
使用tesseract-ocr,可以識別簡單的驗證碼,中文沒嘗試過
⑥ java 中用poi讀取word和用docx4j讀取word
不知道你是具體讀取Word裡面的什麼元素,下面以讀取文字和圖片為例吧,兩個代碼示例,你參考看看:
讀取文本
import com.spire.doc.Document;
import java.io.FileWriter;
import java.io.IOException;
public class ExtractText {
public static void main(String[] args) throws IOException {
//載入Word文檔
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//獲取文檔中的文本保存為String
String text=document.getText();
//將String寫入Txt文件
writeStringToTxt(text,"ExtractedText.txt");
}
public static void writeStringToTxt(String content, String txtFileName) throws IOException {
FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}}
2. 讀取圖片
import com.spire.doc.Document;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.interfaces.ICompositeObject;
import com.spire.doc.interfaces.IDocumentObject;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class ExtractImages {
public static void main(String[] args) throws IOException {
//載入Word文檔
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");
//創建Queue對象
Queue nodes = new LinkedList();
nodes.add(document);
//創建List對象
List images = new ArrayList();
//遍歷文檔中的子對象
while (nodes.size() > 0) {
ICompositeObject node = nodes.poll();
for (int i = 0; i < node.getChildObjects().getCount(); i++) {
IDocumentObject child = node.getChildObjects().get(i);
if (child instanceof ICompositeObject) {
nodes.add((ICompositeObject) child);
//獲取圖片並添加到List
if (child.getDocumentObjectType() == DocumentObjectType.Picture) {
DocPicture picture = (DocPicture) child;
images.add(picture.getImage());
}
}
}
}
//將圖片保存為PNG格式文件
for (int i = 0; i < images.size(); i++) {
File file = new File(String.format("output/圖片-%d.png", i));
ImageIO.write(images.get(i), "PNG", file);
}
}
}
注意這里使用的jar包是spire.doc.jar,需要在java程序中先導入jar文件。
⑦ java中文字圖片讀取保存問題
如果你的文本區中不只包含圖片信息,而且還包含文本信息,就不好保存了。下面的代碼沒有實現這個功能。它只是可以打開圖片或文本文件。保存圖片或文本文件。
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class ImageTest extends JFrame {
private JFileChooser chooser = new JFileChooser(".");
private FileInputStream readfile;
private JTextPane jTextPane1 = new JTextPane();
private JScrollPane jsp = new JScrollPane(jTextPane1);
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem open = new JMenuItem("open");
private JMenuItem save = new JMenuItem("save");
// 只有部分擴展名的文件是可以顯示的。
private String s = "jpg,bmp,tga,vst,pcd,pct,gif,ai,fpx,img,cal,wi,png";
private ImageIcon icon;
private File file;
public ImageTest() {
this.setSize(800, 600);
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
fileMenu.add(open);
fileMenu.add(save);
this.add(jsp);
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3_actionPerformed(evt);
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4_actionPerformed(evt);
}
});
}
public static void main(String args[]) throws Exception {
ImageTest test = new ImageTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
public void jButton3_actionPerformed(ActionEvent e) {
int state = chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
if (file != null && state == JFileChooser.APPROVE_OPTION) {
try {
String fileName = file.getName();
jTextPane1.setText(null);
if(s.indexOf(fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase()) != -1) {
icon = new ImageIcon(file.getAbsolutePath());
jTextPane1.insertIcon(icon);
} else {
readfile = new FileInputStream(file);
jTextPane1.read(readfile, file.getName());
}
setTitle(chooser.getSelectedFile().getName());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
//文件保存
public void jButton4_actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String savefile = fc.getSelectedFile().getPath();
if (savefile == null) {
return;
} else {
if(icon != null) {
try {
FileOutputStream out = new FileOutputStream(savefile);
out.write(getBytesFromFile(file));
out.close();
} catch(Exception ex) {
}
} else {
String docToSave = jTextPane1.getText();
if (docToSave != null) {
ObjectOutputStream fstrm = null;
BufferedOutputStream ostrm = null;
try {
fstrm = new ObjectOutputStream(new FileOutputStream(savefile));
ostrm = new BufferedOutputStream(fstrm);
byte[] bytes = null;
try {
bytes = docToSave.getBytes();
} catch (Exception e1) {
e1.printStackTrace();
}
ostrm.write(bytes);
} catch (IOException io) {
System.err.println("IOException: " +
io.getMessage());
} finally {
try {
ostrm.flush();
fstrm.close();
ostrm.close();
} catch (IOException ioe) {
System.err.println("IOException: " +
ioe.getMessage());
}
}
}
}
}
} else {
return;
}
}
public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
}
}