OnClickListener does not show the output of my function

I make function 2, one function gives brightness to the image, and the second function converts the image to shades of gray, below is my jni code for it (I use android with eclipse)

int toGray(Mat mSrc, Mat& bgra); int tobrightness(Mat mSrc, Mat& bgra); extern "C" { JNIEXPORT jint JNICALL Java_org_opencv_samples_NativeActivity_CvNativeActivity_grayimg(JNIEnv* env, jobject,jint width, jint height, jintArray in, jintArray out) { jint* _in = env->GetIntArrayElements(in, 0); jint* _out = env->GetIntArrayElements(out, 0); Mat mSrc(height, width, CV_8UC4, (unsigned char*)_in); Mat bgra(height, width, CV_8UC4, (unsigned char*)_out); int conv; jint retVal; conv = toGray(mSrc ,bgra); retVal = (jint)conv; env->ReleaseIntArrayElements(in, _in, 0); env->ReleaseIntArrayElements(out, _out, 0); return retVal; } } JNIEXPORT jint JNICALL Java_org_opencv_samples_NativeActivity_CvNativeActivity_eqhist(JNIEnv* env, jobject,jint width, jint height, jintArray in, jintArray out) { jint* _in = env->GetIntArrayElements(in, 0); jint* _out = env->GetIntArrayElements(out, 0); Mat mSrc(height, width, CV_8UC4, (unsigned char*)_in); Mat bgra(height, width, CV_8UC4, (unsigned char*)_out); Mat bgr(height, width, CV_8UC3); int conv; jint retVal; conv = tobrightness(mSrc, bgra); retVal = (jint)conv; env->ReleaseIntArrayElements(in, _in, 0); env->ReleaseIntArrayElements(out, _out, 0); return retVal; } int tobrightness(Mat mSrc, Mat& bgra) { vector<Mat> sChannels; split(mSrc, sChannels); for(int i=0; i<sChannels.size(); i++) { Mat channel = sChannels[i]; equalizeHist(channel, channel); } merge(sChannels, bgra); return 1; } int toGray(Mat mSrc, Mat& bgra) { Mat gray(mSrc.rows, mSrc.cols, CV_8UC1); cvtColor(mSrc , gray , CV_BGRA2GRAY); cvtColor(gray , bgra , CV_GRAY2BGRA); return 1; } 

Is it Okay call this twice / thrice or many times the jni method in one cpp file as described above? Since I want that if I click on one button, it should fulfill the brightness function, and when I click on the second button, it should fulfill the grayscale , so what am I going straight to with the above cpp for this scenerio?

below is my java code:

 public class CvNativeActivity extends Activity { public native int eqhist(int width, int height, int [] mPhotoIntArray, int [] mCannyOutArray); public native int grayimg(int width, int height, int [] mPhotoIntArray, int [] mCannyOutArray); static { System.loadLibrary("native_activity"); Log.i("EqActivity", "native library loaded successfully"); } /** 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.me); Bitmap bmInImg = BitmapFactory.decodeStream(is); int [] mPhotoIntArray; int [] mCannyOutArray; mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()]; imageview_1.setImageBitmap(bmInImg); // 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()]; eqhist(bmInImg.getHeight(), bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray); grayimg(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); String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); String outFileName = extStorageDirectory + "/me.png"; 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(); } } } 

The above code works fine and shows the result for the eqhist method

But the call function by pressing a button (I adjusted the code below the code and did not receive an error, but did not show the output):

 Button button= (Button) findViewById(R.id.NextButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub eqhist(bmInImg.getHeight(), bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray); } }); 

Mistake:

 08-05 00:42:21.656: E/ActivityManager(360): writeStringToFile error: /sys/kernel/debug/tracing/tracing_enabled java.io.FileNotFoundException: /sys/kernel/debug/tracing/tracing_enabled: open failed: ENOENT (No such file or directory) 08-05 00:57:56.150: E/ActivityManager(360): ANR in org.opencv.samples.NativeActivity (org.opencv.samples.NativeActivity/.CvNativeActivity) 08-05 00:57:56.150: E/ActivityManager(360): Reason: keyDispatchingTimedOut 08-05 01:18:16.986: E/Trace(20443): error opening trace file: No such file or directory (2) 

Edit:

main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" > <ImageView android:id="@+id/imageView1" android:contentDescription="@null" android:layout_width="200dp" android:layout_height="200dp" /> <ImageView android:id="@+id/imageView2" android:contentDescription="@null" android:layout_width="250dp" android:layout_height="250dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <Button android:id="@+id/NextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignParentRight="true" android:layout_marginRight="21dp" android:text="@string/Next_Button" /> </RelativeLayout> 
+2
java android eclipse android-ndk jni
Aug 03 '14 at 17:49
source share
2 answers

As I said in the comment that you make very few errors in the java file, your other files are fine, you do not put your result in a button click, so the button has nothing to display (without an image), your raster file The output is located behind the button, click, your bitmap will not display anything when the button is pressed. Place these lines inside the button.

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

The below code works for me, as I tested on Android, but its bit is slow:

 Button button= (Button) findViewById(R.id.NextButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("APP: ", "Into OnClick of SettingDialog. View = " + v); eqhist(bmInImg.getHeight(),bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray); 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); } }); 
+2
Aug 09 '14 at 11:23
source share

You do not need to do this twice. You can call him.

 extern "C" { JNIEXPORT jint JNICALL Java_org_opencv_samples_NativeActivity_CvNativeActivity_grayimg(JNIEnv* env, jobject,jint width, jint height, jintArray in, jintArray out) { // process } JNIEXPORT jint JNICALL Java_org_opencv_samples_NativeActivity_CvNativeActivity_eqhist(JNIEnv* env, jobject,jint width, jint height, jintArray in, jintArray out) { // process } } int tobrightness(Mat mSrc, Mat& bgra) { // process } int toGray(Mat mSrc, Mat& bgra) { // process } 

Now in CvNativeActivity you need to define the following functions.

 public native int grayimg(int w, int h, int[] in, int[] out); public native int eqhist(int w, int h, int[] in, int[] out); 

And it seems that you get an error while writing Bitmap to the .png file. Update your question with the OutputBitmapToFile function.

Another possible solution is to call this function in a new thread.

 public class ProcessTask extends AsyncTask<Void, Void, Boolean> { @Override protected Boolean doInBackground() { // this happens in a background thread eqhist(bmInImg.getHeight(), bmInImg.getWidth(), mPhotoIntArray, mCannyOutArray); } @Override protected void onPostExecute(Boolean result) { // save image, show image etc // This happens on the UI thread } } 

And your onClick call,

 Button button= (Button) findViewById(R.id.NextButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ProcessTask task = new ProcessTask(); task.execute(); } }) 
0
Aug 07 '14 at 16:18
source share



All Articles