java中的霍夫曼编码,图片有问题 [英] huffman coding in java, something wrong for pictures

查看:57
本文介绍了java中的霍夫曼编码,图片有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
下面的代码是我的霍夫曼算法,它适用于文本但是对于图片

它不起作用。

树构建正确,我已经查过了!



解压缩图片时窗口无法显示图片!



什么错了?



hi code below is my huffman algorithm , it works correctly for text but for pictures
it dose not works.
the tree is built correct , i have checked that!

when decompress the pictures windows cant show that picture!

whats wrong ?

public class HopeHuffMan {

  
    public static void main(String[] args) throws IOException {
        
        
       FileInputStream input = new FileInputStream( "f:/testpic.bmp");
         
        
        String text=Change_input_to_string_Text(input);
        
             
   int[] counts = getCharacterFrequency(text); // Count frequency
     
   
    Tree tree = getHuffmanTree(counts); // Create a Huffman tree
      String[] codes = getCode(tree.root); // Get codes

                        
                          int k1=text.length();
                          Compress(text,codes);
                         Decompress (k1,tree);
                  
    }
    
       


    
     private static String Change_input_to_string_Text(FileInputStream input1) throws IOException{                
                   int ch;
                StringBuilder strContent = new StringBuilder("");
                                 
                 while((ch = input1.read()) != -1)
                  strContent.append((char) ch);
                 input1.close();
                String text = strContent.toString();
               
              
               
     return text;      
    }
    
 
    private static int[] getCharacterFrequency(String text) {
        int[] counts = new int[256]; // 256 ASCII characters
    
    for (int i = 0; i < text.length(); i++)
      counts[(int)text.charAt(i)]++; // Count the character in text
    
    return counts;
    }
    
    
    
      private static Tree getHuffmanTree(int[] counts) {
        // Create a heap to hold trees
    Heap<Tree> heap = new Heap<Tree>(); // Defined in Listing 24.10
    for (int i = 0; i < counts.length; i++) {
      if (counts[i] > 0)
        heap.add(new Tree(counts[i], (char)i)); // A leaf node tree
    }
    
    while (heap.getSize() > 1) { 
      Tree t1 = heap.remove(); // Remove the smallest weight tree
      Tree t2 = heap.remove(); // Remove the next smallest weight 
      heap.add(new Tree(t1, t2)); // Combine two trees
    }

    return heap.remove(); // The final tree
    }
    
        private static String[] getCode(Node root) {
         if (root == null) return null;    
    String[] codes = new String[2 * 128];
    assignCode(root, codes);
    return codes;
    }

    
   
    
    private static void assignCode(Node root, String[] codes) {
        if (root.left != null) {
      root.left.code = root.code + "0";
      assignCode(root.left, codes);
      
      root.right.code = root.code + "1";
      assignCode(root.right, codes);
    }
    else {
      codes[(int)root.element] = root.code;
    }
    }
  

       
    
    private static int Compress (String text,String[] codes){
                           String s="";
                        char cd = 0;
                     for(int i=0;i<text.length();++i){
                         cd=text.charAt(i);
                         s=s+codes[cd];
                     }
                     byte [] bytes;
                     bytes=s.getBytes();
                        for(int i=0;i<bytes.length;++i){
                         if(bytes[i]==48) bytes[i]=0;
                         if(bytes[i]==49) bytes[i]=1;
                     }
                      
                     BitOutputStream fop2 = new BitOutputStream("f:/is.oli");
                       for (int i = 0; i < bytes.length; i++) {
                       fop2.writeBit(bytes[i]);
            }
               fop2.close();
               return bytes.length;  
    }
           
    
    

    
    private static void Decompress (int b,Tree t) throws FileNotFoundException, IOException{
        
        BufferedWriter out = new BufferedWriter(new FileWriter("f:/unziped.txt"));
                   
                 BitInputStream fop2 = new BitInputStream("f:/is.oli");
                 System.out.print("\n");
               for (int i = 0;i<b ; i++) {
                        Node x = t.root;
                    while (!x.isLeaf()) {
                    int bit=fop2.readBit();
                  if (bit==1) x = x.right;
                  if (bit==0) x = x.left; 
                }
                       out.write(x.element);
                     
                      
                       }
                 System.out.print("\n");
                   out.close();      
                   fop2.close();
                                  
    }
       
    
    
}

推荐答案

您好。



我认为问题是当您将位图传递给Change_input_to_string_Text函数时,您希望返回的文本在ASCII范围256中,但我的猜测可能不是这样。



您是否尝试过从函数中打印出返回的文本值?检查是否返回ASCII文本,范围为0-256。如果情况并非如此,则需要相应地修改您的霍夫曼算法。
Hi there.

I think the problem is when you pass in a bitmap to the "Change_input_to_string_Text" function you are expecting return text that is in the ASCII range of 256, however my guess is this may not be the case.

Have you tried printing out the returned text value from the function? Check to see if ASCII text is being returned and in the range from 0-256. If that''s not the case you will need to modify your Huffman algorithm accordingly.


这篇关于java中的霍夫曼编码,图片有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆