【问题标题】:Passing image from java to C++ Using JNI使用 JNI 将图像从 java 传递到 C++
【发布时间】:2018-03-08 12:21:31
【问题描述】:

我在我的 android 应用程序中使用 NDK 和 JNI,因此我有一些本地代码。我有一个自定义适配器,其中有一个显示图像和按钮的列表视图,它们来自数据库 btw。但是,图像将被 c++ 使用。如果我单击按钮(来自 java 代码),它会将图像传递(我真的不知道该术语)到我将使用图像的 c++ 代码。 c++代码是否可以从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;
}

下面的代码是我希望我的按钮将图像从数据库传递到将与相机一起使用的 c++ 的地方。在这里,按钮仅在按下时重定向到相机类。

    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);
        }
    }

下面是我的 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*/

Mat imageMask = imread("/storage/emulated/0/plain.png"); 上面的那一行是我只做测试用的。我只声明了它,我的任何按钮都使用了相同的图像。当然会的。这就是为什么我要问,当我按下用 java 代码创建的按钮时,我应该怎么做才能让我的本机代码访问图像。

【问题讨论】:

  • 你想在JNI中对图像做什么?
  • 图像是一件衬衫,我希望它在检测到人体后覆盖在相机预览上,

标签: java android c++ android-ndk java-native-interface


【解决方案1】:

我认为不可能在内存中传递图像对象;但是,您可以传递对图像的引用,例如供 C++ 代码使用的 URI 或文件路径。 只需调整您的 JNI 声明以添加该引用。

【讨论】:

  • 我几乎是一个初学者,所以这对我来说真的是一个很难的话题。你能为你的答案做一个代码示例吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 2019-10-06
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多