`
wingware
  • 浏览: 143630 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论
阅读更多
当做图片识别用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;
	}
	
}
分享到:
评论

相关推荐

    京东价格图片识别源码201271

    京东价格图片识别源码 功能介绍: 使用控制台程序可以识别京东价格图片和一些简单的图片上的数字, 不能识别有干扰的验证码之类的复杂图片。 开发环境为Visual Studio 2008 注意: image图片文件夹在bin\...

    京东商城产品价格图片识别程序

    去年某天,因为某些个人兴趣,做了一个图片识别的功能,主要是可以识别京东产品的价格。原来什么的就不多说了,都是一些基本的,网上也有很多资料了。主要是想把源码什么的发出来,供大家下载。 看过这个之后,有...

    京东商品价格图片解析

    - 对于特定的京东价格格式,可以创建自定义的识别模板。 - 结合其他OCR工具或服务,如Google的Cloud Vision API,进行混合识别。 总结来说,解析京东商品价格图片主要涉及Python图像处理库的使用、OCR技术的集成...

    简单的图片识别,源代码

    本项目提供了一段简单的图片识别源代码,主要针对京东商城的价格图片进行解析。接下来,我们将深入探讨这个话题。 首先,让我们关注“图片识别”这个概念。图片识别是指通过计算机视觉算法对图像中的特定元素进行...

    京东秒购助手-5月7日最新0秒预约抢购下单图片可以看!

    “秒杀”和“抢购”是京东常见的促销方式,商品在特定时间点以极低价格出售,吸引了大量用户参与;“0秒”和“整点”是京东秒购助手的重要特性,它能确保用户在商品开售瞬间进行抢购,从而增加购买成功的机会。 ...

    ecshop 2013京东模板最新版10.25

    3. **商品展示**:商品页面设计规范,包含商品图片、详情、价格、评价等信息,支持多种规格选择,方便用户比较购买。 4. **搜索功能**:强大的搜索引擎,支持关键词、分类、品牌等多种方式进行商品搜索,提高用户...

    京东详情页

    京东详情页可能包含商品图片、价格、描述、评价等模块。设计师可能会使用Material Design指南来创建一致且吸引人的用户体验,利用CardView、RecyclerView等组件展示商品信息。 2. **布局管理**:为了提高代码的可...

    京东秒杀助手,22年6月21更新-非常好用,带成功图片!适合整点秒杀!0秒下单!-永久免费!

    在电子商务领域,京东秒杀活动一直备受消费者青睐,它不仅提供了极具吸引力的价格优惠,还带来了激烈的竞争快感。而针对这种需求,开发者们推出了“京东秒杀助手”,一款专为京东平台设计的辅助工具,旨在帮助用户...

    京东加强版ecshop

    7. `goods.php`:商品详情展示和处理页面,包含商品图片、描述、价格等信息,以及购买、加入购物车等操作。 8. `search.php`:搜索功能实现,允许用户快速找到所需商品,对于提升用户满意度和交易量至关重要。 9. ...

    Python爬虫,京东自动登录,在线抢购商品.zip

    - 获取验证码:登录时可能需要解决验证码问题,这可能需要用到OCR识别技术(如Tesseract)或者使用第三方服务来识别图片验证码。 - 处理Cookie和Session:登录成功后,服务器会返回一个或多个Cookie,用于后续的...

    天代手表男士钻表机械表防水钻表透底机械表男表婚表镶钻情侣对表时尚简约高档大气机械表 棕色间金白面【图片 价格 品牌 评论】-京东.pdf

    5. 技术说明:说明中提到了书籍内容是通过OCR技术扫描而来的,这表明书籍在转换为电子版的过程中可能会有技术误差,例如字识别错误或漏识别。 6. 联系方式:最后,提供了多个QQ联系方式,以便用户在有需要时可以...

    京东店铺后台装修简述.doc

    在商品详情页中,主要是进展商品的详细信息的展示,包括商品的图片、价格、描述等信息。商品详情页的装修主要是通过添加模板进展页面装修,可以选择不同的模板来进展商品的展示。 在活动装修模块中,主要是进展店铺...

    iOS高仿“京东”商城开发

    在模拟京东App的交互时,会用到手势识别(如滑动手势、点击手势等)和动画效果(如页面过渡动画、加载动画等),提升用户体验。 八、状态管理 处理网络请求中的加载状态、错误状态、空状态,以及界面刷新状态,是...

    淘宝商品爬虫, selenium破解滑块验证(目前可用)

    4. 解析商品信息,如价格、销量、评价等。 5. 存储抓取到的数据,可能采用CSV、JSON等格式。 6. 异常处理和循环逻辑,确保爬虫的稳定运行。 总之,淘宝商品爬虫结合Selenium破解滑块验证是一项综合性的技术任务,...

    仿京东商品详情

    `android-vertical-slide-view`是一个适用于Android平台的垂直滑动视图库,其主要目的是实现类似于京东商品详情页的滑动效果,即在上下滑动过程中,商品图片会随着手指的移动而放大或缩小,提供了一种良好的用户体验...

    python_JD抢购.zip

    "area_id.png"可能是一个包含地区ID的图片,这在京东购物中通常是用来指定收货地址的。在自动下单的过程中,正确设置购买区域是至关重要的,因为某些商品可能只在特定地区销售或配送。 "qr.png"可能是二维码图片,...

    ios-仿京东商品详情,上拉显示图文详情.zip

    在这个场景中,商品详情视图可能包含多个子视图,如图片、文字描述、价格等,这些都由UIViewController进行组织和管理。 其次,视图动画是iOS开发中提升用户体验的重要手段。"视图动画"标签提示我们,这个项目会...

    仿京东商城

    8. **goods.php**:商品详情页的PHP文件,展示单个商品的信息,包括图片、描述、价格等。 9. **search.php**:搜索功能的实现,让用户可以通过关键词查找需要的商品。 10. **exchange.php**:可能涉及到积分兑换或...

    仿造京东的套餐购买列表

    在这个案例中,UICollectionViewCell可能会被定制,以显示商品图片、名称、价格等信息,并且可以设置选中状态,以表示用户已选择加入套餐。 3. **Model和Data Binding**:为了存储和管理商品信息,源码中可能会有...

    淘宝天猫商品详情API接口(商品详情页面数据接口,商品销量接口)

    商城建站业务等,整个站的商品详情数据均可以拿到,另外淘宝天猫,京东,1688,当当,唯品会,小红书,抖音,拼多多,快手,亚马逊,速卖通,lazada,苏宁,国美电器,VVIC等30多个电商平台的详情接口也有封装。

Global site tag (gtag.js) - Google Analytics