(二)Android Studio uniapp新项目集成HyperLPR开源项目
本文出自:https://blog.csdn.net/Hello_World_CFF/article/details/116999643
前提:
已做完Android Studio uniapp新项目集成HyperLPR开源项目(一)
本部分主要是把原生的识别的结果,传值至uniapp,再做相应变更。
一、移动部分MainActivity.java至CameraActivity.java
主思路还是移动,但是有些改动,总之,下面的复制粘贴过去,即可。这边就值保留了实时识别的部分,所以只取了实时识别所需要的部分,在个人看来,这部分也更高级。
最开始的初始化:
// Used to load the 'native-lib' library on application startup.
static {
if(OpenCVLoader.initDebug())
{
Log.d("Opencv","opencv load_success");
}
else
{
Log.d("Opencv","opencv can't load opencv .");
}
}
后面有用到的全局变量
public static long handle;
private final String TAG = this.getClass().toString();
onCreate
//初始化识别函数
initRecognizer();
初始化识别函数的定义
public void initRecognizer()
{
String assetPath = "pr";
String sdcardPath = Environment.getExternalStorageDirectory()
+ File.separator + assetPath;
copyFilesFromAssets(this, assetPath, sdcardPath);
String cascade_filename = sdcardPath
+ File.separator+"cascade.xml";
String finemapping_prototxt = sdcardPath
+ File.separator+"HorizonalFinemapping.prototxt";
String finemapping_caffemodel = sdcardPath
+ File.separator+"HorizonalFinemapping.caffemodel";
String segmentation_prototxt = sdcardPath
+ File.separator+"Segmentation.prototxt";
String segmentation_caffemodel = sdcardPath
+ File.separator+"Segmentation.caffemodel";
String character_prototxt = sdcardPath
+ File.separator+"CharacterRecognization.prototxt";
String character_caffemodel= sdcardPath
+ File.separator+"CharacterRecognization.caffemodel";
String segmentationfree_prototxt = sdcardPath
+ File.separator+"SegmenationFree-Inception.prototxt";
String segmentationfree_caffemodel= sdcardPath
+ File.separator+"SegmenationFree-Inception.caffemodel";
handle = PlateRecognition.InitPlateRecognizer(
cascade_filename,
finemapping_prototxt,finemapping_caffemodel,
segmentation_prototxt,segmentation_caffemodel,
character_prototxt,character_caffemodel,
segmentationfree_prototxt,segmentationfree_caffemodel
);
}
初始化识别函数有引用函数copyFilesFromAssets
:
public void copyFilesFromAssets(Context context, String oldPath, String newPath) {
try {
String[] fileNames = context.getAssets().list(oldPath);
if (fileNames.length > 0) {
// directory
File file = new File(newPath);
if (!file.mkdir())
{
Log.d("mkdir","can't make folder");
}
// return false; // copy recursively
for (String fileName : fileNames) {
copyFilesFromAssets(context, oldPath + "/" + fileName,
newPath + "/" + fileName);
}
} else {
// file
InputStream is = context.getAssets().open(oldPath);
FileOutputStream fos = new FileOutputStream(new File(newPath));
byte[] buffer = new byte[1024];
int byteCount;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static PlateInfo simpleRecog(Bitmap bmp,int dp)
{
float dp_asp = dp/10.f;
// imgv.setImageBitmap(bmp);
Mat mat_src = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8UC4);
float new_w = bmp.getWidth()*dp_asp;
float new_h = bmp.getHeight()*dp_asp;
Size sz = new Size(new_w,new_h);
Utils.bitmapToMat(bmp, mat_src);
Imgproc.resize(mat_src,mat_src,sz);
long currentTime1 = System.currentTimeMillis();
// String res = PlateRecognition.SimpleRecognization(mat_src.getNativeObjAddr(),handle);
// resbox.setText("识别结果:"+res);
PlateInfo plateInfo = PlateRecognition.PlateInfoRecognization(mat_src.getNativeObjAddr(),handle);
return plateInfo;
}
二、修改CameraActivity.java
CameraActivity.java调用CameraPreviews.java返回值接收与处理
接收
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(PlateInfo plate){
getResultFinish(this,JSON.toJSONString(plate));
//getResultFinish(this,plate.plateName);
plateTv.setText(plate.plateName);
image.setImageBitmap(plate.bitmap);
stopPreview();
}
只需在接收CameraPreviews.java的位置添加:
getResultFinish(this,JSON.toJSONString(plate));
处理返回至uniapp
添加下面的函数:
/***
* 获取到结果
* @param activity 实体类
* @param plateInfo_str 识别结果
*/
public void getResultFinish(Activity activity, String plateInfo_str) {
Intent intent = new Intent();
intent.putExtra("plateInfo", plateInfo_str);
activity.setResult(RESULT_OK, intent);
activity.finish();
}
返回plateInfo
,并关闭原生的识别弹框
引入相关包
代码复制,会因为没有引入包,报红,只需把下面的包引入即可
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
三、修改CameraPreviews.java
把之前调用MainActivity.java的方法,换成CameraActivity.java的。在onPreviewFrame
函数,160行左右的位置
//PlateInfo result = MainActivity.simpleRecog(rotateBitmap(bitmap), 8);
PlateInfo result = CameraActivity.simpleRecog(rotateBitmap(bitmap), 8);
com.test.aytestcc
四、修改AndroidBridge类
把调用界面MainActivity换成CameraActivity,全部代码如下:
import com.test.aytestcc.CameraActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.json.JSONArray;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
public class AndroidBridge extends StandardFeature {
/**
* 识别方法
* @param activity
*/
public static void doScan(Activity activity) {
Log.i("TAG2-*--","doScan");
Intent intent = new Intent(activity, CameraActivity.class);
activity.startActivityForResult(intent, 1);
}
}
五、ReBuild Project
六、运行调试并打包
不报错,就可以选择手机(手机已设置开发者模式),点运行,然后点拍照,对准一个有车牌图片的图片,就能有示例运行效果。
若您遇到什么问题,欢迎联系。有问题,欢迎指正。
七、对应视频链接
相关链接
Android Studio uniapp新项目集成HyperLPR开源项目(一)
Android Studio 运行HyperLPR开源项目安卓APP
Xcode 运行HyperLPR开源项目苹果APP
有疑问
微信小程序联系客服,及时沟通
扫描公众号,了解更多实例与资源免费分享:
参考
百度安卓原生传值方法