赞
踩
JS BMP二进制图片的处理
bmpDeal(arrybuffer){ let w = 1280; let h = 960; let color = 0; let size = w * h; let allsize = size + 1080; let patte = new Array(1024); let end = new Array(2); let pos = 0; let idx = 0; let line = 0; let head = [ 0x42, 0x4d, 0x30, 0x0C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x04, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0xF5, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x0b, 0x01, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]; //位图信息头处理 head[2] = allsize & 0xFF; head[3] = (allsize >> 8) & 0xFF; head[4] = (allsize >> 16) & 0xFF; head[5] = (allsize >> 24) & 0xFF; head[18] = w & 0xFF; head[19] = (w >> 8) & 0xFF; head[20] = (w >> 16) & 0xFF; head[21] = (w >> 24) & 0xFF; head[22] = h & 0xFF; head[23] = (h >> 8) & 0xFF; head[24] = (h >> 16) & 0xFF; head[25] = (h >> 24) & 0xFF; allsize -= 1078; head[34] = allsize & 0xFF; head[35] = (allsize >> 8) & 0xFF; head[36] = (allsize >> 16) & 0xFF; head[37] = (allsize >> 24) & 0xFF; //调色板处理 for (idx = 0; idx < 1024; idx += 4){ patte[pos++] = color; patte[pos++] = color; patte[pos++] = color; patte[pos++] = 0; color++; } allsize += 1078; let all_un8 = new Uint8Array(allsize); //位图信息头 let head_un8 = new Uint8Array(head); for (let i = 0; i < head_un8.length; i++) { all_un8[i] = head_un8[i]; } //颜色表 let patte_un8 = new Uint8Array(patte); for (let i = 0; i < patte_un8.length; i++) { all_un8[54 + i] = patte_un8[i]; } //位图数据 let rgb_un8 = new Uint8Array(arrybuffer); for (let idx = h - 1; idx >= 0; idx--){ //这里注意 BMP二进制的处理是逆序行读的 let sub_rgb_un8 = rgb_un8.subarray(idx * w, (idx+1)*w); for(let i = 0; i < sub_rgb_un8.length; i++){ all_un8[1078 + line * w + i] = sub_rgb_un8[i] } line ++ } //尾部处理 let end_un8 = new Uint8Array(end); all_un8[allsize-2] = end_un8[0]; all_un8[allsize-1] = end_un8[1]; //也可以通过set方式写入 //all_un8.set(new Uint8Array(head)); //all_un8.set(new Uint8Array(patte), 54); //for (let idx = h - 1; idx >= 0; idx--){ // all_un8.set(rgb_un8.subarray(idx * w, (idx+1)*w), 1078 + line * w); // line ++ //} //all_un8.set(new Uint8Array(end),allsize-2); return all_un8; },
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。