java中的霍夫曼编码,图片有问题

编程入门 行业动态 更新时间:2024-10-24 17:28:56
本文介绍了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中的霍夫曼编码,图片有问题

本文发布于:2023-11-26 20:05:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:霍夫曼   图片   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!