① 用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;
}
}