6.22 android计算字符高度宽度,红蓝3D图片的制作原理及NDK生成实现

1 篇文章 0 订阅

http://my.oschina.net/jingshishengxu/blog/36412

长度:

方法1:Paint.getTextWidths(text, widths)

方法2:Rect rect = new Rect();              

pFont.getTextBounds(text, 0, text.length(), rect);

width=rect.width();

高度:

m_txtPaint.getFontMetrics().descent - m_txtPaint.getFontMetrics().ascent


http://www.cnblogs.com/error404/archive/2013/01/25/2876888.html

Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("豆", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());


红蓝3d图片制作原理:

http://blog.csdn.net/iamsheldon/article/details/8011688

人眼分左右,红蓝3D图片实际是两种不同角度拍摄的两种图片合成的。


ps制作红蓝3d图片

http://jingyan.baidu.com/article/cbcede072c268c02f40b4d26.html

如何制作?:

http://www.scec.org/geowall/makeanaglyph.html

左边图片只提取红色,右边图片提取绿、蓝色,左图片左移一段距离,重叠上右图,最终合成红蓝3D图片。


上文描述了原理,下面是NDK合成红蓝3D图片的代码:

/**
红蓝图片合成
左边图片,自动取红色
右边图片,自动取绿蓝色
目标生成图片,必须和右边图片尺寸一样
两图片间的左右距离
by touch_ping
**/
JNIEXPORT void JNICALL Java_com_george_jni_PUtilJni_createRedBlue3DPic
  (JNIEnv *env, jclass clazz, jobject leftBitmap_Red,jobject rightBitmap_GreenBlue,jobject tagBitmap,jint distent) {

    JNIEnv J = *env;

	if (leftBitmap_Red == NULL) {
	    J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "leftBitmap_Red is null");
		return;
	}

		if (rightBitmap_GreenBlue == NULL) {
		    J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "rightBitmap_GreenBlue is null");
    		return;
    	}

    		if (tagBitmap == NULL) {
    		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "tagBitmap is null");
        		return;
        	}

	// Get bitmap info
	AndroidBitmapInfo lInfo;
	memset(&lInfo, 0, sizeof(lInfo));
	AndroidBitmap_getInfo(env, leftBitmap_Red, &lInfo);
	// Check format, only RGB565 & RGBA are supported
	if (lInfo.width <= 0 || lInfo.height <= 0 ||
		(lInfo.format != ANDROID_BITMAP_FORMAT_RGB_565 && lInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
		LOGI("invalid bitmap:leftBitmap_Red\n");
		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
		return;
   	}
    //format 4==565
    //format 1==8888

		// Lock the bitmap to get the buffer
    	void * lPixels = NULL;
    	int res = AndroidBitmap_lockPixels(env, leftBitmap_Red, &lPixels);
    	if (lPixels == NULL) {
    		LOGI("fail to lock bitmap: %d\n", res);
    		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
    		return;
    	}

    	LOGI("lBitmap Effect: %dx%d, %d\n", lInfo.width, lInfo.height, lInfo.format);





	// Get bitmap info
    	AndroidBitmapInfo rInfo;
    	memset(&rInfo, 0, sizeof(rInfo));
    	AndroidBitmap_getInfo(env, rightBitmap_GreenBlue, &rInfo);
    	// Check format, only RGB565 & RGBA are supported
    	if (rInfo.width <= 0 || rInfo.height <= 0 ||
    		(rInfo.format != ANDROID_BITMAP_FORMAT_RGB_565 && rInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
    		LOGI("invalid bitmap:rightBitmap_GreenBlue\n");
    		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
    		return;
    	}

    			// Lock the bitmap to get the buffer
            	void * rPixels = NULL;
            	 res = AndroidBitmap_lockPixels(env, rightBitmap_GreenBlue, &rPixels);
            	if (rPixels == NULL) {
            		LOGI("fail to lock bitmap: %d\n", res);
            		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
            		return;
            	}

            	LOGI("rBitmap Effect: %dx%d, %d\n", rInfo.width, rInfo.height, rInfo.format);



    		// Get bitmap info
            	AndroidBitmapInfo tInfo;
            	memset(&tInfo, 0, sizeof(tInfo));
            	AndroidBitmap_getInfo(env, tagBitmap, &tInfo);
            	// Check format, only RGB565 & RGBA are supported
            	if (tInfo.width <= 0 || tInfo.height <= 0 ||
            		(tInfo.format != ANDROID_BITMAP_FORMAT_RGB_565 && tInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
            		LOGI("invalid bitmap:tagBitmap\n");
            		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
            		return;
            	}

    			// Lock the bitmap to get the buffer
            	void * tPixels = NULL;
            	 res = AndroidBitmap_lockPixels(env, tagBitmap, &tPixels);
            	if (tPixels == NULL) {
            		LOGI("fail to lock bitmap: %d\n", res);
            		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
            		return;
            	}

            	LOGI("tagBitmap Effect: %dx%d, %d\n", tInfo.width, tInfo.height, tInfo.format);



        if (tInfo.format != rInfo.format || tInfo.format != lInfo.format) {
                    		J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "the format is not same");
                    		return;
        }



	int x = 0, y = 0;
	// From top to bottom
	for (y = 0; y < tInfo.height; ++y) {
		// From left to right
		for (x = 0; x < tInfo.width; ++x) {
			int a = 255, r = 0, g = 0, b = 0;
			void *Lpixel = NULL;
			void *Rpixel = NULL;
			void *Tpixel = NULL;
			// Get each pixel by format

			//红色
			int mx = x + distent;
            if (mx < lInfo.width) {
            		if (lInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
                                				Lpixel = ((uint16_t *)lPixels) + y * lInfo.width + mx;
                                				uint16_t v = *(uint16_t *)Lpixel;
                                				r = RGB565_R(v);
                                			} else {// RGBA
                                				Lpixel = ((uint32_t *)lPixels) + y * lInfo.width + mx;
                                				uint32_t v = *(uint32_t *)Lpixel;
                                				r = RGBA_R(v);
                                			}
                    //            			LOGI("red: %d %d, %d\n", x, y, r);
            } else {
                r=0;
            }


            //绿色和蓝色
            if (rInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
                        				Rpixel = ((uint16_t *)rPixels) + y * rInfo.width + x;
                        				uint16_t v = *(uint16_t *)Rpixel;
                        				g = RGB565_G(v);
                                        b = RGB565_B(v);

                        			} else {// RGBA
                        				Rpixel = ((uint32_t *)rPixels) + y * rInfo.width + x;
                        				uint32_t v = *(uint32_t *)Rpixel;
                        				g = RGBA_G(v);
                                        b = RGBA_B(v);
                        			}
//                        			LOGI("gb: %d %d, %d %d \n", x, y, g,b);
            //目标图片
            if (tInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
                                    				Tpixel = ((uint16_t *)tPixels) + y * tInfo.width + x;
                                    			} else {// RGBA
                                    				Tpixel = ((uint32_t *)tPixels) + y * tInfo.width + x;
                                    			}


            //预测 真实
            //r    b
            //g    g

			// Write the pixel back
			if (tInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
				*((uint16_t *)Tpixel) = MAKE_RGB565(r, g, b);
			} else {// RGBA
				*((uint32_t *)Tpixel) = MAKE_RGBA(r, g, b, a);
			}
		}
	}

AndroidBitmap_unlockPixels(env, leftBitmap_Red);
AndroidBitmap_unlockPixels(env, rightBitmap_GreenBlue);
	AndroidBitmap_unlockPixels(env, tagBitmap);
  }



转载请注明出处。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值