1.指令在这里 指令
2.程序代码
int pos[]={128,64,32,16,8,4,2,1};
jint JNICALL
Java_com_szsicod_print_utils_BitmapUtils_parseBitmap(JNIEnv *env, jclass type, jobject
jsrcBitmap, jbyteArray data) {
jbyte *cmd = (*env)->GetByteArrayElements(env, data, NULL);
AndroidBitmapInfo srcInfo, dstInfo;
if (ANDROID_BITMAP_RESULT_SUCCESS != AndroidBitmap_getInfo(env, jsrcBitmap, &srcInfo)
) {
LOGE("get bitmap info failed");
return FAIL;
}
void *srcBuf;
if (ANDROID_BITMAP_RESULT_SUCCESS != AndroidBitmap_lockPixels(env, jsrcBitmap, &srcBuf)) {
LOGE("lock src bitmap failed");
return FAIL;
}
LOGI("width=%d; height=%d; stride=%d; format=%d;flag=%d",
srcInfo.width, // width=2700 (900*3)
srcInfo.height, // height=2025 (675*3)
srcInfo.stride, // stride=10800 (2700*4)
srcInfo.format, // format=1 (ANDROID_BITMAP_FORMAT_RGBA_8888=1)
srcInfo.flags); // flags=0 (ANDROID_BITMAP_RESULT_SUCCESS=0)
int w = srcInfo.width;
int h = srcInfo.height;
int nWidth = (w + 7) / 8 * 8;
cmd[0] = 29;
cmd[1] = 118;
cmd[2] = 48;
cmd[3] = 0;
cmd[4] = (jbyte) (nWidth / 8 % 256);
cmd[5] = (jbyte) (nWidth / 8 / 256);
cmd[6] = (jbyte) (h % 256);
cmd[7] = (jbyte) (h / 256);
int position=0;
if (srcInfo.format==ANDROID_BITMAP_FORMAT_RGBA_8888) {
//如果使用android 方法对图片操作时,配置了 Bitmap.Config
.ARGB_8888
position = getPixelForARGB(cmd, srcBuf, w, h, nWidth);
}else if (srcInfo.format==ANDROID_BITMAP_FORMAT_RGB_565){
//如果使用android 方法对图片操作时,配置了 Bitmap.Config
.ARG_565 要使用这个不然会打印两个图片
position= getPixelForRGB(cmd, srcBuf, w, h, nWidth);
} else{
return ERR_PARAM;
}
AndroidBitmap_unlockPixels(env, jsrcBitmap);
(*env)->ReleaseByteArrayElements(env, data, cmd, 0);
return position;
}
int getPixelForARGB(unsigned char *cmd, const void *srcBuf, int w, int h, int nWidth) {
int32_t *srcPixs = (int32_t *) srcBuf;
int position = 8;
int i, j;
int color;
int r;
int g;
int b;
int index = 0;
int maxSize = w * h;
int rows = nWidth / 8;
for (i = 0; i < h; i++) {
for (j = 0; j < rows; j++) {
// get the color of per pixel
int dots[8];
int dot = 0;
jbyte value = 0;
for (; dot < 8; dot++) {
index = j * 8 + dot + i * w;
if (maxSize <= index || j * 8 + dot >= w) {
value |= 0;
} else {
color = srcPixs[index];
r = ((color & 0x00FF0000) >> 16);
g = ((color & 0x0000FF00) >> 8);
b = color & 0x000000FF;
int gray = (int) (0.29900 * r + 0.58700 * g + 0.11400 * b);
if (gray < 127) {
value |= pos[dot];
}
}
}
cmd[position] = value;
position++;
}
}
return position;
}
int getPixelForRGB(unsigned char *cmd, const void *srcBuf, int w, int h, int nWidth) {
short *srcPixs = (short *) srcBuf;
int position = 8;
int i, j;
short color;
int r;
int g;
int b;
int index = 0;
int maxSize = w * h;
int rows = nWidth / 8;
for (i = 0; i < h; i++) {
for (j = 0; j < rows; j++) {
// get the color of per pixel
int dots[8];
int dot = 0;
jbyte value = 0;
for (; dot < 8; dot++) {
index = j * 8 + dot + i * w;
if (maxSize <= index || j * 8 + dot >= w) {
value |= 0;
} else {
color = srcPixs[index];
r = ((color & 0xF800) >> 8);
g = ((color & 0x7E0) >> 3);
b = (color & 0x0000001F)<<3;
int gray = (int) (0.29900 * r + 0.58700 * g + 0.11400 * b);
if (gray < 127) {
value |= pos[dot];
}
}
}
cmd[position] = value;
position++;
}
}
return position;
}
ps:速度提高了几十毫秒