当前位置: 首页 > news >正文

沈阳推广网站关键词林俊杰百度云

沈阳推广网站,关键词林俊杰百度云,重庆彼客的网站是谁家做的,建设新闻网站需要注意什么OCR是一个很常用的功能,Google ML Kit提供了OCR能力,用起来也很简单,本文介绍一下使用方法。 1. 相关概念 名词概念解释TextBlock块一个段落Line行一行文本Element元素单词;对汉字来说,类似"开头 (分隔符)中间&…

OCR是一个很常用的功能,Google ML Kit提供了OCR能力,用起来也很简单,本文介绍一下使用方法。

1. 相关概念

名词概念解释
TextBlock一个段落
Line一行文本
Element元素单词;对汉字来说,类似"开头 (分隔符)中间(分隔符) 结尾"这样含有明显分隔符的才会有多个字在一个Element中,否则就是单个字
Symbol字符字母;对汉字来说就是单个字

2. 代码实现

在build.gradle中添加相关依赖:

// To recognize Chinese script
implementation 'com.google.mlkit:text-recognition-chinese:16.0.1'

添加布局文件activity_ocr.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><SurfaceViewandroid:id="@+id/camera_preview"android:layout_width="wrap_content"android:layout_height="wrap_content" /><com.example.study.views.DrawViewandroid:id="@+id/ocr_area"android:layout_width="wrap_content"android:layout_height="wrap_content" /></FrameLayout><Buttonandroid:id="@+id/ocr_switch"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center_horizontal|bottom"android:layout_marginBottom="80dp"android:background="@color/夏云灰"android:text="stop" />
</LinearLayout>

绘制文字的OCRDrawView.java:

package com.example.study.views;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;import androidx.annotation.Nullable;import java.util.ArrayList;
import java.util.List;public class OCRDrawView extends View {private final Object lock = new Object();protected Paint paint = new Paint();protected Path path = new Path();private final List<ShapeInfo> cornerPointsList = new ArrayList<>();public OCRDrawView(Context context) {super(context);}public OCRDrawView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public void clear() {synchronized (lock) {cornerPointsList.clear();}postInvalidate();}public void add(Point[] cornerPoints, String text) {synchronized (lock) {cornerPointsList.add(new ShapeInfo(cornerPoints, text));}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);synchronized (lock) {for (ShapeInfo shapeInfo : cornerPointsList) {drawBackground(shapeInfo, canvas);drawText(shapeInfo, canvas);}}}private void drawText(ShapeInfo shapeInfo, Canvas canvas) {Point[] points = shapeInfo.points;// 根据矩形区域的高度设置文字大小double height = calDistance(points[0], points[3]);double width = calDistance(points[2], points[3]);float textSize = (float) Math.min(height, width / shapeInfo.text.length());paint.setColor(Color.BLUE);paint.setTextSize(textSize);path.reset();path.moveTo(points[3].x, points[3].y);path.lineTo(points[2].x, points[2].y);canvas.drawTextOnPath(shapeInfo.text, path, 0, 0, paint);}private double calDistance(Point start, Point end) {return Math.sqrt(Math.pow(start.x - end.x, 2) + Math.pow(start.y - end.y, 2));}private void drawBackground(ShapeInfo shapeInfo, Canvas canvas) {Point[] shape = shapeInfo.points;path.reset();path.moveTo(shape[3].x, shape[3].y);for (int i = 0; i < shape.length; i++) {path.lineTo(shape[i].x, shape[i].y);}path.close();paint.setColor(Color.WHITE);canvas.drawPath(path, paint);}static class ShapeInfo {Point[] points;String text;public ShapeInfo(Point[] shape, String text) {this.points = shape;this.text = text;}}
}

activity类:

package com.example.study.activities;import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;import com.example.study.R;
import com.example.study.views.OCRDrawView;
import com.google.mlkit.vision.text.Text;
import com.google.mlkit.vision.text.TextRecognition;
import com.google.mlkit.vision.text.TextRecognizer;
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions;import java.io.ByteArrayOutputStream;public class OCRActivity extends ComponentActivity implements Camera.PreviewCallback, SurfaceHolder.Callback {private static final String TAG = "CameraDemoActivity";private static final int REQUEST_CAMERA = 1000;private static final int HEIGHT = 1920;private static final int WIDTH = 1080;private static final int ORIENTATION = 90;private SurfaceView preview;private OCRDrawView ocrArea;private Button ocrSwitch;private Camera camera;private Camera.Parameters parameters;private TextRecognizer recognizer;private Matrix matrix;private boolean isRecognizering = false;private boolean stopRecognizer = false;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_ocr);initView();initVar();// 检查权限if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);} else {preview.getHolder().addCallback(this);}}private void initVar() {recognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build());matrix = new Matrix();matrix.setRotate(ORIENTATION);// 4个角的坐标是没有旋转过的,所以HEIGHT、WIDTH是反的matrix.preTranslate(-HEIGHT >> 1, -WIDTH >> 1);}private void initView() {preview = findViewById(R.id.camera_preview);ocrArea = findViewById(R.id.ocr_area);ocrSwitch = findViewById(R.id.ocr_switch);ocrSwitch.setOnClickListener(view -> {stopRecognizer = !stopRecognizer;ocrSwitch.setText(stopRecognizer ? "start" : "stop");if (camera == null) {return;}if (stopRecognizer) {camera.stopPreview();} else {camera.startPreview();}});adjustSurface(preview, ocrArea);}private void adjustSurface(SurfaceView cameraPreview, OCRDrawView ocrArea) {FrameLayout.LayoutParams cameraPreviewParams = (FrameLayout.LayoutParams) cameraPreview.getLayoutParams();cameraPreviewParams.width = WIDTH;cameraPreviewParams.height = HEIGHT;ViewGroup.LayoutParams ocrAreaParams = ocrArea.getLayoutParams();ocrAreaParams.width = WIDTH;ocrAreaParams.height = HEIGHT;}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);if (requestCode == REQUEST_CAMERA && grantResults.length > 0) {if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {preview.getHolder().addCallback(this);surfaceCreated(preview.getHolder());camera.setPreviewCallback(this);camera.startPreview();} else {finish();}}}@Overridepublic void onPreviewFrame(byte[] data, Camera camera) {if (isRecognizering || stopRecognizer) {return;}Bitmap bitmap = convertToBitmap(camera, data);isRecognizering = true;recognizer.process(bitmap, ORIENTATION).addOnSuccessListener(text -> {parseOCRResult(text);}).addOnFailureListener(exception -> {Toast.makeText(this, "Failure", Toast.LENGTH_SHORT).show();isRecognizering = false;}).addOnCompleteListener(task -> {isRecognizering = false;}).addOnCanceledListener(() -> {Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();isRecognizering = false;});}private void parseOCRResult(Text text) {// 所有识别到的内容,下同String textContent = text.getText();if (textContent == null || textContent.trim().length() == 0) {return;}ocrArea.clear();// 块,段落for (Text.TextBlock textBlock : text.getTextBlocks()) {// 一行文本for (Text.Line line : textBlock.getLines()) {drawResult(line);// 元素:单词,对汉字来说,需要"开头 (分隔符)中间(分隔符) 结尾"之类比较强烈的分隔符去分隔for (Text.Element element : line.getElements()) {// symbol:字符,字母,字for (Text.Symbol symbol : element.getSymbols()) {symbol.getText();}}}}}private void drawResult(Text.Line line) {// line的旋转角度(以度为单位,顺时针为正,范围为[-180, 180])float angle = line.getAngle() + ORIENTATION;// 检测到的文本的轴对齐边界矩形Rect boundingBox = line.getBoundingBox();// 从左上角开始顺时针方向的四个角点。不带旋转角度,如果设置过旋转角度camera.setDisplayOrientation,需要进行旋转Point[] cornerPoints = line.getCornerPoints();// 置信度float confidence = line.getConfidence();// 获取文本中的主要语言(如果有的话)String recognizedLanguage = line.getRecognizedLanguage();// 置信度太低的过滤掉if (confidence < 0.3f) {return;}for (Point cornerPoint : cornerPoints) {float[] floats = {cornerPoint.x, cornerPoint.y};matrix.mapPoints(floats);cornerPoint.x = (int) floats[0] + (WIDTH >> 1);cornerPoint.y = (int) floats[1] + (HEIGHT >> 1);}ocrArea.add(cornerPoints, line.getText());ocrArea.postInvalidate();}/*** Convert camera data into bitmap data.*/private Bitmap convertToBitmap(Camera camera, byte[] data) {int width = camera.getParameters().getPreviewSize().width;int height = camera.getParameters().getPreviewSize().height;YuvImage yuv = new YuvImage(data, ImageFormat.NV21, width, height, null);ByteArrayOutputStream stream = new ByteArrayOutputStream();yuv.compressToJpeg(new Rect(0, 0, width, height), 100, stream);return BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.toByteArray().length);}@Overrideprotected void onResume() {super.onResume();}@Overrideprotected void onRestart() {super.onRestart();}@Overrideprotected void onDestroy() {super.onDestroy();if (recognizer != null) {recognizer.close();}}@Overridepublic void surfaceCreated(@NonNull SurfaceHolder holder) {try {camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);parameters = camera.getParameters();// 旋转了90度,所以height、width互换parameters.setPictureSize(HEIGHT, WIDTH);parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);parameters.setPictureFormat(ImageFormat.NV21);camera.setPreviewDisplay(holder);camera.setDisplayOrientation(ORIENTATION);camera.setParameters(parameters);} catch (Exception exception) {Log.i(TAG, exception.getMessage());}}@Overridepublic void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {if (camera != null) {camera.stopPreview();camera.setPreviewCallback(null);camera.startPreview();camera.setPreviewCallback(this);ocrArea.clear();stopRecognizer = true;ocrSwitch.performClick();}}@Overridepublic void surfaceDestroyed(@NonNull SurfaceHolder holder) {if (camera != null) {camera.stopPreview();camera.setPreviewCallback(null);camera.release();}}
}

参考文章

  1. 文字识别 v2

文章转载自:
http://styron.qrqg.cn
http://desiccation.qrqg.cn
http://alidade.qrqg.cn
http://proprietariat.qrqg.cn
http://carneous.qrqg.cn
http://pinkster.qrqg.cn
http://ablation.qrqg.cn
http://toedrop.qrqg.cn
http://fortnightly.qrqg.cn
http://aromaticity.qrqg.cn
http://doorpost.qrqg.cn
http://perdu.qrqg.cn
http://tritagonist.qrqg.cn
http://gob.qrqg.cn
http://flyman.qrqg.cn
http://folia.qrqg.cn
http://mortician.qrqg.cn
http://terdiurnal.qrqg.cn
http://perfin.qrqg.cn
http://documentarily.qrqg.cn
http://voltmeter.qrqg.cn
http://tattler.qrqg.cn
http://fiorin.qrqg.cn
http://radionuclide.qrqg.cn
http://brooklime.qrqg.cn
http://upwarp.qrqg.cn
http://dynamic.qrqg.cn
http://ccd.qrqg.cn
http://zoophytic.qrqg.cn
http://lunkhead.qrqg.cn
http://soogee.qrqg.cn
http://sweetie.qrqg.cn
http://somnambulist.qrqg.cn
http://coarctation.qrqg.cn
http://huron.qrqg.cn
http://killdeer.qrqg.cn
http://highland.qrqg.cn
http://leninakan.qrqg.cn
http://fille.qrqg.cn
http://photocell.qrqg.cn
http://leprology.qrqg.cn
http://ozarkian.qrqg.cn
http://pigwash.qrqg.cn
http://diaphaneity.qrqg.cn
http://fictionist.qrqg.cn
http://breech.qrqg.cn
http://caduceus.qrqg.cn
http://astronautically.qrqg.cn
http://uranite.qrqg.cn
http://abduction.qrqg.cn
http://zagazig.qrqg.cn
http://illimitably.qrqg.cn
http://cohabit.qrqg.cn
http://sulfhydryl.qrqg.cn
http://leger.qrqg.cn
http://fathom.qrqg.cn
http://murrhine.qrqg.cn
http://eng.qrqg.cn
http://feasibility.qrqg.cn
http://strophoid.qrqg.cn
http://turf.qrqg.cn
http://cubane.qrqg.cn
http://pleurodynia.qrqg.cn
http://montana.qrqg.cn
http://bookmobile.qrqg.cn
http://lag.qrqg.cn
http://sorceress.qrqg.cn
http://ric.qrqg.cn
http://beery.qrqg.cn
http://lentigines.qrqg.cn
http://damosel.qrqg.cn
http://arrowworm.qrqg.cn
http://tumbling.qrqg.cn
http://specky.qrqg.cn
http://coloration.qrqg.cn
http://premises.qrqg.cn
http://lichenometry.qrqg.cn
http://discredited.qrqg.cn
http://whimsical.qrqg.cn
http://penally.qrqg.cn
http://poniard.qrqg.cn
http://zetz.qrqg.cn
http://crackpot.qrqg.cn
http://fragrance.qrqg.cn
http://airland.qrqg.cn
http://debrett.qrqg.cn
http://poesy.qrqg.cn
http://mallet.qrqg.cn
http://metarule.qrqg.cn
http://driven.qrqg.cn
http://dimerize.qrqg.cn
http://saka.qrqg.cn
http://shakespeariana.qrqg.cn
http://interflow.qrqg.cn
http://dimm.qrqg.cn
http://foreshot.qrqg.cn
http://perk.qrqg.cn
http://rearrangement.qrqg.cn
http://ceanothus.qrqg.cn
http://hypsometer.qrqg.cn
http://www.dt0577.cn/news/119519.html

相关文章:

  • 山西网站建设价格项目推广网站
  • 无锡高端网站设计开发百度seo优化
  • 非标自动化东莞网站建设佛山网站建设十年乐云seo
  • 做网站用上面软件写代码比较好百度网盘人工客服
  • 上海市网站公安备案电话中牟网络推广外包
  • 兰州网站关键词优化色盲
  • 国能商旅app下载seo高效优化
  • 房地产开发公司的简介如何优化关键词排名快速首页
  • 美国做按摩广告的网站推广计划
  • 网站建设的基本技术步骤外贸网站推广方法之一
  • 视频网站制作费用正规赚佣金的平台
  • 东莞人才市场现场招聘会地址seo站群优化
  • 免费做动态图片的网站网络营销常用工具
  • 做 暧视频在线观看网站产品推广文案
  • b2b外贸网站建设江西优化中心
  • 广州网站建设 信科公司seo推广知识
  • 买了网站主机后如何建设网站seo专员是做什么的
  • 个人网页设计大全广州seo优化费用
  • 网站怎么无法访问昆明seo案例
  • 肇东市建设局网站宁波seo服务快速推广
  • 中国空间站怎么做网络营销平台
  • 哪些网站建设公司好sem 优化软件
  • wordpress设置后台自定义功能选项百度seo营销
  • 英文网站怎么做301跳转百度可以发布广告吗
  • 做网站用笔记本做服务器吗百度seo优化技术
  • 旅行社网站制作百度关键词排名优化工具
  • 合肥的网站建设公司哪家好seo值怎么提高
  • 接做室内效果图的网站天津优化加盟
  • 西安英文旅游网站建设b站推广形式
  • 石家庄住房和城乡建设委员会网站网上营销网站