目录
一、eclipse开发插件项目
二、lib目录jar包准备
三、src目录代码准备
四、导出为jar包
(1)eclipse开发插件项目全部目录:
(2)完成插件直接在eclipse运行测试:
点击查看我的另一篇博文:【jmeter】实现验证码登录
(1)ImageIOHelper类:
主要包含两大部分,一部分就是降噪逻辑,通过创建降噪规则对背景图片像素点进行逐个扫描过滤,加大识别成功率;
另一部分是将图片格式转换为tiff格式以更好地进行识别,代码参考如下:
-
package com.test.gss;
-
import java.awt.Color;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.IOException;
-
import java.util.Iterator;
-
import java.util.Locale;
-
-
import javax.imageio.IIOImage;
-
import javax.imageio.ImageIO;
-
import javax.imageio.ImageReader;
-
import javax.imageio.ImageWriteParam;
-
import javax.imageio.ImageWriter;
-
import javax.imageio.metadata.IIOMetadata;
-
import javax.imageio.stream.ImageInputStream;
-
import javax.imageio.stream.ImageOutputStream;
-
-
import com.sun.media.imageio.plugins.tiff.TIFFImageWriteParam;
-
-
public
class ImageIOHelper{
-
-
//将图片格式转换为tif格式以更好地进行识别
-
public static File createImage(File imageFile, String imageFormat) {
-
-
File tempFile =
null;
-
ImageInputStream iis =
null;
-
ImageOutputStream ios =
null;
-
ImageReader reader =
null;
-
ImageWriter writer =
null;
-
-
try {
-
Iterator
readers = ImageIO.getImageReadersByFormatName(imageFormat);
-
reader = readers.next();
-
-
iis = ImageIO.createImageInputStream(imageFile);
-
reader.setInput(iis);
-
-
IIOMetadata streamMetadata = reader.getStreamMetadata();
-
TIFFImageWriteParam tiffWriteParam =
new TIFFImageWriteParam(Locale.CHINESE);
-
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
-
Iterator
writers = ImageIO.getImageWritersByFormatName(
"tiff");
-
writer = writers.next();
-
-
BufferedImage bi = removeBackgroud(reader.read(
0));
-
IIOImage image =
new IIOImage(bi,
null,reader.getImageMetadata(
0));
-
tempFile = tempImageFile(imageFile);
-
-
ios = ImageIO.createImageOutputStream(tempFile);
-
writer.setOutput(ios);
-
writer.write(streamMetadata, image, tiffWriteParam);
-
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
finally {
-
if(iis !=
null){
-
try {
-
iis.close();
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
if(ios !=
null){
-
try {
-
ios.close();
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
if(writer !=
null){
-
writer.dispose();
-
}
-
if(reader !=
null){
-
reader.dispose();
-
}
-
-
}
-
return tempFile;
-
}
-
-
private static File tempImageFile(File imageFile) {
-
String path = imageFile.getPath();
-
StringBuffer strB =
new StringBuffer(path);
-
return
new File(strB.toString().replaceFirst(
"jpg",
"tif"));
-
}
-
-
-
//给图片降噪,建立如下降噪规则和方法,提高识别度
-
public static int isFilter(int colorInt) {
-
Color color =
new Color(colorInt);
-
if ((color.getRed() >
85 && color.getRed() <
255)
-
&& (color.getGreen() >
85 && color.getGreen() <
255)
-
&& (color.getBlue() >
85 && color.getBlue() <
255)) {
-
return
1;
-
}
-
return
0;
-
}
-
-
public static BufferedImage removeBackgroud(BufferedImage img)
-
throws Exception {
-
int width = img.getWidth();
-
int height = img.getHeight();
-
for (
int x =
0; x < width; ++x) {
-
for (
int y =
0; y < height; ++y) {
-
if (isFilter(img.getRGB(x, y)) ==
1) {
-
img.setRGB(x, y, Color.WHITE.getRGB());
-
}
-
}
-
}
-
return img;
-
}
-
-
}
(2)OCR类:
通过Process调用已经安装的Tesseract程序,调用命令基本形式为tesseract xxx.tif 1 -l eng,参考如下代码:
-
package com.test.gss;
-
import java.io.BufferedReader;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
public
class OCR {
-
private
final String LANG_OPTION =
"-l";
-
private
final String EOL = System.getProperty(
"line.separator");
-
//第三方图形图像识别工具tesseract-OCR的安装目录
-
private String tessPath =
"G://Tesseract-ocr//tesserract//Tesseract-ORC//Tesseract-OCR";
-
-
public String recognizeText(File imageFile,String imageFormat) {
-
File tempImage = ImageIOHelper.createImage(imageFile,imageFormat);
-
File outputFile =
new File(imageFile.getParentFile(),
"output" + imageFile.getName());
-
StringBuffer sb =
new StringBuffer();
-
List
cmd =
new ArrayList();
-
-
cmd.add(tessPath+
"//tesseract");
-
cmd.add(
"");
-
cmd.add(outputFile.getName());
-
cmd.add(LANG_OPTION);
-
cmd.add(
"eng");
-
ProcessBuilder pb =
new ProcessBuilder();
-
pb.directory(imageFile.getParentFile());
-
-
cmd.set(
1, tempImage.getName());
-
pb.command(cmd);
-
pb.redirectErrorStream(
true);
-
-
Process process =
null;
-
BufferedReader in =
null;
-
int wait;
-
try {
-
process = pb.start();
-
//通过Process调用已经安装的Tesseract程序,调用命令基本形式为:tesseract xxx.tif 1 -l eng
-
wait = process.waitFor();
-
if(wait ==
0){
-
in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(outputFile.getAbsolutePath()+
".txt"),
"UTF-8"));
-
String str;
-
while((str = in.readLine())!=
null){
-
sb.append(str).append(EOL);
-
}
-
in.close();
-
-
}
else{
-
-
tempImage.delete();
-
}
-
new File(outputFile.getAbsolutePath()+
".txt").delete();
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
catch (InterruptedException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
finally {
-
if(in !=
null){
-
try {
-
in.close();
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
-
tempImage.delete();
-
return sb.toString();
-
}
-
}
(3)VcodeExtractor类:
继承AbstractScopedTestElement抽象类,实现PostProcessor接口的process方法,来处理利用OCR读取验证码信息的逻辑控制,参考代码如下:
-
package com.test.gss;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.Serializable;
-
-
import org.apache.jmeter.processor.PostProcessor;
-
import org.apache.jmeter.samplers.SampleResult;
-
import org.apache.jmeter.testelement.AbstractScopedTestElement;
-
import org.apache.jmeter.threads.JMeterContext;
-
import org.apache.jmeter.threads.JMeterVariables;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
-
public
class VcodeExtractor extends AbstractScopedTestElement implements PostProcessor, Serializable{
-
private
static
final
long serialVersionUID =
1L;
-
private
static
final Logger log = LoggerFactory.getLogger(VcodeExtractor.class);
-
-
@Override
-
public void process() {
-
-
// TODO Auto-generated method stub
-
JMeterContext context = getThreadContext();
-
SampleResult previousResult = context.getPreviousResult();
-
if (previousResult ==
null) {
-
return;
-
}
-
log.debug(
"VcodeExtractor processing result");
-
-
String status = previousResult.getResponseCode();
-
int id = context.getThreadNum();
-
//String imageName = id + ".jpg";
-
String path =
"E://" + id +
".jpg";
-
-
if(status.equals(
"200")){
-
byte[] buffer = previousResult.getResponseData();
-
FileOutputStream out =
null;
-
File file =
null;
-
try {
-
file =
new File(path);
-
out =
new FileOutputStream(file);
-
out.write(buffer);
-
out.flush();
-
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
finally {
-
if(out !=
null){
-
try {
-
out.close();
-
}
catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
-
-
try {
-
String vcode =
new OCR().recognizeText(file,
"jpeg");
-
vcode = vcode.replace(
" ",
"").trim();
-
-
JMeterVariables var = context.getVariables();
-
var.put(
"vcode", vcode);
-
var.put(
"vuser", String.valueOf(id));
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
}
-
-
}
(4)VcodeExtractorGUI类:
单纯实现一个可视化的界面用于在测试计划Tree中进行操作,参照代码如下:
-
package com.test.gss;
-
import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
-
import org.apache.jmeter.testelement.TestElement;
-
-
public
class VcodeExtractorGUI extends AbstractPostProcessorGui{
-
private
static
final
long serialVersionUID =
1L;
-
-
@Override
-
public TestElement createTestElement() {
-
// TODO Auto-generated method stub
-
VcodeExtractor extractor =
new VcodeExtractor();
-
modifyTestElement(extractor);
-
return extractor;
-
}
-
-
@Override
-
public String getLabelResource() {
-
// TODO Auto-generated method stub
-
return
this.getClass().getName();
-
}
-
-
@Override
-
public String getStaticLabel() {
-
//设置显示名称
-
// TODO Auto-generated method stub
-
return
"VcodeExtractor";
-
}
-
-
@Override
-
public void modifyTestElement(TestElement extractor) {
-
// TODO Auto-generated method stub
-
super.configureTestElement(extractor);
-
-
}
-
}
(5)TestOCR类:
方便设置main程序启动入口:
-
package com.test.gss;
-
-
import java.io.File;
-
-
public
class TestOCR {
-
-
public static void main(String[] args) {
-
String path =
"E://100.jpg";
-
System.out.println(
"ORC Test Begin......");
-
try {
-
String valCode =
new OCR().recognizeText(
new File(path),
"jpeg");
-
System.out.println(valCode);
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
System.out.println(
"ORC Test End......");
-
}
-
-
}
目前我开发好的插件,主要封装以上代码,但是目前不能直接使用,后期我会再研究优化,如需上面封装的代码,可到我的资源下载:点击前往下载