当做图片识别用tesseract,需要分布式跨平台部署时,依赖tesseract的cmd命令识别就感觉不太靠谱了。
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
public class PriceUtils {
private static Logger logger = Logger.getLogger(PriceUtils.class);
/**
* 解析京东商品价格
*
* @param url
* @return
* @throws IOException
*/
public static String parsePrice(String url) {
try {
BufferedImage img = ImageIO.read(new URL(url));
int width = img.getWidth();
int height = img.getHeight();
byte[][] data = new byte[height][width];
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
if (isWhite(img.getRGB(y, x)) == 1) {
data[x][y] = 0;
// if (x == 0) {
// System.out.print(y + "\t");
// } else {
// if (y == 0) {
// System.out.print(x + "\t");
// } else {
// System.out.print(" \t");
// }
// }
} else {
data[x][y] = 1;
// System.out.print("*\t");
}
}
// System.out.println("");
}
/** 列数 */
List<int[]> columns = getColumns(data);
/** 行数 */
int[] rows = getRows(data);
List<byte[][]> bytes = new ArrayList<byte[][]>();
byte[][] tmp;
int row = rows[1] - rows[0] + 1;
int index;
for (int[] is : columns) {
tmp = new byte[row][];
index = 0;
for (int i = rows[0]; i <= rows[1]; i++) {
// System.out.println("i:"+i+"["+is[0]+","+is[1]+"]");
// for(int j=is[0];j<=is[1];j++){
// System.out.print(data[i][j]);
// }
// System.out.println();
tmp[index++] = Arrays
.copyOfRange(data[i], is[0], is[1] + 1);
}
bytes.add(tmp);
}
Prices t;
List<Prices> prices = new ArrayList<Prices>();
for (byte[][] b : bytes) {
t = Prices.imgFor(b);
if (t != null) {
prices.add(t);
}
}
String s = "";
for (Prices p : prices) {
s = s + p.getValue();
}
return s;
} catch (IOException e) {
logger.warn("解析价格出错url["+url+"]",e);
return "";
}
}
public static int[] getRows(byte[][] data) {
List<Integer> list = new ArrayList<Integer>();
int height = data.length;
int width = data[0].length;
boolean flag_1;
for (int i = 0; i < height; i++) {
flag_1 = true;
for (int j = 0; j < width; j++) {
if (data[i][j] == 1) {
flag_1 = false;
}
}
if (flag_1) {
list.add(i);
}
}
int size = list.size();
int[] rows = null;
for (int i = 0; i < size - 1; i++) {
if (list.get(i + 1) - list.get(i) > 1) {
// System.out.println("行[" + (list.get(i) + 1) + ","+ (list.get(i + 1) - 1) + "]");
rows = new int[] { list.get(i) + 1, list.get(i + 1) - 1 };
}
}
return rows;
}
public static List<int[]> getColumns(byte[][] data) {
List<Integer> list = new ArrayList<Integer>();
List<int[]> columns = new ArrayList<int[]>();
int height = data.length;
int width = data[0].length;
boolean flag_1;
for (int i = 0; i < width; i++) {
flag_1 = true;
for (int j = 0; j < height; j++) {
if (data[j][i] == 1) {
flag_1 = false;
}
}
if (flag_1) {
list.add(i);
}
}
int size = list.size();
for (int i = 0; i < size - 1; i++) {
if (list.get(i + 1) - list.get(i) > 1) {
// System.out.println("列[" + (list.get(i) + 1) + "," + (list.get(i + 1) - 1) + "]");
columns.add(new int[] { list.get(i) + 1, list.get(i + 1) - 1 });
}
}
return columns;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
// R255 G255 B255
if (color.getRed() + color.getGreen() + color.getBlue() > 400) {
return 1;
}
return 0;
}
public static void main(String args[]) throws Exception {
String url = "http://jprice.360buyimg.com/price/gp749416-1-1-1.png";
System.out.println(parsePrice(url));
}
}
public enum Prices {
_Y("¥",new byte[][] {
{1,1,1,1,0,1,1,1,1},
{0,1,1,1,0,0,1,1,0},
{0,0,1,1,1,0,1,0,0},
{0,0,1,1,1,0,1,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,1,1,1,1,1,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,1,0,0}
}),
_POINT(".",new byte[][] {
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{1,1},
{1,1}
}),
_0("0",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0}
}),
_1("1",new byte[][] {
{0,0,0,0},
{0,1,1,0},
{1,1,1,0},
{0,1,1,0},
{0,1,1,0},
{0,1,1,0},
{0,1,1,0},
{0,1,1,0},
{1,1,1,1}
}),
_2("2",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{0,0,0,0,1,1},
{0,0,0,1,1,0},
{0,0,1,1,0,0},
{0,1,1,0,0,0},
{1,1,0,0,0,0},
{1,1,1,1,1,1}
}),
_3("3",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{0,0,0,0,1,1},
{0,0,1,1,1,0},
{0,0,0,0,1,1},
{0,0,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0}
}),
_4("4",new byte[][] {
{0,0,0,0,0,0},
{0,0,0,0,1,0},
{0,0,0,1,1,0},
{0,0,1,1,1,0},
{0,1,0,1,1,0},
{1,0,0,1,1,0},
{1,1,1,1,1,1},
{0,0,0,1,1,0},
{0,0,0,1,1,0}
}),
_5("5",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,1},
{0,1,1,0,0,0},
{0,1,1,0,0,0},
{0,1,1,1,1,0},
{0,0,0,0,1,1},
{0,0,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0}
}),
_6("6",new byte[][] {
{0,0,0,0,0,0},
{0,0,1,1,1,0},
{0,1,1,0,0,0},
{1,1,0,0,0,0},
{1,1,1,1,1,0},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0}
}),
_7("7",new byte[][] {
{0,0,0,0,0,0},
{1,1,1,1,1,1},
{0,0,0,0,1,1},
{0,0,0,1,1,0},
{0,0,0,1,1,0},
{0,0,1,1,0,0},
{0,0,1,1,0,0},
{0,1,1,0,0,0},
{0,1,1,0,0,0}
}),
_8("8",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,0}
}),
_9("9",new byte[][] {
{0,0,0,0,0,0},
{0,1,1,1,1,0},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{1,1,0,0,1,1},
{0,1,1,1,1,1},
{0,0,0,0,1,1},
{0,0,0,1,1,0},
{0,1,1,1,0,0}
});
private byte[][] img;
private String value;
private Prices(String value,byte[][] img){
this.value = value;
this.img = img;
}
public byte[][] getImg(){
return this.img;
}
public String getValue(){
return this.value;
}
public static Prices imgFor(byte[][] img) {
Prices wt = null;
if (img == null) {
return wt;
}
Prices[] wts = Prices.values();
for(Prices w : wts){
if(deepEquals(w.img,img)){
wt = w;
break;
}
}
return wt;
}
public static Prices valueFor(String v) {
Prices wt = null;
if (v == null) {
return wt;
}
Prices[] wts = Prices.values();
for(Prices w : wts){
if(v.equals(w.value)){
wt = w;
break;
}
}
return wt;
}
public static boolean deepEquals(byte[][] a, byte[][] b) {
try {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
if (a[i][j] != b[i][j])
return false;
return true;
} catch (Exception e) {
}
return false;
}
}
分享到:
相关推荐
京东价格图片识别源码 功能介绍: 使用控制台程序可以识别京东价格图片和一些简单的图片上的数字, 不能识别有干扰的验证码之类的复杂图片。 开发环境为Visual Studio 2008 注意: image图片文件夹在bin\...
去年某天,因为某些个人兴趣,做了一个图片识别的功能,主要是可以识别京东产品的价格。原来什么的就不多说了,都是一些基本的,网上也有很多资料了。主要是想把源码什么的发出来,供大家下载。 看过这个之后,有...
京东品牌识别手册【互联网】【品牌手册】【视觉VI】.pdf
京东图片标准 京东图片标准 京东图片标准 京东图片标准
python源码,对京东商城的商品价格图片进行解析,获取该商品的实际价格。
自带PHP测试服务器+VBS脚本,京东价格图片转数字的工具。 某产品价格出现变动,就会提示音乐报警。可以按说明自己设置商品。 VBS为主要测试入口,HOME文件为PHP服务器。 非正常关闭注意后台清理线程可二次运行。 ...
京东评价图片采集,可自定义商品ID
京东猪脸识别比赛.zip
这是京东商品图片获取的python爬虫。当检索一个商品名称之后,会返回值100页的商品介绍,每页有60个商品,每个商品都有一张图片,这个爬虫就是爬取这些图片的。
京东虚假交易识别系统.pdf
全国大学生电子设计竞赛(National Undergraduate Electronics Design Contest),试题,解决方案及源码。计划或参加电赛的同学可以用来学习提升和参考。程序均是实战案例,经过测试可直接运行。...
怎样才能一次性的下载并保存到京东商城的多张商品图片和视频,这是做电商从业人员的基本功底,如果纯靠手动一个个的去挑选商品和保存,那样不但花费了冤枉的时间,还达不到欲想的效果。那么大家可以应用一款专门采集...
京东商城手机类爬虫demo源码,亲测可用!
最近因为项目的缘故,用到了一些图片识别的应用,此代码是以京东商城价格图片为例,如需要分析其它的简单图片只需要小改一下就好了
京东图片列表
京东图片采集无水印
仿淘宝京东评论图片 类似淘宝、京东评论图片,左右滑动查看、图片放大缩小,之间查看源码,复制粘贴就可以用
爬虫抓取京东价格 解析Json格式 对所有开发抓取京东价格人员非常有帮助。
名称:Crawler-淘宝京东详情页图片抓取 -------------------- 版本:1.1.0 作者:lcs584911924 分类:其他 -------------------- 概述:使用这个附加组件,可以便捷的将国内主要的电商平台:淘宝、天猫、京东的...
京东滑块验证码识别