Android 识别车牌信息

时间:2024-02-16 20:43:32
package com.example.lprec; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Build; import android.os.Bundle; import android.util.AndroidException; import android.util.Log; import android.widget.Button; import android.widget.CalendarView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.hyperai.hyperlpr3.HyperLPR3; import com.hyperai.hyperlpr3.bean.HyperLPRParameter; import com.hyperai.hyperlpr3.bean.Plate; import java.io.IOException; import java.io.InputStream; public class MainActivity extends AppCompatActivity { ImageView img = null; TextView text = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { img = findViewById(R.id.rec_img_show); text = findViewById(R.id.text_show); Button button; button = findViewById(R.id.rec_button); initLoadLPR(); // 画布 Canvas canvas = new Canvas(); // 点击button'的点击事件 button.setOnClickListener(v -> { Log.d("Test", ""); try (InputStream file = getAssets().open("lp1.jpg")) { Bitmap lpImg = BitmapFactory.decodeStream(file); Bitmap showImg = null; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { showImg = Bitmap.createBitmap(lpImg.getWidth(), lpImg.getHeight(), Bitmap.Config.RGB_565, false); } if (showImg != null) { canvas.setBitmap(showImg); // 绘制画笔 Paint paint = new Paint(); paint.setStrokeWidth(2);//设置画笔宽为2 paint.setARGB(255, 255, 0, 0); paint.setStyle(Paint.Style.STROKE); // 绘制画笔到画布上 canvas.drawBitmap(lpImg, 0, 0, new Paint()); Plate[] plates = HyperLPR3.getInstance().plateRecognition(lpImg, HyperLPR3.CAMERA_ROTATION_0, HyperLPR3.STREAM_BGRA); // 自定义字符串内容 StringBuilder builder = new StringBuilder(); for (int i = 0; i < plates.length; i++) { Plate plate = plates[i]; canvas.drawRect(plate.getX1(), plate.getY1(), plate.getX2(), plate.getY2(), paint); } img.setImageBitmap(showImg); text.setText(builder.toString()); } } catch (IOException e) { Toast.makeText(this, "图像加载失败了", Toast.LENGTH_SHORT).show(); } }); } private void initLoadLPR() { // 配置HyperLPRParameter的车牌识别库,链式调用 HyperLPRParameter parameter = new HyperLPRParameter(). setDetLevel(HyperLPR3.DETECT_LEVEL_HIGH).//设置功耗等级 setBoxConfThreshold(0.5f);//设置最低功率,过滤白分别 // 初始化Hyper车牌识别库,创建单例类 HyperLPR3.getInstance().init(this, parameter); } }