我从以下位置下载了谷歌移动视觉示例:https://components.xamarin.com/view/googleplayservices-vision
并且使用演示条形码,面部检测等的样本没有问题,相机的预览在照明等方面看起来不错。 他们的示例不使用 PCL,仅使用 android 项目中定义的相机屏幕。 所以我在我的 PCL 中创建了一个基本的 ViewRender(这里没有什么特别的事情(
public class CameraScanView : View
{
public CameraScanView() { }
}
并将他们的示例中的 android 活动更改为继承 ViewRender,并将他们的代码从 OnCreate 移动到 OnElementChanged,如下所示:
[assembly: ExportRenderer(typeof(CameraScanView), typeof(BarcodeScannerActivity))]
namespace VisionSample
{
public class BarcodeScannerActivity : ViewRenderer
{
bool _isViewLoaded = false;
QRCodeScanView _qrCodeScanView;
CameraSource _cameraSource;
CameraSourcePreview _preview;
GraphicOverlay _graphicOverlay;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (e.OldElement == null && e.NewElement != null)
{
if (!_isViewLoaded)
{
_isViewLoaded = true;
_qrCodeScanView = (QRCodeScanView)e.NewElement;
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
var view = inflater.Inflate(Resource.Layout.FaceTracker, null, false);
_preview = view.FindViewById<CameraSourcePreview>(Resource.Id.preview);
_graphicOverlay = view.FindViewById<GraphicOverlay>(Resource.Id.faceOverlay);
var detector = new BarcodeDetector.Builder(Android.App.Application.Context).SetBarcodeFormats(BarcodeFormat.QrCode).Build();
GraphicBarcodeTrackerFactory barcodeFactory = new GraphicBarcodeTrackerFactory(_graphicOverlay);
// result.ResultReceived += Result_ResultReceived;
detector.SetProcessor(new MultiProcessor.Builder(barcodeFactory).Build());
if (!detector.IsOperational)
{
// Note: The first time that an app using barcode API is installed on a device, GMS will
// download a native library to the device in order to do detection. Usually this
// completes before the app is run for the first time. But if that download has not yet
// completed, then the above call will not detect any barcodes.
//
// IsOperational can be used to check if the required native library is currently
// available. The detector will automatically become operational once the library
// download completes on device.
// Log.Warn("SCAN", "Barcode detector dependencies are not yet available.");
}
_cameraSource = new CameraSource.Builder(Android.App.Application.Context, detector)
.SetRequestedPreviewSize(1600, 1024)
.SetFacing(CameraFacing.Back)
.SetRequestedFps(30.0f)
.SetAutoFocusEnabled(true)
.Build();
StartCameraSource();
_preview.RemoveFromParent();
SetNativeControl(_preview);
}
}
}
/*
protected override void OnResume ()
{
base.OnResume ();
StartCameraSource ();
}
protected override void OnPause ()
{
base.OnPause ();
mPreview.Stop ();
}
protected override void OnDestroy ()
{
mCameraSource.Release ();
base.OnDestroy ();
}*/
//==============================================================================================
// Camera Source Preview
//==============================================================================================
/**
* Starts or restarts the camera source, if it exists. If the camera source doesn't exist yet
* (e.g., because onResume was called before the camera source was created), this will be called
* again when the camera source is created.
*/
void StartCameraSource ()
{
try {
_preview.Start (_cameraSource, _graphicOverlay);
} catch (Exception e) {
Android.Util.Log.Error (TAG, "Unable to start camera source.", e);
_cameraSource.Release ();
_cameraSource = null;
}
}
//==============================================================================================
// Graphic Face Tracker
//==============================================================================================
/**
* Factory for creating a face tracker to be associated with a new face. The multiprocessor
* uses this factory to create face trackers as needed -- one for each individual.
*/
class GraphicBarcodeTrackerFactory : Java.Lang.Object, MultiProcessor.IFactory
{
public GraphicBarcodeTrackerFactory (GraphicOverlay overlay) : base ()
{
Overlay = overlay;
}
public GraphicOverlay Overlay { get; private set; }
public Android.Gms.Vision.Tracker Create (Java.Lang.Object item)
{
return new GraphicBarcodeTracker (Overlay);
}
}
/**
* Face tracker for each detected individual. This maintains a face graphic within the app's
* associated face overlay.
*/
class GraphicBarcodeTracker : Tracker
{
GraphicOverlay mOverlay;
BarcodeGraphic mBarcodeGraphic;
public GraphicBarcodeTracker (GraphicOverlay overlay)
{
mOverlay = overlay;
mBarcodeGraphic = new BarcodeGraphic (overlay);
}
/**
* Start tracking the detected face instance within the face overlay.
*/
public override void OnNewItem (int idValue, Java.Lang.Object item)
{
mBarcodeGraphic.Id = idValue;
}
/**
* Update the position/characteristics of the face within the overlay.
*/
public override void OnUpdate (Detector.Detections detections, Java.Lang.Object item)
{
mOverlay.Add (mBarcodeGraphic);
mBarcodeGraphic.UpdateBarcode (item.JavaCast<Barcode> ());
}
/**
* Hide the graphic when the corresponding face was not detected. This can happen for
* intermediate frames temporarily (e.g., if the face was momentarily blocked from
* view).
*/
public override void OnMissing (Detector.Detections detections)
{
mOverlay.Remove (mBarcodeGraphic);
}
/**
* Called when the face is assumed to be gone for good. Remove the graphic annotation from
* the overlay.
*/
public override void OnDone ()
{
mOverlay.Remove (mBarcodeGraphic);
}
}
}
结果是现在预览屏幕更暗了.. im使用谷歌像素XL(供参考(。 谁能向我解释为什么简单地将自定义渲染器添加到示例中会导致预览屏幕变暗?
我看到其他人说要降低帧率,但这对这里的这个问题没有影响。
这个问题与我的代码无关。
我的主要安卓风格.xml有以下几行:
<item name="android:backgroundDimEnabled">true</item>
相机预览使用了表面视图,即使屏幕的其余部分没有,它显然也会变暗。 我的解决方案是将其设置为 false