图片转字符串再另存为图片
来源:互联网 发布:日文图片翻译软件 编辑:程序博客网 时间:2024/05/22 02:20
http://blog.csdn.net/xxb2008
import javax.swing.*;import javax.swing.text.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.*;class MyDefaultEditorKit extends StyledEditorKit { @Override public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException { char[] buff = new char[4096]; int nch; AttributeSet attr = getInputAttributes(); while ((nch = in.read(buff, 0, buff.length)) != -1) { doc.insertString(pos, new String(buff, 0, nch), attr); pos += nch; } } @Override public void write(Writer out, Document doc, int pos, int len) throws IOException, BadLocationException { if ((pos < 0) || ((pos + len) > doc.getLength())) { throw new BadLocationException("DefaultEditorKit.write", pos); } Segment data = new Segment(); int nleft = len; int offs = pos; while (nleft > 0) { int n = Math.min(nleft, 4096); doc.getText(offs, n, data); out.write(data.array, data.offset, data.count); offs += n; nleft -= n; } out.flush(); }}class MainFrame extends JFrame { JTextPane textPane; JLabel jLabel; JButton button; MainFrame() { setLayout(new BorderLayout()); button = new JButton("写图片"); jLabel = new JLabel(); textPane = new JTextPane(); textPane.setEditorKit(new MyDefaultEditorKit()); JPanel jPanel = new JPanel(); jPanel.add(textPane); //jPanel.add(jLabel); JScrollPane jScrollPane = new JScrollPane(jPanel); add(button, BorderLayout.NORTH); add(jScrollPane, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }}public class ByteToChar { public static void main(String[] args) { try { final MainFrame frame = new MainFrame(); //读图片,放入文本框http://blog.csdn.net/xxb2008 File file = new File("C:\\Users\\feifei\\Desktop\\垃圾\\图片\\1.jpg"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); byte[] imgByte = new byte[(int) file.length()]; bis.read(imgByte); String toStr = BytesToStr(imgByte); frame.textPane.setText(toStr); frame.jLabel.setText(toStr); frame.button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { //写图片,文本框取值 String imgStr = frame.textPane.getText(); // imgStr = frame.jLabel.getText(); File newFile = new File("C:\\Users\\feifei\\Desktop\\垃圾\\图片\\2.jpg"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile)); bos.write(StrToBytes(imgStr)); bos.flush(); bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } }); frame.setSize(400, 300); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public static String BytesToStr(byte[] target) { StringBuffer buf = new StringBuffer(); for (int i = 0, j = target.length; i < j; i++) { buf.append((char) target[i]); } return buf.toString(); } public static byte[] StrToBytes(String str) { byte[] buf = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { buf[i] = (byte) str.charAt(i); } return buf; }}
http://blog.csdn.net/xxb2008
0 0
- 图片转字符串再另存为图片
- 图片转字符串再另存为图片
- 图片另存为js代码
- 图片另存为jpg
- Qt图片另存为功能
- 图片另存为找不到桌面
- Qt打开,保存,另存为图片
- 双击图片弹出图片另存为对话框
- 禁止ie邮件“目标另存为”和“图片另存为”
- base64图片字符串转图片
- 右击鼠标弹出图片另存为对话框
- C# winform 另存为picturebox里的图片
- 禁止图片另存为、禁止右键、禁止打印
- js base64编码格式图片另存为下载
- python抓取word中的图片并另存为
- matlab fig另存为pdf 图片缺失解决方案
- PPT另存为pdf的图片失真问题解决
- word中图片 另存为图片文件的方法
- 教您如何查看MySQL用户权限
- SpringMvc 拦截器的使用
- 深入浅出Coroutine
- mysqldump 导出数据库报错“does not exist when using LOCK TABLES”
- 重载与重写
- 图片转字符串再另存为图片
- glPushMatrix glPopMatrix
- CentOS 6.0配置本地yum源
- Ubuntu10.04中使用minicom
- 安装memcached服务器和php中添加memcache拓展模块
- Android的.9.png制作
- 谷歌技术面试要点(Google面试)(14年5月20日交大专场)
- hadoop jar singlnode下运行的命令行输出
- 5分钟内搞定 Tomcat 的 SSL 配置