京东价格图片识别

当做图片识别用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;
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值