圖片轉字符串再另存爲圖片

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章