import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.util.Map.Entry;
import java.util.*;
public class Huffman_coding{
String allCode ="";
public int[] readImage(String imageUrl) throws Exception{
int[] rgb = new int[3];
File file = new File(imageUrl);
BufferedImage image = ImageIO.read(file);
try {
image = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = image.getWidth();
int height = image.getHeight();
int[] pixelValue = new int[width*height];
int sum = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = image.getRGB(i, j);
pixelValue[sum] = (pixel & 0xff0000) >> 16;
rgb[0] = (pixel & 0xff0000) >> 16;
sum = sum + 1;
}
}
return pixelValue;
}
public List<Node> copyList(List<Node> list){
List<Node> newlist = new ArrayList<Node>();
for (Node node:list){
newlist.add(node);
}
return newlist;
}
public Node creatHuffmanTree(List<Node> nodeList){
List<Node> list = copyList(nodeList);
while(list.size() > 1){
list.sort(Comparator.naturalOrder());
Node leftNode = list.get(0);
list.remove(list.get(0));
Node rightNode = list.get(0);
list.remove(list.get(0));
Node father = new Node(leftNode.value+rightNode.value);
father.left = leftNode;
father.right = rightNode;
leftNode.father = father;
rightNode.father = father;
list.add(father);
}
System.out.println(list);
return list.get(0);
}
///
public List<Node> creatNodes(List<Map.Entry<Integer,Integer>> list){
List<Node> nodelist = new ArrayList<Node>();
for (Map.Entry<Integer, Integer> mapping : list) {
nodelist.add(new Node(mapping.getKey(),mapping.getValue()));
}
return nodelist;
}
///
public List<Map.Entry<Integer,Integer>> caculateRate(int[] pixelValue){
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int values= 0;
for(int i = 0; i < pixelValue.length; i++){
if (!map.containsKey(pixelValue[i])){
map.put(pixelValue[i],1);
}
else{
values = map.get(pixelValue[i]);
map.put(pixelValue[i],values+1);
}
}
List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
return list;
}
public Map<Integer,String> toEncoding(List<Node> list,Node root,int[] pixelValue){
Map<Integer,String> map = new HashMap<Integer,String>();
for(Node node: list){
String code = "";
Node node_Now = node;
while(node_Now != root){
if(node_Now.isLeft()){
code = code + "0";
}
else
code = code + "1";
node_Now = node_Now.father;
}
code = invertString(code);
map.put(node.key,code);
}
for(int i=0;i<pixelValue.length;i++){
allCode = allCode + map.get(pixelValue[i]);
}
System.out.println("编码后大小:"+allCode.length()+" bit");
System.out.println("编码:"+allCode);
return map;
}
public int[] decoding(int[] pixelValue,Map<Integer,String> dictionary ){
String coding = "";
int j = 0;
int[] decodingPixelValue = new int[pixelValue.length];
for(int i = 0;i<allCode.length();i++){
coding = coding + allCode.charAt(i);
for (Map.Entry<Integer, String> entry : dictionary.entrySet()) {
if(coding.equals(entry.getValue())){
decodingPixelValue[j] = entry.getKey();
j = j+1;
coding = "";
}
}
}
return decodingPixelValue;
}
public String invertString(String s){
String s1 = "";
for(int i = s.length()-1;i>=0;i--){
s1 = s1+s.charAt(i);
}
return s1;
}
///
public static void main(String[] args) throws Exception{
Map map = new HashMap();
Map dictionary = new HashMap();
List list = new ArrayList();
List<Node> nodeList = new ArrayList<Node>();
String url = "pig.bmp";
Huffman_coding hc = new Huffman_coding();
int[] pixelValue = hc.readImage(url);
list = hc.caculateRate(pixelValue);
nodeList = hc.creatNodes(list);
Node root = hc.creatHuffmanTree(nodeList);
dictionary = hc.toEncoding(nodeList,root,pixelValue);
int[] decodingPixelValue = hc.decoding(pixelValue,dictionary);
System.out.println("字典:"+dictionary);
System.out.println("像素值:"+Arrays.toString(pixelValue));
System.out.println("解码后的像素值:"+Arrays.toString(decodingPixelValue));
}
}
评论1