Android Jni 利用OpenCV 实现图像任意角度旋转
来源:互联网 发布:网络社会案例分析 编辑:程序博客网 时间:2024/05/23 02:07
一,java代码,收集Bitmap 信息
private Button btnProc;private ImageView imageView;private Bitmap bmp;// Used to load the 'native-lib' library on application startup.static { System.loadLibrary("native-lib");}@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Example of a call to a native method btnProc = (Button) findViewById(R.id.btn_gray_process); imageView = (ImageView) findViewById(R.id.image_view); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test7); imageView.setImageBitmap(bmp); btnProc.setOnClickListener(this);}/** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */public static native int[] grayProc(int[] pixels, int w, int h);@Overridepublic void onClick(View view) { int w = bmp.getWidth(); int h = bmp.getHeight(); int[] pixels = new int[w*h]; bmp.getPixels(pixels, 0, w, 0, 0, w, h); long startTime = System.currentTimeMillis(); int[] resultInt = grayProc(pixels, w, h); long endTime = System.currentTimeMillis(); Log.e("JNITime",""+(endTime-startTime)); Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); //(@ColorInt int[] pixels, int offset, int stride,int x, int y, int width, int height) resultImg.setPixels(resultInt, 0, w, 0, 0, w, h); imageView.setImageBitmap(resultImg);}
二,jni实现
using namespace std;
using namespace cv;
Mat rotateImage1(Mat img, int degree){
degree = -degree;double angle = degree * CV_PI / 180.; // 弧度//获取弧度对应的三角函数值double a = sin(angle);double b = cos(angle);//获取矩阵的宽高int width = img.cols;int height = img.rows;int width_rotate = int(height * fabs(a) + width * fabs(b));int height_rotate = int(width * fabs(a) + height * fabs(b));float map[6];Mat map_matrix = Mat(2, 3, CV_32F, map);// 旋转中心CvPoint2D32f center = cvPoint2D32f(width / 2 +0.5, height / 2 +0.5);CvMat map_matrix2 = map_matrix;//( CvPoint2D32f center, double angle, double scale, CvMat* map_matrix );cv2DRotationMatrix(center, degree, 1.0, &map_matrix2);Mat img_rotate;//CV_WARP_FILL_OUTLIERS - 填充所有输出图像的象素。//如果部分象素落在输入图像的边界外,那么它们的值设定为 fillval.//CV_WARP_INVERSE_MAP - 指定 map_matrix 是输出图像到输入图像的反变换,//( InputArray src, OutputArray dst, InputArray M, Size dsize,warpAffine(img, img_rotate, map_matrix, Size(width_rotate, height_rotate));return img_rotate;
}
extern “C”
JNIEXPORT jintArray JNICALL
Java_com_example_dgxq008_opencv_1readpixel_MainActivity_grayProc(JNIEnv *env, jclass type
, jintArray pixels_
, jint w
, jint h) {
jint* pixels = env->GetIntArrayElements(pixels_, NULL);if (pixels==NULL){ return 0;}//图片一进来时是ARGB 通过mat转换BGRAMat img(h,w,CV_8UC4,(uchar *)pixels); //pixels 操作的是同一份数据Mat temp; //传入自己要实现的角度,这里实现倒影效果int degree = 180;//其实现方法,是从网上搜的工具方法Mat m_ResImg = rotateImage1(img, degree);resize(m_ResImg,img,Size( img.cols, img.rows ));//对应数据指针int size = w*h;jintArray result = env->NewIntArray(size);env->SetIntArrayRegion(result,0,size,pixels);env->ReleaseIntArrayElements(pixels_, pixels, 0);return result;
}
阅读全文
0 0
- Android Jni 利用OpenCV 实现图像任意角度旋转
- opencv 任意角度旋转图像
- opencv 任意角度旋转图像
- opencv 任意角度旋转图像
- opencv 任意角度旋转图像
- OPENCV入门教程九:图像旋转任意角度
- opencv 中对图像旋转任意角度
- c++实现图像旋转任意角度
- opencv 利用仿射变换函数对图像进行任意角度旋转
- 图像旋转任意角度
- 任意角度旋转图像
- opencv实现图像任意角度旋转的算法解析及代码实现
- 用OpenCV的仿射变换函数warpAffine实现图像的任意角度旋转!
- OpenCV 下的图像任意角度的旋转
- RGB图像任意角度旋转
- matlab旋转图像任意角度
- opencv下任意角度旋转
- Android Jni 利用OpenCV 实现图像尺寸缩放(一)
- lua table的使用及坑
- Vue 中 export default 和 module.exports
- Xcode :Could not locate device support files 的解决方案
- 集电极开路输出
- JACK_C#_循环语句
- Android Jni 利用OpenCV 实现图像任意角度旋转
- Effective java ---异常
- 【深入Java虚拟机】之四:类加载机制
- 在HTML页面中使用自定义弹框
- 学习日常链接收集
- 输入n个正整数,求输出0~9出现的次数(以-1为结束标志)
- web开发基础-第一讲
- ASP.NET的Web.config基本配置节介绍
- MyBatis中配置文件