java 图片转txt文本,文本转换图片
来源:互联网 发布:淘宝什么字体是免费的 编辑:程序博客网 时间:2024/06/06 09:51
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Date;import javax.imageio.stream.FileImageInputStream;import javax.imageio.stream.FileImageOutputStream;import com.enation.framework.util.FileUtil;/** * * @User:HuangXinyu * * 创建时间:2014-5-14 下午1:09:16 * * 修改备注: * @version 1.0 * */@SuppressWarnings("all")public class FileTools extends FileUtil {/** * 读取文件 * * @param fileUrl */public static StringBuffer read(String fileUrl) {String s = null;StringBuffer text = new StringBuffer();File f = new File(fileUrl);if (f.exists()) {try {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));while ((s = br.readLine()) != null) {text.append(s);}return text;} catch (Exception e) {e.printStackTrace();}} else {System.out.println("文件不存在!");}return null;}/** * 写文件 * * @param path * @param content */public static void write(String path, String content) {String s = new String();String s1 = new String();try {File f = new File(path);if (f.exists()) {} else {System.out.println("文件不存在,正在创建...");if (f.createNewFile()) {System.out.println("文件创建成功!");} else {System.out.println("文件创建失败!");}}BufferedReader input = new BufferedReader(new FileReader(f));while ((s = input.readLine()) != null) {s1 += s + "\n";}input.close();s1 += content;BufferedWriter output = new BufferedWriter(new FileWriter(f));output.write(s1);output.close();} catch (Exception e) {e.printStackTrace();}}/** * 把图片生成Byte * * @param path * @return */public static byte[] image2byte(String path) {byte[] data = null;FileImageInputStream input = null;try {input = new FileImageInputStream(new File(path));ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buf = new byte[1024];int numBytesRead = 0;while ((numBytesRead = input.read(buf)) != -1) {output.write(buf, 0, numBytesRead);}data = output.toByteArray();output.close();input.close();} catch (FileNotFoundException ex1) {ex1.printStackTrace();} catch (IOException ex1) {ex1.printStackTrace();}return data;}/** * byte数组到图片 * * @param data * @param path */public static void byte2image(byte[] data, String path) {if (data.length < 3 || path.equals(""))return;try {FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));imageOutput.write(data, 0, data.length);imageOutput.close();System.out.println("Make Picture success,Please find image in "+ path);} catch (Exception ex) {System.out.println("Exception: " + ex);ex.printStackTrace();}}/** * 读取文本的Byte转换成图片 * * @param path * 文本路径 * @param fileUrl * 图片保存路径 * @throws Exception */public static void stringToImg(String path, String fileUrl)throws Exception {FileTools to = new FileTools();InputStream is = new FileInputStream(path);InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String str = null;StringBuilder sb = new StringBuilder();while ((str = br.readLine()) != null) {sb.append(str);}to.saveToImgFile(sb.toString().toUpperCase(), fileUrl);}private void saveToImgFile(String src, String output) {if (src == null || src.length() == 0) {return;}try {FileOutputStream out = new FileOutputStream(new File(output));byte[] bytes = src.getBytes();for (int i = 0; i < bytes.length; i += 2) {out.write(charToInt(bytes[i]) * 16 + charToInt(bytes[i + 1]));}out.close();} catch (Exception e) {e.printStackTrace();}}private int charToInt(byte ch) {int val = 0;if (ch >= 0x30 && ch <= 0x39) {val = ch - 0x30;} else if (ch >= 0x41 && ch <= 0x46) {val = ch - 0x41 + 10;}return val;}/** * 图片转String(效率比较快) * * @param contentPath * @param ImgFileUrl */public static void Img2String(String contentPath, String ImgFileUrl){write(contentPath, byteTostring(image2byte(ImgFileUrl)));}/** * 图片转String * * @param fileUrl 文本路径 * @param imgPath 图片路径 */public static void ImgToString(String fileUrl,String imgPath) {try {StringBuffer sb = new StringBuffer();FileInputStream fis = new FileInputStream(imgPath);BufferedInputStream bis = new BufferedInputStream(fis);java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();byte[] buff = new byte[1024];int len = 0;while ((len = fis.read(buff)) != -1) {bos.write(buff, 0, len);}// 得到图片的字节数组byte[] result = bos.toByteArray();//System.out.println("++++" + byte2HexStr(result));// 字节数组转成十六进制String str = byte2HexStr(result);/* * 将十六进制串保存到txt文件中 */PrintWriter pw = new PrintWriter(new FileWriter(fileUrl));pw.println(str);pw.close();} catch (IOException e) {e.printStackTrace();}}/** * 实现字节数组向十六进制的转换方法一 * * @param b * @return */public static String byte2HexStr(byte[] b) {String hs = "";String stmp = "";for (int n = 0; n < b.length; n++) {stmp = (Integer.toHexString(b[n] & 0XFF));if (stmp.length() == 1)hs = hs + "0" + stmp;elsehs = hs + stmp;}return hs.toUpperCase();}private static byte uniteBytes(String src0, String src1) {byte b0 = Byte.decode("0x" + src0).byteValue();b0 = (byte) (b0 << 4);byte b1 = Byte.decode("0x" + src1).byteValue();byte ret = (byte) (b0 | b1);return ret;}/** * 实现字节数组向十六进制的转换的方法二 * * @param src * @return */public static String bytesToHexString(byte[] src) {StringBuilder stringBuilder = new StringBuilder("");if (src == null || src.length <= 0) {return null;}for (int i = 0; i < src.length; i++) {int v = src[i] & 0xFF;String hv = Integer.toHexString(v);if (hv.length() < 2) {stringBuilder.append(0);}stringBuilder.append(hv);}return stringBuilder.toString();}/** * byte数组到16进制字符串 * @param data * @return */public static String byteTostring(byte[] data) {if (data == null || data.length <= 1)return "0x";if (data.length > 200000)return "0x";StringBuffer sb = new StringBuffer();int buf[] = new int[data.length];// byte数组转化成十进制for (int k = 0; k < data.length; k++) {buf[k] = data[k] < 0 ? (data[k] + 256) : (data[k]);}// 十进制转化成十六进制for (int k = 0; k < buf.length; k++) {if (buf[k] < 16)sb.append("0" + Integer.toHexString(buf[k]));elsesb.append(Integer.toHexString(buf[k]));}return sb.toString().toUpperCase();}
0 0
- java 图片转txt文本,文本转换图片
- txt 文本转图片,txt文本缩略图
- 文本转换成图片
- 文本转换为图片
- Matlab将图片转换成asc码txt文本格式
- 图片转换为文本样式
- 将文本转换成图片
- 文本构造文本图片
- C#实现将文本转换为图片
- java读取txt文本
- java 获取文本内容信息图片
- JAVA通过CKEditor文本编辑器上传图片
- Java 上传图片(非文本)
- Java实现图片到txt的转换
- C#实现文本图片
- Android文本环绕图片
- 从网络文本图片
- 富文本图片上传
- android控件的对齐方式(转)
- jdbc dao 注入 dataSource 失败
- 黑马程序员_.java文件的复制与改名
- 深入浅出实例解析linux内核container_of宏
- SQL SERVER 2008行锁与jdbc配置
- java 图片转txt文本,文本转换图片
- 黑马程序员_反射小结
- Web服务请求异步化测试
- ubuntu蓝牙bluez
- python3.4 编译时提示错误 IndentationError: unexpected indent
- Java设计模式之工厂模式(简单工厂、工厂方法、抽象工厂)
- SQL Server 索引相关
- 01模板 使用template<class T>
- Fragment的使用与学习过程中遇到的问题