[SOLVED] How to get densityDpi in OF through JNI?

#1

Hi guys, I need to get the density DPI of the screen in Android, to make my app to show correctly in all devices.

I searched in the forum and I got that I have to use JNI, bui I don’t know precisely how it works and I am getting some issues… I think i am wrong in something.

In Java I have the displayMetrics library, that works correctly, and I call the code in the onCreate method:

@Override
    public void onCreate(Bundle savedInstanceState)
    { 
        super.onCreate(savedInstanceState);
        String packageName = getPackageName();

        ofApp = new OFAndroid(packageName,this);
        
        DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		densityDpi = dm.densityDpi;
		System.out.println("Java dpi: " + densityDpi);
		
		this.dpiJNI();
    }
	
	public native void dpiJNI();

The console print me correctly tha density in DPI of the screen.

Here is the code to call it in C++ in main.cpp:

extern "C" {
	void Java_cc_openframeworks_androidEmptyExample_OFActivity_dpiJNI (JNIEnv* env, jobject thiz, jint densityDpi) {
		int dpi = densityDpi;
		ofLog(OF_LOG_NOTICE, "javaDPI: " + ofToString(densityDpi));
		ofLog(OF_LOG_NOTICE, "DPI: " + ofToString(dpi));
	}
}

What I get is a little number (46 for 480dpi screen that I get in Java).
I know I am wrong in something, I think it’s because of my little experience in C++…

Thanks a lot whoever will help (or not) me, maybe it can be helpful for someone else :smiley:

Getting data from OFActvity and viceversa
#2

you need to pass the parameter to the function if not what you’ll get is just anything that is in memory in that position:

java:

    ...
        this.dpiJNI(densityDpi);
    }

public native void dpiJNI(int dpi);
#3

Thanks a lot, it worked for me, ut now I have another problem related: the int dpi that i have in main.cpp, is not a global variable, so I can’t use it in ofApp.cpp… I was thinking to call the JNI in ofApp.cpp, at the beginning, something like this:

#include "ofApp.h"
#include <jni.h>

extern "C" {
	void Java_cc_openframeworks_androidEmptyExample_OFActivity_dpiJNI (JNIEnv* env, jobject thiz, jint densityDpi) {
		dpi = densityDpi;
	}
}

//--------------------------------------------------------------
void ofApp::setup() {
// some code here
}

//--------------------------------------------------------------
void ofApp::update() {
// some code here
}

//--------------------------------------------------------------
void ofApp::draw() {
// some code here
}

with dpi declared in ofMain.h. But I don’t know if it is the best solution :\

#4

you can’t declare variables in a .h file but you can just declare it at the beginning of the ofApp.cpp

#5

OK now it works, thanks a lot, really :slight_smile:

#6

Hi! i’m trying your example. But is not recognizing this variable
“ofApp”
ofApp = new OFAndroid(packageName,this);
and "densityDpi " densityDpi = dm.densityDpi;

I guess "densityDpi " is a float or a int, but what is ofApp?