Android Jni 利用OpenCV 实现图像任意角度旋转

一,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");
}

@Override
protected 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);


@Override
public 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转换BGRA
Mat 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android中使用JNI调用OpenCV库来实现摄像头功能是可行的。以下是调用摄像头的简单步骤: 首先,你需要在Android项目中集成OpenCV库。可以通过将OpenCV库添加到Gradle依赖项或手动导入库文件来完成。 其次,创建一个JNI接口,将Java代码与C/C++代码进行绑定。JNI接口允许在Java和C/C++之间进行双向通信。 然后,在C/C++代码中编写与摄像头相关的功能。可以使用OpenCV提供的函数和类来处理视频流。例如,你可以使用VideoCapture类打开摄像头并获取图像帧。 接下来,在JNI接口中实现调用摄像头的功能。通过JNI接口,将Java代码的请求传递给C/C++函数来执行摄像头操作。你可以设置一个循环,不断从摄像头读取图像帧并进行处理。 最后,在Java代码中调用JNI接口中的方法来启动摄像头。可以使用SurfaceView来显示摄像头捕获的图像。你可以使用Camera类来控制摄像头的预览以及其他设置。 需要注意的是,在JNI和C/C++代码中处理摄像头操作时,要遵循正确的线程管理和图像处理技术,以确保流畅的手机摄像头应用程序。 总之,通过JNI调用OpenCV库,可以很方便地在Android实现摄像头功能。项目中要同时涉及Java和C/C++代码,需要进行正确的接口绑定和线程管理。这样,你可以轻松地使用OpenCV函数和类来处理摄像头操作并实现自己的摄像头应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值