Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with linking javaCV #157

Closed
Kaitis opened this issue May 26, 2015 · 6 comments
Closed

Need help with linking javaCV #157

Kaitis opened this issue May 26, 2015 · 6 comments
Labels

Comments

@Kaitis
Copy link

Kaitis commented May 26, 2015

Hello , I used the manual instructions to JavaCV to my project but it doesn't work. Please help!

This is from build.gradle:

   android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.example.andreaskaitis.myapplication"
        minSdkVersion 19
        targetSdkVersion 21
        maxSdkVersion 21
        versionCode 1
        versionName "1.0"

        sourceSets.main {
            jniLibs.srcDir 'src/main/libs'
            jni.srcDirs = []
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    task ndkBuild(type: Exec) {
        // Read ndk path from local.properties file
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        def ndkDir = properties.getProperty('ndk.dir')

        commandLine "$ndkDir/ndk-build", '-C', file('src/main/jni').absolutePath
    }

    tasks.withType(JavaCompile) {

        compileTask -> compileTask.dependsOn ndkBuild
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'uk.co.chrisjenx:calligraphy:2.0.1'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
    compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3'
    compile 'com.makeramen:roundedimageview:2.0.1'
    compile files('libs/ffmpeg.jar')
    compile files('libs/javacpp.jar')
    compile files('libs/javacv.jar')
    compile files('libs/opencv.jar')
    compile files('libs/opencv-android-arm.jar')
    compile files('libs/ffmpeg-android-arm.jar')
}

I think the problem I am having has to do with the bug:

 Caused by: java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.bytedeco.javacpp.avutil

However, the difference in my case is that my application uses the ndk to build a cpp library at runtime.
Tried gradle and maven dependencies. Tried unpacking the .jar files and including the .so files in the folders jniLibs/armeabi-v7a, jniLibs/x86, etc but it seems that the jniLibs subfolders get re-created at runtime.

Is there another way to link JavaCV?

@saudet
Copy link
Member

saudet commented May 27, 2015

I don't know, that seems to be a problem with Android Studio. There's a related issue here: #133 (comment) I'm not sure what Google intends to do about these kinds of situations... Maybe you could ask them and come back with a definitive answer? It would help a lot.

@Kaitis
Copy link
Author

Kaitis commented May 28, 2015

Hello,
Thanks for your reply. I followed the instructions in #133 and tries the
proguard settings in the wiki page but it didn't work for me.
The closest I got was when I created the folder structure as you can see in
the photo(below). This made the UnsatisfiedLink for avutil go away and now
I am left with only this error (proguard settings made no difference to the
output):

05-28 04:18:40.226    4354-4354/com.example.andreaskaitis.myapplication
W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while
initializing Lorg/bytedeco/javacpp/opencv_core;

If you can offer a suggestion on what to try next, it would be greatly
appreciated.

This is my folder structure:[image: Inline image 1]

Thanks for your time.

On Wed, May 27, 2015 at 4:18 PM, Samuel Audet [email protected]
wrote:

I don't know, that seems to be a problem with Android Studio. There's a
related issue here: #133 (comment)
#133 (comment) I'm
not sure what Google intends to do about these kinds of situation... Maybe
you could ask them and come back with a definitive answer? It would help a
lot.


Reply to this email directly or view it on GitHub
#157 (comment).

@saudet
Copy link
Member

saudet commented May 30, 2015

GitHub apparently doesn't support email attachments, so there is no image for me to see, but I'm guessing the native libraries for OpenCV are simply just missing. Please try to add them once more.

@Kaitis
Copy link
Author

Kaitis commented Jun 3, 2015

I finally managed to resolve the link error!

If it is not too much to ask, can you please take a look at the method I
use to create the movie, I must be missing something :

public void createMovie(View view) {

    Loader.load(opencv_highgui.class);
    Loader.load(org.bytedeco.javacpp.opencv_core.class);
    Loader.load(swresample.class);

    new AsyncTask<Void, Void, Void>() {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(WriteMovieActivityAsync.this);
            dialog.setMessage("Generating video, Please wait.......");
            dialog.setCancelable(false);
            dialog.show();
        };

        @Override
        protected Void doInBackground(Void... params) {
            videoPath = OUTPUT_DIR + "/" + "test.mp4";
            FFmpegFrameRecorder recorder = new
FFmpegFrameRecorder(videoPath, mWidth, mHeight);

            AndroidFrameConverter frameConverter = new AndroidFrameConverter();

            try {
                recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
                recorder.setFormat("mp4");
                recorder.setVideoQuality(0);
                recorder.setFrameRate(10);

                long startTime = 0;
                try {
                    recorder.start();
                } catch (FrameRecorder.Exception e) {
                    e.printStackTrace();
                }

                int nLen = imageIDs.length;
                if (nLen > durations.size())
                    nLen = durations.size();

                for (int i = 0; i < nLen; i++) {

                       int imageID = imageIDs[i];
                       Uri imageURI =
Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""
+ imageID);


                       final Bitmap rgb565 =
loader.loadImageSync(imageURI.toString(), options);

                       Frame frame = frameConverter.convert(rgb565);

                       //Get frame duration in milliseconds
                       long duration = (long) ((float) durations.get(i));

                       recorder.setTimestamp(startTime);
                       try {
                           recorder.record(frame);
                       } catch (FrameRecorder.Exception e) {
                           e.printStackTrace();
                       }

                       startTime += duration;

                }
            } finally {
                try {
                    Log.v("RECORDER", "Total time: " + recorder.getTimestamp());
                    recorder.stop();
                } catch (FrameRecorder.Exception e) {
                    e.printStackTrace();
                }

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            dialog.dismiss();
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(videoPath), "video/mp4");
            startActivity(intent);
            Toast.makeText(WriteMovieActivityAsync.this, "Video
Saved", Toast.LENGTH_LONG).show();
        }
    }.execute();


}

I get this error on every iteration:

06-03 10:00:54.740    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ org.bytedeco.javacv.FrameRecorder$Exception:
avcodec_encode_video2() error -1: Could not encode video packet.
06-03 10:00:54.740    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
org.bytedeco.javacv.FFmpegFrameRecorder.recordImage(FFmpegFrameRecorder.java:722)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:634)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
com.example.andreaskaitis.myapplication.WriteMovieActivityAsync$2.doInBackground(WriteMovieActivityAsync.java:586)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
com.example.andreaskaitis.myapplication.WriteMovieActivityAsync$2.doInBackground(WriteMovieActivityAsync.java:536)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-03 10:00:54.744    3710-5457/com.example.andreaskaitis.myapplication
W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
06-

Thanks.
On Sat, May 30, 2015 at 10:58 AM, Samuel Audet [email protected]
wrote:

GitHub apparently doesn't support email attachments, so there is no image
for me to see, but I'm just guessing the native libraries for OpenCV are
missing. Please try to add them once more.


Reply to this email directly or view it on GitHub
#157 (comment).

@saudet saudet added the question label Jun 6, 2015
@saudet
Copy link
Member

saudet commented Jun 6, 2015

First, make sure that duration >= 100000 (microseconds) and that you have write access to OUTPUT_DIR. Let me know if that's the cause of the issue or not, thanks!

BTW, you've asked the same question in #159. Please don't do that.

@saudet saudet closed this as completed Jun 6, 2015
@Kaitis
Copy link
Author

Kaitis commented Jun 7, 2015

Thank you! My problem was the with the duration.

On Sat, Jun 6, 2015 at 4:24 PM, Samuel Audet [email protected]
wrote:

First, make sure that duration >= 100000 (microseconds) and that you have
write access to OUTPUT_DIR. Let me know if that's the cause of the issue
or not, thanks!


Reply to this email directly or view it on GitHub
#157 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants