Java 对于RLE映像,函数无法正常工作
输入为黑白图像。需要使用group函数对其进行压缩以对图像进行编码。相反,我的尺码反而增加了。感谢您提供的任何帮助:)Java 对于RLE映像,函数无法正常工作,java,image,rle,Java,Image,Rle,输入为黑白图像。需要使用group函数对其进行压缩以对图像进行编码。相反,我的尺码反而增加了。感谢您提供的任何帮助:) publicstaticvoidmain(字符串[]args)引发IOException{ File rleImage=新文件(“pool.png”); BuffereImage img=图像IO.read(rleImage); byteArray(img); getRunLength(); 试一试{ File newImage=新文件(“Saved.png”); ImageI
publicstaticvoidmain(字符串[]args)引发IOException{
File rleImage=新文件(“pool.png”);
BuffereImage img=图像IO.read(rleImage);
byteArray(img);
getRunLength();
试一试{
File newImage=新文件(“Saved.png”);
ImageIO.write(img,“BMP”,newImage);
}捕获(例外e){
System.out.println(“错误”);
}
}
公共静态字节[]字节数组(BuffereImage图像){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]imageInByte=null;
试一试{
图像IO.write(图像“BMP”,baos);
paos.flush();
imageInByte=baos.toByteArray();
baos.close();
}捕获(IOE异常){
System.out.println(e.getMessage());
}
返回图像字节;
}
公共静态字节[]getRunLength(){
ByteArrayOutputStream dest=新建ByteArrayOutputStream();
字节lastByte=imageByteArray[0];
int matchCount=1;
for(int i=1;i
1。您的代码似乎无法编译。你忘了什么吗?2.byteArray
和getRunLength
方法的返回值被忽略。这可能不是你想要的。。。3.您的byteArray
方法返回一个带有头等的BMP文件。。。它可能只返回像素值。4.最后,PNG的带预测器的ZLib压缩比原始的RLE压缩要复杂得多,因此可以预期大小会增加。您应该将其与未压缩像素的大小进行比较。但即便如此,增长也可能发生。
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();
}