微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RLE 图像的功能无法正常工作

如何解决RLE 图像的功能无法正常工作

输入是黑白图像。需要使用 group 函数对其进行压缩以对图像进行编码。相反,我的尺寸反而增加了。感谢您提供的任何帮助:)

 public static void main(String[] args) throws IOException {
        File rleImage=new File("pool.png");
        BufferedImage img=ImageIO.read(rleImage);
        byteArray(img);
        getRunLength();
        try{
            File newImage = new File("Saved.png");
            ImageIO.write(img,"BMP",newImage);
        }catch(Exception e){
            System.out.println("Error");
        }
    }
    public static byte[] byteArray(BufferedImage image){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] imageInByte = null;
        try{
            ImageIO.write(image,baos);
            baos.flush();
            imageInByte = baos.toByteArray();
            baos.close();
        }catch(IOException e){
            System.out.println(e.getMessage());
        }

        return imageInByte;
    }
    public static byte[] getRunLength(){
        ByteArrayOutputStream dest = new ByteArrayOutputStream();
        byte lastByte = imageByteArray[0];
        int matchCount = 1;
        for(int i=1; i < imageByteArray.length; i++){
            byte thisByte = imageByteArray[i];
            if (lastByte == thisByte) {
                matchCount++;
            }
            else {
                dest.write((byte)matchCount);
                dest.write((byte)lastByte);
                matchCount=1;
                lastByte = thisByte;
            }
        }
        dest.write((byte)matchCount);
        dest.write((byte)lastByte);
        return dest.toByteArray();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。