jenly1314 / MLKit

🌝 MLKit是一个强大易用的工具包。通过ML Kit您可以很轻松的实现文字识别、条码识别、图像标记、人脸检测、对象检测等功能。
https://jenly1314.github.io/MLKit/
Apache License 2.0
892 stars 160 forks source link

Android 14 beta版本 androidx.camera.view.PreviewView有点问题 #34

Closed lr555 closed 1 year ago

lr555 commented 1 year ago

用MLKit 1.4.0,在pixel 6 pro,Android 14 beta版本,显示有问题,PreviewView获取的bitmap只覆盖了一部分内容。 以下是版本信息和视频: image screen-20230602-194349_074457_AdobeExpress 不清楚是android 14适配还是beta版本的问题

jenly1314 commented 1 year ago

布局加个android:scaleType="centerCrop"试试

    <ImageView
        android:id="@+id/ivResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
lr555 commented 1 year ago

布局加个android:scaleType="centerCrop"试试

    <ImageView
        android:id="@+id/ivResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

加了centerCrop,还是同样的问题。我把previewView.bitmap通过alertDialog显示出来,顶部不知道为啥没有(红框是我手动加的,和bitmap无关),之前用Android 13测是没问题的,用的是MLKit/app例子下的QRCodeScanningActivity, Screenshot_20230605_150008_副本 不过Android 14还没正式发,可能是beta版本的问题

jenly1314 commented 1 year ago

布局加个android:scaleType="centerCrop"试试

    <ImageView
        android:id="@+id/ivResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

加了centerCrop,还是同样的问题。我把previewView.bitmap通过alertDialog显示出来,顶部不知道为啥没有(红框是我手动加的,和bitmap无关),之前用Android 13测是没问题的,用的是MLKit/app例子下的QRCodeScanningActivity, Screenshot_20230605_150008_副本 不过Android 14还没正式发,可能是beta版本的问题

获取相机预览图还能有一部分是透明的?头一次听说,感觉这不太科学;短时间内我估计没机会体验 Android 14 的真机; 如果你有兴趣分析原因的话,可以继续深挖:在扫描回调方法onScanResultCallback 中获取result.bitmap,将previewView.bitmapresult.bitmap都保存到本地,然后查看对比图片的宽高,并确认图片是否有透明部分。

lr555 commented 1 year ago

在扫描回调方法onScanResultCallback 中获取result.bitmap,将previewView.bitmap与result.bitmap都保存到本地, result.bitmap为 result

previewView.bitmap为 previewView

jenly1314 commented 1 year ago

这样看来,previewView获取bitmap可能确实有点问题,不过previewView默认使用的是 SufaceView 来显示的,还有个备选方案:你可以改成 使用 TexttureView 来显示试试,然后再获取previewView.bitmap看看是否还存在只有一部分的情况。

如果你是直接或间接继承的BaseCameraScanActivity来实现的扫码,可以在 initCameraScan 方法中进行如下设置:

// 使用TexttureView预览显示
previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);
lr555 commented 1 year ago

这样看来,previewView获取bitmap可能确实有点问题,不过previewView默认使用的是 SufaceView 来显示的,还有个备选方案:你可以改成 使用 TexttureView 来显示试试,然后再获取previewView.bitmap看看是否还存在只有一部分的情况。

如果你是直接或间接继承的BaseCameraScanActivity来实现的扫码,可以在 initCameraScan 方法中进行如下设置:

// 使用TexttureView预览显示
previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);

可以了,现在显示正常,666 ♪(・ω・)ノ