最近在写b站的弹幕,首先碰到的就是如何将图片显示。
后来发现b站弹幕底层用的是flash的bitmap来将图像显示。大致的流程是
输入一串base64加密的字符串->extract取出所有的pixel->Bitmap.setpixel进行显示
这里直接贴出代码
function extract(data) {
var bmd = Bitmap.createBitmapData(1, 1);
var output = bmd.getPixels(bmd.rect);
output.clear();
var dataBuffer = [];
dataBuffer.length = 4;
var outputBuffer = [];
outputBuffer.length = 3;
for (var i = 0; i < data.length; i += 4) {
for (var j = 0; j < 4 && i + j < data.length; j++) {
dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
}
outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);
outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
for (var k = 0; k < outputBuffer.length; k++) {
if (dataBuffer[k + 1] == 64) break;
output.writeByte(outputBuffer[k]);
}
}
output.position = 0;
return output;
}
上述代码用来将base64字符串生成一个所有图像数据的一维数组。
int main(int argc, const char * argv[]) {
stringstream ss("");
ifstream in("文件位置", ios::in | ios::binary);
if (! in.is_open())
{
cout << "Error opening file";
exit (1);
}
int num[4], cnt = 0, thresh = 0;
int temp, width_cnt = 0;
long length = LONG_MAX - 54;
char buf[10] = "";
long width = 0, height = 0;
while (!in.eof() && cnt < length + 54) {
if (cnt == 18) {
in.read((char *) &width, 4);
in.read((char *) &height, 4);
length = (width + 4 - width % 4) * height * 3;
// cout<<"size:"<<width<<" * "<<height<<endl;
cnt += 8;
continue;
}
if (cnt >= 54) {
thresh = 0;
width_cnt ++;
for (int i = 2; i >= 0; i--) {
in.read((char *)(&num[i+1]), 1);
thresh += num[i+1];
cnt++;
}
if (thresh == 255 * 3) {
num[0] = 0;
} else {
num[0] = 255;
}
for (int i = 0; i < 4; i++) {
sprintf(buf, "%02x", num[i]);
ss<<buf<<" ";
}
if (width_cnt == width) {
if (width_cnt < (width + 4 - width % 4) && width % 4) {
for (int i = 0; i < 4 - width % 4; ++i) {
in.read((char *)(&temp), 1);
}
}
width_cnt = 0;
continue;
}
} else {
in.read((char *)(&temp), 1);
cnt++;
}
}
string result = ss.str();
// reverse(result.begin(), result.end());
cout<<result;
return 0;
}
上述C++代码通过读取bmp将每个像素RGB对应的byte以16进制的方式打印出来。
bmp的格式在18字节处存储了宽高的信息,从第54字节处是所有实际的图像数据。
需要注意的是bmp是4字节对齐的,所以遇到不是4字节对齐的长宽需要额外处理。