如何在C ++界面中传输我的代码

时间:2014-04-15 07:48:05

标签: android opencv android-ndk java-native-interface jnienv

jboolean
Java_org_opencv_samples_NativeActivity_CvNativeActivity_BrightnessJNI (
        JNIEnv* env, jobject thiz,
        jint height, jint width, jintArray in, jintArray out)
{
    jint* _in = env->GetIntArrayElements(in, 0);
    jint* _out = env->GetIntArrayElements(out, 0);
        //Build the Mat structure for input data
    Mat mSrc(height, width, CV_8UC4, (unsigned char *)_in);
        //Build the Mat structure for output data
    Mat mOut(height, width, CV_8UC4, (unsigned char *)_out);

   IplImage  mSrcImg = mSrc;
   IplImage  moutImg = mOut;
   IplImage *moutimg2,*b,*g,*r;
   moutimg2 = cvCreateImage( cvGetSize(&mSrcImg), 8, 3 ) ;
   b = cvCreateImage( cvGetSize(&mSrcImg), 8, 1 );
   g = cvCreateImage( cvGetSize(&mSrcImg), 8, 1 );
   r = cvCreateImage( cvGetSize(&mSrcImg), 8, 1 ) ;

   cvSplit(&mSrcImg, b, g, r, 0 );
   cvEqualizeHist( b, b );
   cvEqualizeHist( g, g );
   cvEqualizeHist( r, r );
   cvMerge(b,g,r,0,moutimg2);
   cvCvtColor(moutimg2,  &moutImg, CV_BGR2GRAY);
   env->ReleaseIntArrayElements(in, _in, 0);
   env->ReleaseIntArrayElements(out, _out, 0);
   return true;
}

我想在使用JNI时将我的代码转换为C ++界面。这是我的java代码

public class CvNativeActivity extends Activity 
{

    public native boolean BrightnessJNI(int width, int height, int [] mPhotoIntArray, int [] mCannyOutArray);

    static 
    {
        System.loadLibrary("native_activity");
    }
    /** Called when the activity is first created. */

    ImageView imageview_1;
    ImageView imageview_2;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         imageview_1=(ImageView) findViewById(R.id.imageView1);
         imageview_2=(ImageView) findViewById(R.id.imageView2);

        InputStream is;
        is = this.getResources().openRawResource(R.drawable.foot);
        Bitmap bmInImg = BitmapFactory.decodeStream(is);

        int [] mPhotoIntArray;
        int [] mCannyOutArray;

        mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];
        // Copy pixel data from the Bitmap into the 'intArray' array
        bmInImg.getPixels(mPhotoIntArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());

        //create the Brightness result buffer
        mCannyOutArray = new int[bmInImg.getWidth() * bmInImg.getHeight()];

        //
        // Do Brightness
        //
        BrightnessJNI(bmInImg.getHeight(), bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray);

        //
        // Convert the result to Bitmap
        //
        Bitmap bmOutImg = Bitmap.createBitmap(bmInImg.getWidth(), bmInImg.getHeight(), Config.ARGB_8888);  
        bmOutImg.setPixels(mCannyOutArray, 0, bmInImg.getWidth(), 0, 0, bmInImg.getWidth(), bmInImg.getHeight());


        imageview_2.setImageBitmap(bmOutImg);

        //
        // Save the result to file
        //
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        String outFileName = extStorageDirectory + "/Brightness";

        OutputBitmapToFile(bmOutImg, outFileName);





    }

    void OutputBitmapToFile(Bitmap InBm, String Filename)
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        InBm.compress(Bitmap.CompressFormat.PNG, 100, bytes);

        File f = new File(Filename);
        try
        {
            f.createNewFile();
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }           
    }
}

使用C接口时,这会给我以下错误

OpenCV Error: Assertion failed <dst.data == dst0.data> in cvCvtColor, file ..\..\..\source\modules\imgproc\src\color.cpp, line 4422

我可以使用带有Jni和原生android的C ++接口,任何样本都非常具有吸引力

1 个答案:

答案 0 :(得分:0)

是。你不需要丑陋的IplImage来使用JNI接口。对于java来说至少它是这样的。我对Android没有100%肯定,但我认为你可以。