With the new 2.3rc version of OpenCV, we are able to run executables that use OpenCV in adb shell without dealing with JNI interfaces for android. The new version also comes with prebuilt native camera support. (see http://opencv.willowgarage.com/wiki/Android2.3.0 for details ) I would like to know if it is possible to capture a preview frame from the camera using an executable that runs run in adb shell. Using the code snippet in the documentation I wrote the following simple code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
const char* message = "Capture a frame!";
const char* errorMessage = "Could not open the camera!";
int main(int argc, char* argv[])
{
// print message to console
printf("%s\n", message);
cv::VideoCapture capture(CV_CAP_ANDROID + 0);
//cv::VideoCapture capture(CV_CAP_ANDROID + 1);//front camera for Android 2.3.3 or newer
if( !capture.isOpened() )
{
printf("%s\n", errorMessage);
return 0;
}
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cv::Mat frame;
capture >> frame;
if( !frame.empty() ){
imwrite("/mnt/sdcard/CaptureFrame.png", frame);
}
return 0;
}
(The build scripts are similar to HelloAndroid sample application in the downloaded library)
It builds and runs in adb shell but I cannot capture frame and thus write to the image file. I checked the logcat output and it seems to load the libraries and opens the camera but then it gets stuck at "capture>>frame" step.
Any ideas?
Thanks
Zafer
Related
I am new to the android studio, could you please help me, I tried multiple time to print Logcats on Android emulator but unable to see Logcats print.Is there any possibility to print on Android emulator console by using Native code prints and also How to print Logcats on android studio logcat window using normal C printf.
I tried this one,
#include <stdio.h>
#include <android/log.h>
#define LOGD(...) __android_log_print(int prio, const char *tag, const char *fmt, ...)
int main()
{
int a=23;
LOGD(ANDROID_LOG_DEBUG,"Print something %d",a);
return 0;
}
Had a normal c code that prints helloworld
#include<stdio.h>
int main(void)
{
printf("Helloworld");
return 0;
}
I have to make an app to run this c code on a device running in android without using NDK and JNU for this and output should be in text view .As i am new to android help me regarding this.
I searched online and on this site and found this link which asks the same question I am about to, but the reply does not seem address the question. Plus one of the referenced links are missing. Basically the question is how do you efficiently and intelligently decide when to use a jni-compatible function to what you have in your C source file. I am familiar with what javah command does, but that command converts JAVA methods into a C header file to be used. What about the methods that are already implemented in a C source file? How can you know if you have to convert them to a JNI version of the method? I am using Android Studio and put in the following code in the .c source file that is in my jni folder of the project:
#include "com_example_sansari_usetbt_MainActivity.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <android/log.h>
#define TAG "native-log-tag"
#define LOGI(LOG_TAG, ...) __android_log_print (ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGV(LOG_TAG, ...) __android_log_print (ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGE(LOG_TAG, ...) __android_log_print (ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
/*
* Class: com_example_sansari_usetbt_MainActivity
* Method: usetbt
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_sansari_useqsee_MainActivity_Usetbt
(JNIEnv *env, jobject obj)
{
int fd;
int rc = 0;
char *rd_buf[16];
//(*env)->printf("<1>%s: entered\n", argv[0]);
printf("<1>: entered\n");
fd = open("/dev/tbt", O_RDWR);
LOGI(LOG_TAG,"This is a log test");
LOGV(LOG_TAG,"This is a log test");
LOGE(LOG_TAG,"This is a log test");
//(*env)->fd;
//return;
if ( fd == -1 ) {
perror("<1>open failed");
rc = fd;
}
printf("<1>: open: successful\n");
/* Issue a read */
rc = read(fd, rd_buf, 0);
//I need to find what fd is and then use command completion to pick a jni finction. rd-buf seems to be jstring, and 0 seems to be int
rc = (*env)->GetString
if ( rc == -1 ) {
perror("<1>read failed");
close(fd);
}
printf("<1>: read: returning %d bytes!\n",rc);
close(fd);
(*env)->NewStringUTF(env,"Hi From Usetbt version 2");
}
And ndk-build compiles the project, but I do not get the result I am looking for. That is, the normal C version of the code which I compiled with NDK opens the driver and is able to interface with it, but this does not do the same. I do not see the result of the print statement in the kernel logs that is. I am not asking for someone to convert the above code; rather show me the way to know which of the above lines need to be converted, and what is the best way to do it please. I have read a number of items about this, and I am quickly coming up to speed, but if you can describe at a high level how
this conversion is done, please advise. I do have a copy of Java Native Interface and a number of Android texts and am going through them as fast as possible.
Thanks
I believe you need to call fflush(stdout); after your printf. As per this answer.
I've been trying to make the HelloLua example to work in Android. It works if the hello.lua is not in compiled form using luac. But if I compile the hello.lua and upload it in my Android phone, it just gives me a black screen. Can anyone help me out in this?
This is the code in the AppDelegate::applicationDidFinishLaunching()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
if (pstrFileContent)
{
pEngine->executeString(pstrFileContent->getCString());
}
#else
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path_c_str());
#endif
Seems that compiled lua works when I'm running in Windows but not in Android.
The man page for luac says that "Precompiled chunks are not portable across different architectures".
I want my code will run automatically when the android starts up.And,i know that the init.rc srcipt will be runned when the android starts up.So,i decided to add my code's information as the service item in the init.rc.But unfortunately, it fails.
Below are the steps that I used:
1. I build the server.c via the ndk's cross-compiler:
server.c:
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
void sys_log(char* msg) {
FILE *fp;
time_t t;
if((fp=fopen("/data/server.log","a")) >=0)
{
t=time(0);
fprintf(fp,"%s at time: %s\n", msg, asctime(localtime(&t)) );
}
fclose(fp);
}
int main() {
printf("server begin!");
sys_log("server begin!");
)
I add the following lines to the end of /init.rc:
service socketserver /data/server
oneshot
I restart the android device
I use the instruction cat /data/server.log to check the log, but there is nothing in the server.log!
I run the server directly. Everything runs okay. Both the log and the console will prompt what i want!
So in conclusion, I think the init.rc does not start the server that I built.
Are my steps correct? Any ideas? Thank you for your help in advance!