JAVA 比较两张图片的相似度
Posted 起个名字好难
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 比较两张图片的相似度相关的知识,希望对你有一定的参考价值。
原文:http://liuguihua0823.iteye.com/blog/1178118#bc2395917
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 比较两张图片的相似度 * @author Guihua * */ public class BMPLoader { // 改变成二进制码 public static String[][] getPX(String args) { int[] rgb = new int[3]; File file = new File(args); BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } int width = bi.getWidth(); int height = bi.getHeight(); int minx = bi.getMinX(); int miny = bi.getMinY(); String[][] list = new String[width][height]; for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { int pixel = bi.getRGB(i, j); rgb[0] = (pixel & 0xff0000) >> 16; rgb[1] = (pixel & 0xff00) >> 8; rgb[2] = (pixel & 0xff); list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2]; } } return list; } public static void compareImage(String imgPath1, String imgPath2){ String[] images = {imgPath1, imgPath2}; if (images.length == 0) { System.out.println("Usage >java BMPLoader ImageFile.bmp"); System.exit(0); } // 分析图片相似度 begin String[][] list1 = getPX(images[0]); String[][] list2 = getPX(images[1]); int xiangsi = 0; int busi = 0; int i = 0, j = 0; for (String[] strings : list1) { if ((i + 1) == list1.length) { continue; } for (int m=0; m<strings.length; m++) { try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; n<value2.length; n++) { if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) { xiangsi++; } else { busi++; } } } catch (RuntimeException e) { continue; } j++; } i++; } list1 = getPX(images[1]); list2 = getPX(images[0]); i = 0; j = 0; for (String[] strings : list1) { if ((i + 1) == list1.length) { continue; } for (int m=0; m<strings.length; m++) { try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; n<value2.length; n++) { if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) { xiangsi++; } else { busi++; } } } catch (RuntimeException e) { continue; } j++; } i++; } String baifen = ""; try { baifen = ((Double.parseDouble(xiangsi + "") / Double.parseDouble((busi + xiangsi) + "")) + ""); baifen = baifen.substring(baifen.indexOf(".") + 1, baifen.indexOf(".") + 3); } catch (Exception e) { baifen = "0"; } if (baifen.length() <= 0) { baifen = "0"; } if(busi == 0){ baifen="100"; } System.out.println("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%"); } public static void main(String[] args){ BMPLoader.compareImage("E:\\12.bmp", "E:\\1.bmp"); } }
package com.rd.p2p.additional.redisCaptcha.util; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.octo.captcha.service.CaptchaServiceException; import com.rd.p2p.common.util.jcaptcha.CaptchaServiceSingleton; public class ResponseUtil { private static Logger logger= Logger.getLogger(ResponseUtil.class); public static boolean sendMessageNoCache(HttpServletResponse response, String message) { response.setContentType("text/html;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("Pragma", "no-cache"); StringBuffer sb = new StringBuffer(); try { PrintWriter writer = response.getWriter(); sb.append(message); writer.write(sb.toString()); response.flushBuffer(); return true; } catch (Exception e) { logger.error("直接推送字符串错误", e); return false; } } public static boolean sendJSON(HttpServletResponse response, Object obj){ response.setContentType("text/json;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("Pragma", "no-cache"); StringBuffer sb = new StringBuffer(); try { PrintWriter writer = response.getWriter(); sb.append(JSONUtil.toJSON(obj)); writer.write(sb.toString()); response.flushBuffer(); return true; } catch (Exception e) { logger.error("直接推送JSON数据错误", e); return false; } } public static boolean sendJSONP(HttpServletResponse response, String callBack, Object obj){ response.setContentType("application/javascript;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("Pragma", "no-cache"); StringBuffer sb = new StringBuffer(); try { PrintWriter writer = response.getWriter(); sb.append(JSONUtil.toJSONP(callBack, obj)); writer.write(sb.toString()); response.flushBuffer(); return true; } catch (Exception e) { logger.error("直接推送JSONP数据错误", e); return false; } } public static void sendImg(HttpServletResponse response, BufferedImage buffImg, String mimeType, String fileName, String extName) throws IOException { // OutputStream out= null; // response.setContentType(mimeType); // response.setHeader("Content-Disposition", String.format("attachment;filename=%s.%s", fileName, extName)); // try { // out= response.getOutputStream(); // ImageIO.write(buffImg, extName, out); //// BufferedInputStream buffIn= new BufferedInputStream(in); //// BufferedOutputStream buffOut= new BufferedOutputStream(out); //// byte buff[]= new byte[4096];//申请缓冲区 //// int size= buffIn.read(buff);//初始化读取缓冲 //// while (size!= -1) { //// buffOut.write(buff, 0, size);//输出缓冲数据 //// size= buffIn.read(buff);//继续读取数据 //// } //// buffIn.close();//关闭输入流 //// buffOut.flush();//清空输出流 //// buffOut.close();//关闭输出流 // return true; // } catch (IOException e) { // logger.error("直接推送图片错误", e); // return false; // } response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream out = response.getOutputStream(); try { // String captchaId = request.getSession(true).getId(); // LOGGER.info("当前生产验证码的sessionId为: "+captchaId); // BufferedImage challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID( // captchaId, request.getLocale()); ImageIO.write(buffImg, "jpg", out); out.flush(); } catch (CaptchaServiceException e) { } finally { out.close(); } } }
以上是关于JAVA 比较两张图片的相似度的主要内容,如果未能解决你的问题,请参考以下文章