Android:java.lang.UnsatisfiedLinkError ...无法找到" lib ..."

时间:2016-12-03 05:15:19

标签: java android-studio

我已在Github上遵循AndroidScannerDemo的构建说明。 我成功构建了AndroidScannerDemo。然后我想通过Github上的SimpleTesseractExample导入OCR库来开发此应用程序。我正在尝试运行测试应用程序。应用程序可以启动,但是当我尝试访问相机和图库模式时,它会出现以下异常:

testRunner出错:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.scanner.demo, PID: 2715
              java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-support-annotations-23.2.1_3e4fb588ccc5090e3d7674f4d618c6c58911a463-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-internal_impl-23.2.1_95668bb7e1c637bd79cf411dae8840e187175822-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-com.rmtheis-tess-two-5.4.1_e5cb86e300070c39241b9a43567ee2efb208d508-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.2.1_6c016aaa8587fb0461cb7e32ce3f36bda91ced6b-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-com.android.support-support-v4-23.2.1_387cf51515861ffabd82d96d9419f72e885ab771-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.2.1_8d29c9636ac8f816df67405c13d3515c565cd287-classes.dex", dex file "/data/data/com.scanner.demo/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.2.1_96909a566032fc8bdb0819e184295a7955082571-classes.dex"],nativeLibraryDirectories=[/data/app/com.scanner.demo-2/lib/x86_64, /system/fake-libs64, /data/app/com.scanner.demo-2/base.apk!/lib/x86_64, /system/lib64, /vendor/lib64]]] couldn't find "libopencv_java3.so"
                  at java.lang.Runtime.loadLibrary0(Runtime.java:984)
                  at java.lang.System.loadLibrary(System.java:1530)
                  at com.scanlibrary.ScanActivity.<clinit>(ScanActivity.java:73)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

当我将此行添加到依赖项部分

中的build.gradle时,会发生这种情况
compile 'com.rmtheis:tess-two:5.4.1'

MainActivity Code:

package com.scanner.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.googlecode.tesseract.android.TessBaseAPI; //OCR

public class MainActivity extends ActionBarActivity {

private static final int REQUEST_CODE = 99;
private Button scanButton;
private Button cameraButton;
private Button mediaButton;
private ImageView scannedImageView;

//----------- OCR PART --------
private TessBaseAPI mTess;
String datapath = "";
Bitmap image;
//-----------------------------

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

//----------- OCR PART --------
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //init image
    image = BitmapFactory.decodeResource(getResources(), R.id.scannedImage);

    //initialize Tesseract API
    String language = "eng";
    datapath = getFilesDir()+ "/tesseract/";
    mTess = new TessBaseAPI();

    checkFile(new File(datapath + "tessdata/"));

    mTess.init(datapath, language);
//-----------------------------

}



private void init() {

    scanButton = (Button) findViewById(R.id.scanButton);
    scanButton.setOnClickListener(new ScanButtonClickListener());

    cameraButton = (Button) findViewById(R.id.cameraButton);
    cameraButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_CAMERA));

    mediaButton = (Button) findViewById(R.id.mediaButton);
    mediaButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_MEDIA));

    scannedImageView = (ImageView) findViewById(R.id.scannedImage);
}


private class ScanButtonClickListener implements View.OnClickListener {


    private int preference;

    public ScanButtonClickListener(int preference) {
        this.preference = preference;
    }

    public ScanButtonClickListener() {
    }

    @Override
    public void onClick(View v) {
        startScan(preference);
    }
}

protected void startScan(int preference) {
    Intent intent = new Intent(this, ScanActivity.class);
    intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            getContentResolver().delete(uri, null, null);
            scannedImageView.setImageBitmap(bitmap);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private Bitmap convertByteArrayToBitmap(byte[] data) {

    return BitmapFactory.decodeByteArray(data, 0, data.length);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

//----------- OCR PART --------
public void processImage(View view){
    String OCRresult = null;
    mTess.setImage(image);
    OCRresult = mTess.getUTF8Text();
    TextView OCRTextView = (TextView) findViewById(R.id.OCRTextView);
    OCRTextView.setText(OCRresult);
}

private void checkFile(File dir) {
    if (!dir.exists()&& dir.mkdirs()){
        copyFiles();
    }
    if(dir.exists()) {
        String datafilepath = datapath+ "/tessdata/eng.traineddata";
        File datafile = new File(datafilepath);

        if (!datafile.exists()) {
            copyFiles();
        }
    }
}
private void copyFiles() {
    try {
        String filepath = datapath + "/tessdata/eng.traineddata";
        AssetManager assetManager = getAssets();

        InputStream instream = assetManager.open("tessdata/eng.traineddata");
        OutputStream outstream = new FileOutputStream(filepath);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = instream.read(buffer)) != -1) {
            outstream.write(buffer, 0, read);
        }


        outstream.flush();
        outstream.close();
        instream.close();

        File file = new File(filepath);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//-----------------------------

}

任何人都可以帮助我... 提前谢谢。

PS.如果建议是一步一步的话会很好。因为我以前没有太多关于android-studio的经验。即使我努力学习它,但在某些情况下我仍然不理解。

1 个答案:

答案 0 :(得分:0)

这可能与解决scanlibray项目和应用程序之间的方法有关。下面介绍如何导入现有项目并使用其类。

给这一点。

settings.gradle下的scanlibrary文件中,将include ':app', ':scanlibrary'更改为include ':scanlibrary', ':app'

然后,在您的app项目中,打开build.gradle,在依赖项下添加complie project (':scanlibrary')