我正在使用android上的OCR进行文本识别。我写的代码完全没有错误,它也成功安装和创建了APK,但我不知道问题出在哪里,它停止了启动活动。它甚至没有显示任何关于它为什么不运行的错误,如果你有任何解决这个问题的方法,或者如果你可以在android studio上运行并测试它并找出错误。下面是我的代码和stacktrace结果。谢谢。
package com.example.nadeemahmad.smartcalculator;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;
import java.io.IOException;
public class cam_calculator extends Activity {
Button back_frm_came;
public static final int CAMERA_PERMISSION = 100;
private SurfaceView cameraView;
private TextView textBlockContent;
private CameraSource cameraSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cam_calculator);
back_frm_came = (Button) findViewById(R.id.back_frm_came);
cameraView = (SurfaceView) findViewById(R.id.surface_view);
textBlockContent = (TextView) findViewById(R.id.cam_txt_read);
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational()) {
Log.w("MainActivity", "Detector dependencies are not yet available.");
}
cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(2.0f)
.setAutoFocusEnabled(true)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
//noinspection MissingPermission
cameraSource.start(cameraView.getHolder());
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
Log.d("Main", "receiveDetections");
final SparseArray<TextBlock> items = detections.getDetectedItems();
if (items.size() != 0) {
textBlockContent.post(new Runnable() {
@Override
public void run() {
StringBuilder value = new StringBuilder();
for (int i = 0; i < items.size(); ++i) {
TextBlock item = items.valueAt(i);
value.append(item.getValue());
value.append("\n");
}
//update text block content to TextView
textBlockContent.setText(value.toString());
}
});
}
}
});
back_frm_came.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {cam_calculator.this.finish();
}
});
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION);
cam_calculator.this.finish();
} else {
cam_calculator.this.finish();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case CAMERA_PERMISSION:{
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Permission Denied !", Toast.LENGTH_SHORT).show();
finish();
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
cameraSource.release();
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nadeemahmad.smartcalculator">
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="ocr" />
</application>
</manifest>
堆栈跟踪
08-07 10:00:28.655 15603-15603/? E/Zygote: v2
08-07 10:00:28.655 15603-15603/? I/libpersona: KNOX_SDCARD checking this for 10256
08-07 10:00:28.655 15603-15603/? I/libpersona: KNOX_SDCARD not a persona
08-07 10:00:28.657 15603-15603/? E/Zygote: accessInfo : 0
08-07 10:00:28.658 15603-15603/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -1 -1 0 1]
08-07 10:00:28.660 15603-15603/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.nadeemahmad.smartcalculator
08-07 10:00:28.665 15603-15603/? I/art: Late-enabling -Xcheck:jni
08-07 10:00:28.778 15603-15603/com.example.nadeemahmad.smartcalculator W/System: ClassLoader referenced unknown path: /data/app/com.example.nadeemahmad.smartcalculator-1/lib/arm64
08-07 10:00:28.794 15603-15603/com.example.nadeemahmad.smartcalculator I/InstantRun: Instant Run Runtime started. Android package is com.example.nadeemahmad.smartcalculator, real application class is null.
08-07 10:00:30.055 15603-15603/com.example.nadeemahmad.smartcalculator W/System: ClassLoader referenced unknown path: /data/app/com.example.nadeemahmad.smartcalculator-1/lib/arm64
08-07 10:00:30.429 15603-15793/com.example.nadeemahmad.smartcalculator I/OpenGLRenderer: Initialized EGL, version 1.4
08-07 10:00:32.212 15603-15603/com.example.nadeemahmad.smartcalculator I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
08-07 10:00:33.046 15603-15603/com.example.nadeemahmad.smartcalculator W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
08-07 10:00:33.047 15603-15603/com.example.nadeemahmad.smartcalculator E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
08-07 10:00:34.118 15603-15603/com.example.nadeemahmad.smartcalculator W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
08-07 10:00:34.129 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1101
08-07 10:00:34.129 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1101
08-07 10:00:34.140 15603-15603/com.example.nadeemahmad.smartcalculator W/System: ClassLoader referenced unknown path:
08-07 10:00:34.163 15603-15603/com.example.nadeemahmad.smartcalculator W/System: ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/0000000c/n/arm64-v8a
08-07 10:00:34.235 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Loading library libocr.so
08-07 10:00:34.243 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: libocr.so library load status: true
08-07 10:00:34.244 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Reading ocr models from /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models
08-07 10:00:34.245 15603-15603/com.example.nadeemahmad.smartcalculator I/native: jni_helper.cc:76 Loading model config /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models/mognet_lstm_engine_0.7.bincfg
08-07 10:00:34.247 15603-15603/com.example.nadeemahmad.smartcalculator I/native: timer.h:126 PhotoOcrEngine::Init (detector): 0.215 ms (elapsed)
08-07 10:00:34.253 15603-15603/com.example.nadeemahmad.smartcalculator I/native: mognet_classifier_client.cc:1810 Using full specialization.
08-07 10:00:34.254 15603-15603/com.example.nadeemahmad.smartcalculator I/native: timer.h:126 PhotoOcrEngine::Init (classifier): 6.963 ms (elapsed)
08-07 10:00:34.281 15603-15603/com.example.nadeemahmad.smartcalculator I/native: timer.h:126 PhotoOcrEngine::Init (recognizer): 27.017 ms (elapsed)
08-07 10:00:34.281 15603-15603/com.example.nadeemahmad.smartcalculator I/native: timer.h:126 void ocr::photo::PhotoOcrEngine::Init(): 34.511 ms (elapsed)
08-07 10:00:36.481 15603-15603/com.example.nadeemahmad.smartcalculator W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
08-07 10:00:36.491 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1101
08-07 10:00:36.491 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1101
08-07 10:00:36.499 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Loading library libocr.so
08-07 10:00:36.500 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: libocr.so library load status: true
08-07 10:00:36.500 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Reading ocr models from /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models
08-07 10:00:37.619 15603-15603/com.example.nadeemahmad.smartcalculator I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
08-07 10:00:38.473 15603-15603/com.example.nadeemahmad.smartcalculator I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
08-07 10:00:39.869 15603-15603/com.example.nadeemahmad.smartcalculator W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
08-07 10:00:39.872 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1101
08-07 10:00:39.872 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1101
08-07 10:00:39.877 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Loading library libocr.so
08-07 10:00:39.877 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: libocr.so library load status: true
08-07 10:00:39.878 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Reading ocr models from /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models
08-07 10:00:50.454 15603-15603/com.example.nadeemahmad.smartcalculator W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
08-07 10:00:50.460 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1101
08-07 10:00:50.460 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1101
08-07 10:00:50.465 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Loading library libocr.so
08-07 10:00:50.465 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: libocr.so library load status: true
08-07 10:00:50.465 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Reading ocr models from /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models
08-07 10:02:32.208 15603-15603/com.example.nadeemahmad.smartcalculator W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
08-07 10:02:32.213 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1101
08-07 10:02:32.213 15603-15603/com.example.nadeemahmad.smartcalculator I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1101
08-07 10:02:32.218 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Loading library libocr.so
08-07 10:02:32.218 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: libocr.so library load status: true
08-07 10:02:32.218 15603-15603/com.example.nadeemahmad.smartcalculator I/Vision: Reading ocr models from /data/user/0/com.google.android.gms/files/com.google.android.gms.vision/ocr/data/models
Gradle依赖
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.nadeemahmad.smartcalculator"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:design:22.2.0'
compile group: 'com.fathzer', name: 'javaluator', version: '3.0.0'
compile 'com.google.android.gms:play-services-vision:11.0.2'
testCompile 'junit:junit:4.12'
}