效果展示
1.导入下面这个库
implementation 'com.github.HyperInspire:hyperlpr3-android-sdk:1.0.3'//车牌
2.代码编写
private Bitmap ChePaiBitmap;//车牌图片
private String ChePaiResult;//车牌结果
private int Chepai_type;//车牌的类型
private String Chepai_color;//车牌颜色
/**
* 识别车牌
*/
//车牌识别
private void ChePai() {
ChePaiBitmap = null;
ChePaiResult = "";
Chepai_type=-1;
new Thread(() -> {//开启线程
Looper.prepare();
ChePaiBitmap = MainActivity.INSTANCE.getBitmap();//获取当前视频流的图片,如果你这里报错,请参考我前面的文章。
Log.e("图片大小", "w" + ChePaiBitmap.getWidth() + "h" + ChePaiBitmap.getHeight());
//初始化车牌识别
// 车牌识别算法配置参数
HyperLPRParameter parameter = new HyperLPRParameter()
.setDetLevel(HyperLPR3.DETECT_LEVEL_LOW)//设置识别水平
.setMaxNum(3)//可更改识别的车牌数量
.setRecConfidenceThreshold(0.85f);//设置精度
// 初始化(仅执行一次生效)
HyperLPR3.getInstance().init(this, parameter);
if (ChePaiBitmap != null) {
Bitmap bcopy = ChePaiBitmap.copy(Bitmap.Config.ARGB_8888, true);
Plate[] plates = HyperLPR3.getInstance().plateRecognition(ChePaiBitmap, HyperLPR3.CAMERA_ROTATION_0, HyperLPR3.STREAM_BGRA);
if (plates.length > 0) {
Canvas canvas = new Canvas(bcopy);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2.0f);
StringBuilder car = new StringBuilder();
StringBuilder car_color = new StringBuilder();
for (Plate plate : plates) {
// 打印检测到的车牌号
car.append(plate.getCode().substring(1)).append("\n");
Chepai_type = plate.getType();
String color = Plate_color(Chepai_type);//自己定义的方法
car_color.append(color).append("\n");
canvas.drawRect(new android.graphics.Rect((int) plate.getX1(), (int) plate.getY1(), (int) plate.getX2(), (int) plate.getY2()), paint);
}
ChePaiResult = car.toString();//车牌结果
ChePaiBitmap = bcopy;//框出车牌后的图片
Chepai_color = car_color.toString();//车牌颜色
// System.out.println(ChePaiResult);
}
}
qrHandler.sendEmptyMessage(60);//通过handler跳出线程
}).start();
}
因为车牌识别需要耗时,所以为了防止代码耗时过长,需要将其放在线程中执行,完成识别后,及时跳出线程,防止在线程卡死。
2.1 Plate_color 方法的代码
/**
* 返回车牌的颜色
* @param Chepai_type
* @return
*/
public String Plate_color(int Chepai_type){
String Plate_Color="";
if (Chepai_type==0){
Plate_Color="蓝牌";
}
if (Chepai_type==1){
Plate_Color="黄牌单层";
}
if (Chepai_type==2){
Plate_Color="白牌单层";
}
if (Chepai_type==3){
Plate_Color="绿牌新能源";
}
if (Chepai_type==4){
Plate_Color="黑牌港澳";
}
if (Chepai_type==5){
Plate_Color="香港单层";
}
if (Chepai_type==6){
Plate_Color="香港双层";
}
if (Chepai_type==7){
Plate_Color="澳门单层";
}
if (Chepai_type==8){
Plate_Color="澳门双层";
}
if (Chepai_type==9){
Plate_Color="黄牌双层";
}
if (Chepai_type==-1){
Plate_Color="未知车牌";
}
return Plate_Color;
}
如果对您有帮助,请点个赞。