[英]Passing image from java to C++ Using JNI
I am using NDK and JNI for my android app therefore I have some of my codes in native. 我正在为我的android应用程序使用NDK和JNI,因此我有一些本机代码。 I have a custom adapter in which there's a list view that displays the images and buttons, that came from a database btw.
我有一个自定义适配器,其中有一个列表视图,显示来自数据库btw的图像和按钮。 However, the image will be used by the c++.
但是,该图像将由c ++使用。 If I click the button (from the java code), it will pass (I really don't know the term) the image to the c++ code where I will use the image.
如果我单击按钮(来自Java代码),它将把图像传递(我真的不知道该术语)到使用该图像的C ++代码。 Is it possible for the c++ code to access the image triggered by the button from the Java code?
c ++代码是否可以从Java代码访问由按钮触发的图像?
I'm pretty much a beginner but this is for my final project so any help will be appreciated :) 我几乎是个初学者,但这是我的最后一个项目,因此对您的帮助将不胜感激:)
Adapter.Java Adapter.Java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.listview, null);
TextView pName = (TextView) v.findViewById(R.id.product_name);
ImageView pImage = (ImageView) v.findViewById(R.id.product_image);
TextView pPrice = (TextView) v.findViewById(R.id.product_price);
TextView pDescription = (TextView) v.findViewById(R.id.product_description);
TextView pCategory = (TextView) v.findViewById(R.id.product_category);
pName.setText(mProductList.get(position).getName());
Product image = mProductList.get(position);
final byte[] img = image.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
pImage.setImageBitmap(bitmap);
pPrice.setText("$" + String.valueOf(mProductList.get(position).getPrice()));
pDescription.setText(mProductList.get(position).getDescription());
pCategory.setText(mProductList.get(position).getCategory());
Button tryMe = (Button)v.findViewById(R.id.tryMe);
tryMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cameraIntent = new Intent(mContext, OpencvCamera.class);
mContext.startActivity(cameraIntent);
}
});
return v;
}
This code below is where I want my button to pass the image from the database to the c++ in which it will be used with the camera. 下面的代码是我希望按钮将图像从数据库传递到将与相机一起使用的C ++的位置。 In here, the button only redirects when pressed to the camera class.
在此,仅当按下摄像机类别时,按钮才会重定向。
Button tryMe = (Button)v.findViewById(R.id.tryMe);
tryMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*WHAT SHOULD I PUT HERE FOR THE IMAGE PASSING*/
Intent cameraIntent = new Intent(mContext, OpencvCamera.class);
mContext.startActivity(cameraIntent);
}
}
Below are the codes for my C++ 以下是我的C ++代码
JNIEXPORT void JNICALL Java_nerds_thesis_clartips_OpencvClass_humanDetection
(JNIEnv *, jclass, jlong addrRgba){
Mat& frame = *(Mat*)addrRgba;
detectHuman(frame);
}
void detectHuman(Mat& frame){
String human_cascade_name = "/storage/emulated/0/haarcascade_upperbody.xml";
CascadeClassifier human_cascade;
if(!human_cascade.load( human_cascade_name ) ) { printf("--(!)Error loading\n"); return; };
std::vector<Rect> humans;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray);
//Detect Human
human_cascade.detectMultiScale( frame_gray, humans, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(100, 100) );
Mat imageMask = imread("/storage/emulated/0/plain.png");
/*CODE FOR OVERLAYING THE IMAGE TO THE CAMERA*/
The line above Mat imageMask = imread("/storage/emulated/0/plain.png");
Mat imageMask = imread("/storage/emulated/0/plain.png");
上方的行Mat imageMask = imread("/storage/emulated/0/plain.png");
is what I only do for testing. 这是我唯一要做的测试。 I only declared it and any of my buttons used the same image.
我只声明了它,而我的任何按钮都使用了相同的图像。 Of course it will.
当然可以。 That's why I'm asking what should I do to have my native code get an access with the images when I press the button created with a java code.
这就是为什么我问当我按下用Java代码创建的按钮时,应该怎么做才能使本机代码访问图像。
I don't think it is possible to pass the image object in memory; 我认为不可能将图像对象传递到内存中。 however you can pass a reference to the image, like an URI or filepath for your C++ code to use.
但是,您可以传递对图像的引用,例如URI或C ++代码要使用的文件路径。 Simply adapt your JNI declaration to add that reference.
只需修改您的JNI声明即可添加该引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.