输入是黑白图像。需要使用 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, "BMP", 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();
}