数据库连接

master
aikang 5 years ago
parent 22bd279300
commit 5cf0b01218

@ -1,5 +1,9 @@
package com.yuxue.test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import org.bytedeco.javacpp.opencv_imgproc;
import java.io.File;
@ -36,7 +40,7 @@ public class PlateRecoTest {
*/
@Test
public void testPlateRecognise() {
String imgPath = "res/image/test_image/plate_recognize.jpg";
String imgPath = "res/image/test_image/E.D3098.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
@ -53,8 +57,53 @@ public class PlateRecoTest {
String palte = cr.charsRecognise(img, "tem/"); // 字符识别
PlateColor color = CoreFunc.getPlateType(img, true);
String col=color.desc;
System.err.println("识别到的车牌: " + palte + "_" + color.desc);
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=AdventureWorks;integratedSecurity=true;";
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=chepai;user=sa;password=197905";//以sa身份连接数据库:请根据自己的用户名和密码进行重新设置!!!!!!
// 声明JDBC对象
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 建立数据库连接.
System.out.println("尝试建立数据库连接.");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
System.out.println("数据库连接成功!");
String SQL = "INSERT INTO plate(chepaihao,color) VALUES('"+palte+"','"+col+"' )";
stmt = con.createStatement();
stmt.executeUpdate(SQL);
}
//处理和打印程序异常
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null)
try {
rs.close(); //关闭查询结果集句柄
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close(); //关闭语句句柄
} catch (Exception e) {
}
if (con != null)
try {
con.close(); //关闭数据库连接
} catch (Exception e) {
}
}
// 识别的车牌,保存图片文件 //需要先创建文件夹
String str = "d:/PlateDetect/" + palte + "_"+ color + "_" + System.currentTimeMillis() +".png";
String str1 = "d:/PlateDetect/" + i + ".png";
@ -68,161 +117,11 @@ public class PlateRecoTest {
file.renameTo(targetFile);
}
}
}
/**
* = +
* Mat
*
*/
@Test
public void testPlateDetect() {
String imgPath = "res/image/test_image/test.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
PlateDetect plateDetect = new PlateDetect();
plateDetect.setPDLifemode(true);
Vector<Mat> matVector = new Vector<Mat>();
if (0 == plateDetect.plateDetect(src, matVector)) {
for (int i = 0; i < matVector.size(); ++i) {
Mat img = matVector.get(i);
// 弹窗显示
opencv_highgui.imshow("Plate Detected", img);
String str = "d:/test/" + i + ".png";
opencv_imgcodecs.imwrite(str, img);
}
}
}
/**
*
*
*
*/
@Test
public void testPlateLocate() {
String imgPath = "res/image/test_image/test.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
PlateLocate plate = new PlateLocate();
plate.setDebug(true);
plate.setLifemode(true);
Vector<Mat> resultVec = plate.plateLocate(src);
int num = resultVec.size();
for (int j = 0; j < num; j++) {
Mat img = resultVec.get(j);
// showImage("Plate Located " + j, resultVec.get(j));
String str = "d:/test/" + j + ".png";
opencv_imgcodecs.imwrite(str, img);
}
return;
}
/**
*
*/
@Test
public void testCharsRecognise() {
String imgPath = "res/image/test_image/chars_recognise_huAGH092.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
CharsRecognise cr = new CharsRecognise();
cr.setCRDebug(true);
String result = cr.charsRecognise(src, "tem/");
System.out.println("Chars Recognised: " + result);
}
/**
*
*/
@Test
public void testColorDetect() {
String imgPath = "res/image/test_image/core_func_yellow.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
PlateColor color = CoreFunc.getPlateType(src, true);
System.out.println("Color Deteted: " + color);
}
/**
*
*/
@Test
public void testProjectedHistogram() {
String imgPath = "res/image/test_image/chars_identify_E.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
CoreFunc.projectedHistogram(src, Direction.HORIZONTAL);
}
/**
*
*/
@Test
public void testCharsIdentify() {
String imgPath = "res/image/test_image/chars_identify_E.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
CharsIdentify charsIdentify = new CharsIdentify();
String result = charsIdentify.charsIdentify(src, false, true);
System.out.println(result);
}
/**
* 绿
*/
@Test
public void testGreenColorReco() {
String imgPath = "res/image/test_image/debug_resize_2.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
// 判断绿色车牌
Mat src_hsv = new Mat();
opencv_imgproc.cvtColor(src, src_hsv, opencv_imgproc.CV_BGR2HSV);
src_hsv = CoreFunc.colorMatch(src, PlateColor.GREEN, true);
System.err.println(CoreFunc.plateColorJudge(src, PlateColor.GREEN, true));
String str = "d:/PlateDetect/src_hsv.png";
opencv_imgcodecs.imwrite(str, src_hsv);
}
@Test
public void testGreenPlate() {
String imgPath = "res/image/test_image/debug_resize_2.jpg";
Mat src = opencv_imgcodecs.imread(imgPath);
// 车牌检测对象
PlateDetect plateDetect = new PlateDetect();
plateDetect.setPDLifemode(true);
plateDetect.setDebug(false, ""); // 将过程的图块保存到盘符
Vector<Mat> matVector = new Vector<Mat>();
System.err.println(plateDetect.plateDetect(src, matVector));
System.err.println(matVector.size());
for (int i = 0; i < matVector.size(); ++i) { // 遍历车牌图块Mat进行识别
Mat img = matVector.get(i);
String str = "d:/PlateDetect/temp/result_.png";
opencv_imgcodecs.imwrite(str, img);
}
}
}
}
}

Loading…
Cancel
Save