ハフマン符号化と逆符号化の実現javaソースコード
ハフマン符号化と逆符号化の実現 JAvaソースダウンロードアドレス:http://download.csdn.net/source/2357457
public class Huffman {
public static void main(String[] args) {
new HaffmanFrame();
}
}
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//
public class HaffmanFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1808617924849665897L;
private JButton choice, ok, reset,recoding,re;//choice ,ok ,reset ,recoding
private JLabel choiceJL,codingJL,recodingJL;//
private JTextArea inputJTA,recodingJTA;
private JScrollPane js1,js2,js3;
private HaffmanJTable table ;
private TotalChars tc = null;
private HffmanCoding hfc = null;
HaffmanFrame(){
super(" ");
newFrame();//
formeValidate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO
if(e.getSource() == choice) {
open();
}
else if(e.getSource() == ok) {
ok();
}
else if(e.getSource() == reset) {
reset();
}
else if(e.getSource() == recoding) {
if(tc != null )
{
JOptionPane.showMessageDialog(this, hfc.reCoding(recodingJTA.getText()), " ",
JOptionPane.DEFAULT_OPTION);
}
}
else if(e.getSource() == re) {
recodingJTA.setText(null);
}
}
//
private void reset() {
inputJTA.setText("");
recodingJTA.setText("");
table.beginTable();
table.repaint();
}
//
private void ok() {
table.beginTable();
table.repaint();
tc = new TotalChars();
int chars[][] = tc.getCharAddWeight(inputJTA.getText());
hfc = new HffmanCoding(chars);
hfc.coding();//
String s[] = hfc.CreateHCode();//
for (int i = 0; i < chars.length; i++) {
if (chars[i][0]<= 32)
table.setT(i, 0, "A" + String.valueOf(chars[i][0]));// ascll
else
table.setT(i, 0, String.valueOf((char) chars[i][0]));//
table.setT(i, 1, String.valueOf(chars[i][1]));
table.setT(i, 2, s[i]);
}
recodingJTA.setText(hfc.show(inputJTA.getText()));
table.repaint();
}
//
private void open() {
inputJTA.setText("");
FileOpen fo = new FileOpen(this);
fo.open();
inputJTA.setText(fo.getText());
fo = null;
}
//
private void newFrame() {
table = new HaffmanJTable();
choice = new JButton(" ");
ok = new JButton(" ");
reset = new JButton(" ");
recoding = new JButton(" ");
re = new JButton(" ");
choiceJL = new JLabel(" :");
codingJL = new JLabel(" :");
recodingJL = new JLabel(" :");
inputJTA = new JTextArea(5,5);
recodingJTA = new JTextArea(5,5);
recodingJTA.setLineWrap(true);
js1 = new JScrollPane(inputJTA);
js2 = new JScrollPane(table.myTable());
js3 = new JScrollPane(recodingJTA);
//
choiceJL.setBounds(10, 10, 150, 30);
js1.setBounds(160, 10, 320, 150);
ok.setBounds(200, 170, 80, 30);
reset.setBounds(350, 170, 80, 30);
choice.setBounds(30,50,100,30);
codingJL.setBounds(10, 210, 150, 30);
js2.setBounds(160, 210, 320, 150);
recodingJL.setBounds(10, 370, 150, 30);
js3.setBounds(160, 370, 320, 150);
recoding.setBounds(30, 410, 100, 30);
re.setBounds(30, 450, 100, 30);
this.setLayout(null);
this.add(choiceJL);
this.add(js1);
this.add(ok);
ok.addActionListener(this);
this.add(reset);
reset.addActionListener(this);
this.add(choice);
choice.addActionListener(this);
this.add(re);
re.addActionListener(this);
this.add(codingJL);
this.add(js2);
this.add(recodingJL);
this.add(js3);
this.add(recoding);
recoding.addActionListener(this);
Font f = new Font(" ",Font.BOLD,15);//
inputJTA.setFont(f);
recodingJTA.setFont(f);
}
private void formeValidate() {
Toolkit tk = getToolkit();//
Dimension dim = tk.getScreenSize();
this.setResizable(false);
this.setBounds(dim.width/2-250, dim.height/2-280,500,570);
setVisible(true);
validate();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class HaffmanJTable {
private JTable table;
private Object t[][] = new Object[100][3];//
private Object name[] = {" "," "," "};//
public Component myTable() {
// TODO Auto-generated method stub
table = new JTable(t,name);
Font f = new Font(" ",Font.BOLD,15);//
table.setFont(f);
setColumnSize(0,40);// 1
setColumnSize(1,60);// 2
setColumnSize(2,202);// 3
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//AUTO_RESIZE_OFF
// ; 。
//table.setAutoResizeMode() , 。
return table;
}
public void setT(int i ,int j, String volue) {//
this.t[i][j] = volue;
}
public void setColumnSize(int i, int width ) {
TableColumnModel cm = table.getColumnModel(); //
TableColumn column = cm.getColumn(i);// i
column.setPreferredWidth(width);// preferredWidth。
// preferredWidth , 。
}
public void repaint() {//
table.repaint();
}
public void beginTable()//
{
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 3; j++)
setT(i, j, "");
}
}
}
//
import java.awt.FileDialog;
import java.awt.event.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
public class FileOpen {
private FileDialog filedialog_open;
private String fileopen = null, filename = null;//
private File file1; //
private FileReader file_reader;//
private BufferedReader in;//
private StringBuffer text = new StringBuffer();
HaffmanFrame haffman= null;
FileOpen(HaffmanFrame hf) {
haffman = hf;
filedialog_open = new FileDialog(haffman, " ", FileDialog.LOAD);
//
filedialog_open.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
filedialog_open.setVisible(false);
}
});
}
public void open() {
String s = "";
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();//
filename = filedialog_open.getFile();//
// NULL
if (filename != null)//
{
try {
file1 = new File(fileopen,filename );
file_reader = new FileReader(file1);
in = new BufferedReader(file_reader);//
while ((s = in.readLine()) != null)
text.append(s + '/n');
in.close();
file_reader.close();
} catch (IOException e2) {
System.out.println(" !");
}
}
}
//
public String getText() {
return new String(text);
}
}
//
public class TotalChars {
private int charCount[] = new int[255];
private char c[];//
private int charAddWeight[][];// ascll
private int total = 0;//
TotalChars(){
for(int i = 0; i<255;i++)
charCount[i] = 0;
}
//
public void charToal(String s) {
c = new char[s.length()];
c = s.toCharArray();//
for (int i = 0; i < c.length; i++) {
int j = c[i];
charCount[j]++;
}
for(int i = 0; i < 255 ; i++) {
if (charCount[i] != 0) {
total++;//
}
}
}
// ascll
public int[][] getCharAddWeight(String s){
charToal(s);//
charAddWeight = new int[total][2];
for(int i = 0,j=0; i < 255 ; i++) {
if (charCount[i] != 0) {
charAddWeight[j][0] = i;
charAddWeight[j++][1] = charCount[i];
}
}
return charAddWeight;
}
}
//
public class HffmanCoding {
private int charsAndWeight[][];// [][0] ,[][1] ( )
private int hfmcoding[][];//
private int i = 0;//
private String hcs[];
public HffmanCoding(int[][] chars) {
// TODO
charsAndWeight = new int[chars.length][2];
charsAndWeight = chars;
hfmcoding = new int[2 * chars.length - 1][4];//
}
//
public void coding() {
int n = charsAndWeight.length;
if (n == 0)
return;
int m = 2 * n - 1;
//
for (i = 0; i < n; i++) {
hfmcoding[i][0] = charsAndWeight[i][1];//
hfmcoding[i][1] = 0;//
hfmcoding[i][2] = 0;//
hfmcoding[i][3] = 0;//
}
for (i = n; i < m; i++) {
hfmcoding[i][0] = 0;//
hfmcoding[i][1] = 0;//
hfmcoding[i][2] = 0;//
hfmcoding[i][3] = 0;//
}
//
for (i = n; i < m; i++) {
int s1[] = select(i);// weight
hfmcoding[s1[0]][1] = i;//
hfmcoding[s1[1]][1] = i;
hfmcoding[i][2] = s1[0];//
hfmcoding[i][3] = s1[1];//
hfmcoding[i][0] = hfmcoding[s1[0]][0] + hfmcoding[s1[1]][0];//
}
}
// weight
private int[] select(int w) {
// TODO Auto-generated method stub
int s[] = { -1, -1 }, j = 0;// s1 , i
int min1 = 32767, min2 = 32767;
for (j = 0; j < w; j++) {
if (hfmcoding[j][1] == 0) {// ( )
if (hfmcoding[j][0] < min1) {
min2 = min1;
s[1] = s[0];
min1 = hfmcoding[j][0];
s[0] = j;
} else if (hfmcoding[j][0] < min2) {
min2 = hfmcoding[j][0];
s[1] = j;
}
}
}
return s;
}
public String[] CreateHCode() {//
int n = charsAndWeight.length;
int i, f, c;
String hcodeString = "";
hcs = new String[n];
for (i = 0; i < n; i++) {//
c = i;
hcodeString = "";
f = hfmcoding[i][1]; // f
while (f != 0) {//
if (hfmcoding[f][2] == c) {//
hcodeString += "0";
} else {
hcodeString += "1";
}
c = f;
f = hfmcoding[f][1];
}
hcs[i] = new String(new StringBuffer(hcodeString).reverse());
}
return hcs;
}
public String show(String s) {//
String textString = "";
char c[];
int k = -1;
c = new char[s.length()];
c = s.toCharArray();//
for (int i = 0; i < c.length; i++) {
k = c[i];
for (int j = 0; j < charsAndWeight.length; j++)
if (k == charsAndWeight[j][0])
textString += hcs[j];
}
return textString;
}
//
public String reCoding(String s) {
String text = "";//
int k = 0, m = hfmcoding.length - 1;//
char c[];
c = new char[s.length()];
c = s.toCharArray();
k = m;
for (int i = 0; i < c.length; i++) {
if (c[i] == '0') {
k = hfmcoding[k][2];// k
if (hfmcoding[k][2] == 0 && hfmcoding[k][3] == 0)// , ( )
{
text += (char) charsAndWeight[k][0];
k = m;
}
}
if (c[i] == '1') {
k = hfmcoding[k][3];// k
if (hfmcoding[k][2] == 0 && hfmcoding[k][3] == 0)// , ( )
{
text += (char) charsAndWeight[k][0];
k = m;
}
}
}
return text;
}
}