when I am developing a Text scanning app I got an issue That is when I am trying to take a snap to scan/recognize the text using the camera it has frozen on my Huawei mobile. But It works fine other android devices Why?
This is logcat Msg Omit the time & Project package name
I/CameraManagerGlobal: Connecting to camera service
I/HwPointEventFilter: do not support AFT because of no config
I/SendBroadcastPermission: action:huawei.intent.action.APS_TOUCH_ACTION_DOWN, mPermissionType:0
I/SendBroadcastPermission: action:huawei.intent.action.APS_TOUCH_ACTION_UP, mPermissionType:0
Manifest file
<activity
android:name=".CameraActivity"
android:screenOrientation="portrait">
</activity>
This is my CameraActivity.java
Omit Imports & Package name
public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
private CameraKitView cameraKitView;
private ImageView btn_capture, btn_flashlight;
private String flashMode = "";
private AlertDialog dialog;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Objects.requireNonNull(getSupportActionBar()).hide();
mContext = this;
cameraKitView = findViewById(R.id.camera);
btn_capture = findViewById(R.id.btn_capture);
btn_flashlight = findViewById(R.id.btn_flashlight);
cameraKitView.requestPermissions(this);
flashMode = SharedHelper.getKey(this, "flashMode");
if (flashMode.isEmpty() || flashMode.equals("off")) {
flashMode = "off";
flash("off");
}
flash(flashMode);
btn_flashlight.setOnClickListener(this);
btn_capture.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.processing);
builder.setView(R.layout.loading_dialoag);
dialog = builder.create();
}
public void flash(String mode) {
switch (mode) {
case "off":
cameraKitView.setFlash(CameraKit.FLASH_OFF);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.ic_flash_off_white_24dp));
break;
case "on":
cameraKitView.setFlash(CameraKit.FLASH_ON);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.ic_flash_on_white_24dp));
break;
case "auto":
cameraKitView.setFlash(CameraKit.FLASH_AUTO);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.ic_flash_auto_white_24dp));
break;
}
}
@Override
protected void onStart() {
super.onStart();
cameraKitView.onStart();
}
@Override
protected void onResume() {
super.onResume();
cameraKitView.onResume();
}
@Override
protected void onPause() {
cameraKitView.onPause();
super.onPause();
}
@Override
protected void onStop() {
cameraKitView.onStop();
super.onStop();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, "You must allow permission to use the camera", Toast.LENGTH_SHORT).show();
finish();
}
cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onClick(View view) {
if (view == btn_capture) {
cameraKitView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, final byte[] capturedImage) {
// capturedImage contains the image from the CameraKitView.
captureSound();
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(capturedImage, 0, capturedImage.length, opts);
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
imageProcessor(image);
dialog.show();
cameraKitView.onStop();
}
});
} else if (view == btn_flashlight) {
switch (flashMode) {
case "off":
flashMode = "on";
flash("on");
SharedHelper.putKey(this, "flashMode", "on");
break;
case "on":
flashMode = "auto";
flash("auto");
SharedHelper.putKey(this, "flashMode", "auto");
break;
case "auto":
flashMode = "off";
flash("off");
SharedHelper.putKey(this, "flashMode", "off");
break;
}
}
}
public void imageProcessor(FirebaseVisionImage image) {
FirebaseVisionDocumentTextRecognizer detector = FirebaseVision.getInstance()
.getCloudDocumentTextRecognizer();
detector.processImage(image)
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionDocumentText>() {
@Override
public void onSuccess(FirebaseVisionDocumentText result) {
dialog.dismiss();
Intent intent = new Intent(CameraActivity.this, TextActivity.class);
intent.putExtra("result", result.getText());
startActivity(intent);
overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
finish();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Processing Failed");
builder.setMessage(e.getLocalizedMessage());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cameraKitView.onStart();
cameraKitView.onResume();
}
}).show();
}
});
}
public void captureSound(){
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
MediaActionSound sound = new MediaActionSound();
sound.play(MediaActionSound.SHUTTER_CLICK);
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
break;
}
}
}
I tried to run the code you provided on a Huawei phone. When the app was started for the first time, the camera did not respond, which I think is because the app does not have the permission to access the camera.
According to reference documents on the official website of Camera Engine, I don't think you need to call cameraKitView.requestPermissions(this). After I deleted the line that calls this method, the app runs properly after the first startup and is able to apply for camera permission dynamically.
Besides, I noticed that you integrated ML Kit of Firebase. According to Firebase's reference documents, Google Play is required for com.google.firebase:firebase-ml-vision:24.1.0. Otherwise, the Kit cannot work properly.
As GMS is not available to Huawei phones launched after the Google ban, your phone may do not support Google Play. That's why your app cannot work properly. You can integrate HUAWEI ML Kit, which works properly on all Android devices. HUAWEI ML Kit supports such functions as text recognition and document recognition. For details, please refer to Integration Documents.You are advised to use HMS Toolkit, which helps you quickly add the HMS+GMS adaptation layer code and upgrade your logic code to support both GMS and HMS.
I have made the following changes to the code for your reference: The modifications are as follows:
public class CameraActivity extends AppCompatActivity implements View.OnClickListener {
private CameraKitView cameraKitView;
private ImageView btn_capture, btn_flashlight;
private String flashMode = "";
private AlertDialog dialog;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Objects.requireNonNull(getSupportActionBar()).hide();
mContext = this;
cameraKitView = findViewById(R.id.camera);
btn_capture = findViewById(R.id.btn_capture);
btn_flashlight = findViewById(R.id.btn_flashlight);
// cameraKitView.requestPermissions(this);
flashMode = SharedHelper.getKey(this, "flashMode");
if (flashMode.isEmpty() || flashMode.equals("off")) {
flashMode = "off";
flash("off");
}
flash(flashMode);
btn_flashlight.setOnClickListener(this);
btn_capture.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.processing);
builder.setView(R.layout.loading_dialoag);
dialog = builder.create();
}
public void flash(String mode) {
switch (mode) {
case "off":
cameraKitView.setFlash(CameraKit.FLASH_OFF);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.flashlight_off));
break;
case "on":
cameraKitView.setFlash(CameraKit.FLASH_ON);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.flashlight_on));
break;
case "auto":
cameraKitView.setFlash(CameraKit.FLASH_AUTO);
btn_flashlight.setImageDrawable(getDrawable(R.drawable.flashlight_auto));
break;
}
}
@Override
protected void onStart() {
super.onStart();
cameraKitView.onStart();
}
@Override
protected void onResume() {
super.onResume();
cameraKitView.onResume();
}
@Override
protected void onPause() {
cameraKitView.onPause();
super.onPause();
}
@Override
protected void onStop() {
cameraKitView.onStop();
super.onStop();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, "You must allow permission to use the camera", Toast.LENGTH_SHORT).show();
finish();
}
cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onClick(View view) {
if (view == btn_capture) {
cameraKitView.captureImage(new CameraKitView.ImageCallback() {
@Override
public void onImage(CameraKitView cameraKitView, final byte[] capturedImage) {
// capturedImage contains the image from the CameraKitView.
captureSound();
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeByteArray(capturedImage, 0, capturedImage.length, opts);
// FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
MLFrame frame = MLFrame.fromBitmap(bitmap);
imageProcessor(frame);
dialog.show();
cameraKitView.onStop();
}
});
} else if (view == btn_flashlight) {
switch (flashMode) {
case "off":
flashMode = "on";
flash("on");
SharedHelper.putKey(this, "flashMode", "on");
break;
case "on":
flashMode = "auto";
flash("auto");
SharedHelper.putKey(this, "flashMode", "auto");
break;
case "auto":
flashMode = "off";
flash("off");
SharedHelper.putKey(this, "flashMode", "off");
break;
}
}
}
private void imageProcessor(MLFrame frame) {
MLTextAnalyzer analyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer();
Task<MLText> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLText>() {
@Override
public void onSuccess(MLText text) {
Toast.makeText(CameraActivity.this, text.getStringValue(), Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(CameraActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void captureSound(){
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
MediaActionSound sound = new MediaActionSound();
sound.play(MediaActionSound.SHUTTER_CLICK);
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
break;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout tools:context=".CameraActivity" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<com.camerakit.CameraKitView android:layout_height="0dp" android:layout_width="match_parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/btn_capture" app:camera_permissions="camera" app:camera_focus="continuous" app:camera_flash="auto" app:camera_facing="back" android:keepScreenOn="true" android:adjustViewBounds="true" android:id="@+id/camera"> </com.camerakit.CameraKitView>
<ImageView android:layout_height="50dp" android:layout_width="match_parent" app:layout_constraintBottom_toTopOf="@id/btn_flashlight" android:id="@+id/btn_capture" android:src="@drawable/take_photos"/>
<ImageView android:layout_height="50dp" android:layout_width="match_parent" android:id="@+id/btn_flashlight" android:src="@drawable/flashlight_off" app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>