学习ruby,尝试用ruby读取png的基本信息。
网上找到的开源的库chunky_png, 下载地址: https://github.com/wvanbergen/chunky_png。
chunky_png 是一个很强大的库,可以修改png图片的数据。
本小例子只是读取一些基本的信息。
require "chunky_png"
class PngImageInfo
def initialize()
@image = nil;
end
# 加载png图片
def setImagePath(path)
@image = ChunkyPNG::Image.from_file(path);
end
#获取png数据
def getImage
@image;
end
#获取图片的大小
def getImageSize
{"width"=>@image.dimension.width, "height"=>@image.dimension.height};
end
#获取图片的在x,y出的像素值
def getImagePixelAtPoint(x, y)
@image.get_pixel(x, y);
end
def getImageColorRAt(x, y)
@image.get_pixel(x, y) >> 24
end
def getImageColorGAt(x, y)
@image.get_pixel(x, y) >> 16 & 0xFF
end
def getImageColorBAt(x, y)
@image.get_pixel(x, y) >> 8 & 0xFF
end
#获取图片在x,y出的alpha值
def getImageColorAlphaAt(x, y)
@image.get_pixel(x, y) & 0xFF
end
end
texture = PngImageInfo.new();
texture.setImagePath("soldier.png");
puts texture.getImageSize;
puts texture.getImagePixelAtPoint(287,133);
puts texture.getImageColorRAt(287,133);
puts texture.getImageColorGAt(287,133);
puts texture.getImageColorBAt(287,133);
puts texture.getImageColorAlphaAt(287,133);
puts texture.getImageColorAlphaAt(374,250);