I/HwPointEventFilter: 不支持 AFT 因为没有配置

I/HwPointEventFilter: do not support AFT because of no config

当我开发一个文本扫描应用程序时,我遇到了一个问题,那就是当我试图拍摄 scan/recognize 文本时,它已冻结在我的华为手机上。但它在其他 android 设备上运行良好 为什么?

这是logcat消息 省略时间&项目包名

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

清单文件

  <activity
        android:name=".CameraActivity"
        android:screenOrientation="portrait">
 </activity>

这是我的CameraActivity.java

省略导入和包名称

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;
        }
    }
}

我试图 运行 您在华为 phone 上提供的代码。第一次启动app时,相机没有反应,我认为是因为app没有访问相机的权限。

根据Camera Engine官网的参考文档,我认为不需要调用cameraKitView.requestPermissions(this)。删除调用该方法的那一行后,应用运行第一次启动正常,可以动态申请摄像头权限


此外,我注意到您集成了 Firebase 的 ML Kit。根据Firebase's reference documents,Google播放需要com.google.firebase:firebase-ml-vision:24.1.0。否则无法正常使用。

由于 Google 禁令后推出的华为 phone 无法使用 GMS,因此您的 phone 可能不支持 Google Play。这就是您的应用无法正常运行的原因。您可以集成 HUAWEI ML Kit,它可以在所有 Android 设备上正常工作。 HUAWEI ML Kit支持文本识别、文档识别等功能。详情请参考Integration Documents.You are advised to use HMS Toolkit,帮助您快速添加HMS+GMS适配层代码,升级您的逻辑代码,同时支持GMS和HMS。

我对代码做了如下修改,供大家参考:修改如下:

  1. 将下面一行注释掉,解决相机无响应问题。 cameraKitView.requestPermissions(这个);
  2. 将配置firebase-ml-vision的行替换为配置HUAWEI ML Kit的行,以便您的应用程序可以支持文本识别。
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>