Android Xamarin-Tesseract实时检测
我试图在Xamarin.Forms中使用tesseract对图像进行文本字符识别。在Android上,我使用以下步骤实现了逻辑Android Xamarin-Tesseract实时检测,android,xamarin,xamarin.forms,ocr,tesseract,Android,Xamarin,Xamarin.forms,Ocr,Tesseract,我试图在Xamarin.Forms中使用tesseract对图像进行文本字符识别。在Android上,我使用以下步骤实现了逻辑 抓取TextureView的位图 将位图转换为黑白 需要从位图图像进行裁剪 使用裁剪图像设置细分图像 目前面临内存问题,目前还没有解决办法 下面的代码片段显示了代码的主要功能 public async void takeContinuousPhotos(){ byte[] newBytes; byte[] bytes;
public async void takeContinuousPhotos(){
byte[] newBytes;
byte[] bytes;
while (ocrTextChosen == false){
bytes = await CapturePhoto();
newBytes = this.CropPhoto(bytes, new System.Drawing.Rectangle(40, view.Height / 2 - 200, view.Width - 40, 100), (view.Width - 40) * 2, 200);
if (!_tesseract.Initialized)
{
await _tesseract.Init("eng");
var result = await _tesseract.SetImage(new MemoryStream(newBytes));
if (result)
{
Device.BeginInvokeOnMainThread(() => {
this.ocrText.Text = this._tesseract.Text;
});
_tesseract.Clear();
bytes = new byte[0];
newBytes = new byte[0];
}
}
else {
var result = await _tesseract.SetImage(new MemoryStream(newBytes));
if (result)
{
Device.BeginInvokeOnMainThread(() => {
this.ocrText.Text = this._tesseract.Text;
});
_tesseract.Clear();
bytes = new byte[0];
newBytes = new byte[0];
}
}
}
}
// Capture photo from stream
public async Task<byte[]> CapturePhoto()
{
var ratio = ((decimal)Height) / Width;
var blackwhiteBitmap = this.toGrayscale(liveView.Bitmap);
var image = Bitmap.CreateBitmap(blackwhiteBitmap, 0, 0, blackwhiteBitmap.Width, (int)(blackwhiteBitmap.Width * ratio));
byte[] imageBytes = null;
using (var imageStream = new System.IO.MemoryStream())
{
await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 10, imageStream);
image.Recycle();
imageBytes = imageStream.ToArray();
imageStream.Dispose();
}
image = null;
return imageBytes;
}
你有什么样的项目吗?Github?你有什么样的项目吗?github?
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
camera = Android.Hardware.Camera.Open();
var parameters = camera.GetParameters();
var aspect = ((decimal)height) / ((decimal)width);
var previewSize = parameters.SupportedPreviewSizes
.OrderBy(s => System.Math.Abs(s.Width / (decimal)s.Height - aspect))
.First();
parameters.SetPreviewSize(previewSize.Width, previewSize.Height);
parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeContinuousPicture;
camera.SetParameters(parameters);
camera.SetPreviewTexture(surface);
StartCamera();
Task.Run(() => {
this.takeContinuousPhotos();
});
}