简体   繁体   English

如何使用LZW算法压缩图像,压缩后如何打开查看结果?

[英]How can I use LZW algorithm to compress image, and after compress, how can I open it and see the result?

I'm trying to compress an image using the LZW algorithm using java.我正在尝试使用使用 java 的 LZW 算法压缩图像。

1. The first problem is that the file has grown in size I expected it to decrease instead 1.第一个问题是文件变大了,我预计它会变小

2. The second problem is that after compressing the file how can I open the file resulting from the algorithm I hav the following code: 2.第二个问题是压缩文件后如何打开由算法产生的文件我有以下代码:

public static HashMap<String, Integer> dictionary = new HashMap<>();

public static int dictSize = 256;

public static String P = "",filename="",BP="";
public static byte inputByte;

public static byte[] buffer = new byte[3];

public static boolean isLeft = true;

public static void compress() throws IOException {

    int i,byteToInt;
    char C;
    
    // Character dictionary 
    for(i=0;i<256;i++) {
        dictionary.put(Character.toString((char)i),i);
    }
    
    // Read input file and output file
    RandomAccessFile inputFile = new RandomAccessFile(filename,"r");
    String[] getFileNameWOExtn = filename.split("\\.");
    RandomAccessFile outputFile = new RandomAccessFile(getFileNameWOExtn[0].concat(".lzw"),"rw");
    
    try {
    
        // Read first byte to initialize P
        inputByte = inputFile.readByte();
        byteToInt = new Byte(inputByte).intValue();
        
        if(byteToInt < 0) byteToInt += 256;
        C = (char) byteToInt;
        P = ""+C;
        
        while(true) {
            inputByte = inputFile.readByte();
            byteToInt = new Byte(inputByte).intValue();
        
            if(byteToInt < 0) byteToInt += 256;
            C = (char) byteToInt;
            
            // if P+C is present in dictionary
            if(dictionary.containsKey(P+C)) {
                P = P+C;
            }
            
            else {
                BP = convertTo12Bit(dictionary.get(P));
                if(isLeft) {
                    buffer[0] = (byte) Integer.parseInt(BP.substring(0,8),2);  
                    buffer[1] = (byte) Integer.parseInt(BP.substring(8,12)+"0000",2);                   
                }
                else {
                    buffer[1] += (byte) Integer.parseInt(BP.substring(0,4),2); 
                    buffer[2] = (byte) Integer.parseInt(BP.substring(4,12),2);
                    for(i=0;i<buffer.length;i++) {
                        outputFile.writeByte(buffer[i]);
                        buffer[i]=0;
                    }
                }
                isLeft = !isLeft;
                if(dictSize < 4096) dictionary.put(P+C,dictSize++);
                
                P=""+C;
            }            
        }
    
    }
    catch(IOException ie) {
        BP = convertTo12Bit(dictionary.get(P));
        if(isLeft) {
            buffer[0] = (byte) Integer.parseInt(BP.substring(0,8),2);  
            buffer[1] = (byte) Integer.parseInt(BP.substring(8,12)+"0000",2);
            outputFile.writeByte(buffer[0]);  
            outputFile.writeByte(buffer[1]);                
        }
        else {
            buffer[1] += (byte) Integer.parseInt(BP.substring(0,4),2); 
            buffer[2] = (byte) Integer.parseInt(BP.substring(4,12),2);
            for(i=0;i<buffer.length;i++) {
                 outputFile.writeByte(buffer[i]);
                 buffer[i]=0;
            }
        }
        inputFile.close();
        outputFile.close();        
    }}public static String convertTo12Bit(int i) {
    String to12Bit = Integer.toBinaryString(i);
    while (to12Bit.length() < 12) to12Bit = "0" + to12Bit;
    return to12Bit;}

and in the main method:在主要方法中:

public static void main(String[] args) {
    System.out.println("image site:");
    Scanner sc = new Scanner(System.in);
    filename = sc.next();
    try {
        File file = new File(filename);
        compress();
        String[] getFileNameWOExtn = filename.split("\\.");
        System.out.println("Compression complete!Check file "+getFileNameWOExtn[0].concat(".lzw")+"!");      
    }
    catch(IOException ie) {
        System.out.println("File "+filename+" not found!");
    }
}

The input file is 1.jpg and the output file is 1.lzw like this:输入文件是 1.jpg 而 output 文件是 1.lzw 像这样:

在此处输入图像描述

I'm trying to compress an image using the LZW algorithm using java.我正在尝试使用使用 java 的 LZW 算法压缩图像。

1. The first problem is that the file has grown in size I expected it to decrease instead 1.第一个问题是文件变大了,我预计它会变小

2. The second problem is that after compressing the file how can I open the file resulting from the algorithm I hav the following code: 2.第二个问题是压缩文件后如何打开由算法产生的文件我有以下代码:

public static HashMap<String, Integer> dictionary = new HashMap<>();

public static int dictSize = 256;

public static String P = "",filename="",BP="";
public static byte inputByte;

public static byte[] buffer = new byte[3];

public static boolean isLeft = true;

public static void compress() throws IOException {

    int i,byteToInt;
    char C;
    
    // Character dictionary 
    for(i=0;i<256;i++) {
        dictionary.put(Character.toString((char)i),i);
    }
    
    // Read input file and output file
    RandomAccessFile inputFile = new RandomAccessFile(filename,"r");
    String[] getFileNameWOExtn = filename.split("\\.");
    RandomAccessFile outputFile = new RandomAccessFile(getFileNameWOExtn[0].concat(".lzw"),"rw");
    
    try {
    
        // Read first byte to initialize P
        inputByte = inputFile.readByte();
        byteToInt = new Byte(inputByte).intValue();
        
        if(byteToInt < 0) byteToInt += 256;
        C = (char) byteToInt;
        P = ""+C;
        
        while(true) {
            inputByte = inputFile.readByte();
            byteToInt = new Byte(inputByte).intValue();
        
            if(byteToInt < 0) byteToInt += 256;
            C = (char) byteToInt;
            
            // if P+C is present in dictionary
            if(dictionary.containsKey(P+C)) {
                P = P+C;
            }
            
            else {
                BP = convertTo12Bit(dictionary.get(P));
                if(isLeft) {
                    buffer[0] = (byte) Integer.parseInt(BP.substring(0,8),2);  
                    buffer[1] = (byte) Integer.parseInt(BP.substring(8,12)+"0000",2);                   
                }
                else {
                    buffer[1] += (byte) Integer.parseInt(BP.substring(0,4),2); 
                    buffer[2] = (byte) Integer.parseInt(BP.substring(4,12),2);
                    for(i=0;i<buffer.length;i++) {
                        outputFile.writeByte(buffer[i]);
                        buffer[i]=0;
                    }
                }
                isLeft = !isLeft;
                if(dictSize < 4096) dictionary.put(P+C,dictSize++);
                
                P=""+C;
            }            
        }
    
    }
    catch(IOException ie) {
        BP = convertTo12Bit(dictionary.get(P));
        if(isLeft) {
            buffer[0] = (byte) Integer.parseInt(BP.substring(0,8),2);  
            buffer[1] = (byte) Integer.parseInt(BP.substring(8,12)+"0000",2);
            outputFile.writeByte(buffer[0]);  
            outputFile.writeByte(buffer[1]);                
        }
        else {
            buffer[1] += (byte) Integer.parseInt(BP.substring(0,4),2); 
            buffer[2] = (byte) Integer.parseInt(BP.substring(4,12),2);
            for(i=0;i<buffer.length;i++) {
                 outputFile.writeByte(buffer[i]);
                 buffer[i]=0;
            }
        }
        inputFile.close();
        outputFile.close();        
    }}public static String convertTo12Bit(int i) {
    String to12Bit = Integer.toBinaryString(i);
    while (to12Bit.length() < 12) to12Bit = "0" + to12Bit;
    return to12Bit;}

and in the main method:在主要方法中:

public static void main(String[] args) {
    System.out.println("image site:");
    Scanner sc = new Scanner(System.in);
    filename = sc.next();
    try {
        File file = new File(filename);
        compress();
        String[] getFileNameWOExtn = filename.split("\\.");
        System.out.println("Compression complete!Check file "+getFileNameWOExtn[0].concat(".lzw")+"!");      
    }
    catch(IOException ie) {
        System.out.println("File "+filename+" not found!");
    }
}

The input file is 1.jpg and the output file is 1.lzw like this:输入文件是 1.jpg 而 output 文件是 1.lzw 像这样:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM