Всем привет! Сделал приложение с TextRecognizer. Приложение прекрасно работает и определяет текст на Xiaomi Redmi4. На Asus zenfone max pro m1 камера запускается, но текст при нажатии кнопки не определяется. Предполагаю, проблема в том, что в приложении используется Camera (deprecated), а не Camera2. Пожалуйста, помогите разобраться.
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.malgindenis...........">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera"
android:required="true"/>
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".StartActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr"></meta-data>
</application>
</manifest>
StartActivity
public class StartActivity extends AppCompatActivity {
private boolean start = false;
private final int MY_PERMISSIONS_REQUEST_CAMERA = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_activity);
header = (TextView) findViewById(R.id.header);
button = (Button) findViewById(R.id.button2);
animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
button.startAnimation(animation);
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{ // разрешение не предоставлено
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
} else {
// разрешение предоставлено
start =true;
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
start = true;
}
else {
//Toast.makeText(StartActivity.this, "Camera Not detecting", Toast.LENGTH_LONG).show();
showPermissionDialog(StartActivity.this);
}
return;
}
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Camera camera;
FrameLayout frameLayout;
ShowCamera showCamera;
private boolean flagTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
flagTxt = true;
camera = Camera.open();
showCamera = new ShowCamera(this, camera);
frameLayout.addView(showCamera);
}
public void onClick(View view) {
if (camera == null) {
camera = Camera.open();
showCamera = new ShowCamera(this, camera);
frameLayout.addView(showCamera);
}
camera.takePicture(null,
null, myPictureCallback_JPG);
}
Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback() {
@Override
public void onShutter() {
}
};
Camera.PictureCallback myPictureCallback_RAW = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}
};
Camera.PictureCallback myPictureCallback_JPG = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Bitmap mBitmap;
//Bitmap cropBitmap;
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (mBitmap != null) {
int height = mBitmap.getHeight();
int width = mBitmap.getWidth();
Bitmap cropBitmap = ThumbnailUtils.extractThumbnail(mBitmap, width / 4, height / 8);
if (cropBitmap != null) {
textRcgn(cropBitmap);
cropBitmap.recycle();
cropBitmap = null;
}
}
camera.startPreview();
mBitmap.recycle();
//cropBitmap.recycle();
mBitmap = null;
//cropBitmap = null;
}
};
public String textRcgn(Bitmap bitmap) {
String st = null;
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational()) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
} else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = textRecognizer.detect(frame);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < items.size(); i++) {
TextBlock myItem = items.valueAt(i);
sb.append(myItem.getValue());
}
st = sb.toString();
}
return st;
}
}
ShowCamera
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
Camera camera;
SurfaceHolder holder;
public ShowCamera(Context context, Camera camera) {
super(context);
this.camera = camera;
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (holder != null) {
Camera.Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
Camera.Size mSize = null;
for (Camera.Size size : sizes) {
mSize = size;
}
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
params.set("orientation", "portrait");
camera.setDisplayOrientation(90);
params.setRotation(90);
} else {
params.set("orientation", "landscape");
camera.setDisplayOrientation(0);
params.setRotation(0);
}
params.setPictureSize(mSize.width, mSize.height);
if (params.getMaxNumFocusAreas() > 0) {
List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
Rect areaRect1 = new Rect(-100, -60, 100, 60);
meteringAreas.add(new Camera.Area(areaRect1, 900));
params.setFocusAreas(meteringAreas);
}
params.setAntibanding(ANTIBANDING_AUTO);
params.setJpegThumbnailQuality(75);
params.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(params);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
}
Build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.malgindenis............"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//implementation 'com.google.android.gms:play-services-vision:11.0.4'
implementation 'com.google.android.gms:play-services-vision:16.2.0'
// https://mvnrepository.com/artifact/com.google.android.gms/play-services-vision
//compile group: 'com.google.android.gms', name: 'play-services-vision', version: '17.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
При нажатии на кнопку по поиску устройств приложение вылетает вот с таким логом:
подскажите, пожалуйста, в чем проблемаНахожу ORB дискрипторы двух картинок, а потом сравниваю их расстояния
Я пытаюсь создать небольшое приложение, которое будет считывать загрузку ЦП компьютеров / ноутбуков и отправлять push-уведомление в браузер...