Last active
December 16, 2015 02:19
-
-
Save shonenada/5361301 to your computer and use it in GitHub Desktop.
上传图片并且比较图片相似度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.awt.image.BufferedImage, javax.imageio.ImageIO, java.io.*, java.util.*" %> | |
<%! | |
static int[] deColor(BufferedImage orgImgBI) | |
{ | |
int oWidth = orgImgBI.getWidth(); | |
int oHeight = orgImgBI.getHeight(); | |
int oMinx = orgImgBI.getMinX(); | |
int oMiny = orgImgBI.getMinY(); | |
int i,j; | |
int pixel, R, G, B, Gray; | |
int[] oData = new int[oWidth * oHeight]; | |
int index = 0; | |
for(i=oMinx;i<oWidth;i++){ | |
for(j=oMiny;j<oHeight;j++){ | |
pixel = orgImgBI.getRGB(i, j); | |
R = (pixel & 0xff0000 ) >> 16; | |
G = (pixel & 0xff00 ) >> 8; | |
B = (pixel & 0xff ); | |
Gray = (int)(0.299 * R + 0.587 * G + 0.114 * B); | |
oData[index++] = Gray; | |
} | |
} | |
return oData; | |
} | |
static int[] bin(int[] img) | |
{ | |
int n = img.length; | |
int n1, n2; | |
int s1, s2; | |
double w1, w2; | |
double u1, u2; | |
double maxBetween = 0.0; | |
double between; | |
int threshold = 0; | |
int t; | |
for(t=0;t<256;++t) | |
{ | |
n1 = n2 = s1 = s2 = 0; | |
for(int i=0;i<n;++i) | |
{ | |
if((int)img[i] < t){ | |
++n1; | |
s1 = s1 + (int)img[i]; | |
} | |
else{ | |
++n2; | |
s2 = s2 + (int)img[i]; | |
} | |
} | |
w1 = (double)n1 / (double)n; | |
w2 = (double)n2 / (double)n; | |
u1 = (double)s1 / (double)n1; | |
u2 = (double)s2 / (double)n2; | |
between = w1 * w2 * (u1 - u2) * (u1 - u2) ; | |
if(maxBetween < between) | |
{ | |
maxBetween = between; | |
threshold = t; | |
} | |
} | |
for(int i=0;i<n;++i) | |
{ | |
img[i] = img[i] > threshold ? 255 : 0; | |
} | |
return img; | |
} | |
static int compare(int[] one, int[] two) | |
{ | |
int n1 = one.length; | |
int n2 = two.length; | |
if(n1 != n2) | |
{ | |
return -1; | |
} | |
int r = 0; | |
for(int i=0;i<n1; ++i) | |
{ | |
r += one[i] ^ two[i]; | |
} | |
return r; | |
} | |
%> | |
<% | |
String path=request.getRealPath("/"); | |
path = path + "exp5_extra/upload/"; | |
File orgImage = new File(path + "origin-rgb-0"); | |
File comImage1 = new File(path + "origin-rgb-1"); | |
File comImage2 = new File(path + "origin-rgb-2"); | |
if (orgImage == null || comImage1 == null || comImage2 == null) | |
{ | |
out.println("读取文件失败"); | |
} | |
BufferedImage orgBI = null; | |
BufferedImage comBI1 = null; | |
BufferedImage comBI2 = null; | |
try { | |
orgBI = ImageIO.read(orgImage); | |
comBI1 = ImageIO.read(comImage1); | |
comBI2 = ImageIO.read(comImage2); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
int[] oData = deColor(orgBI); | |
int[] comData1 = deColor(comBI1); | |
int[] comData2 = deColor(comBI2); | |
oData = bin(oData); | |
comData1 = bin(comData1); | |
comData2 = bin(comData2); | |
int distance1 = compare(oData, comData1); | |
int distance2 = compare(oData, comData2); | |
%> | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
<html> | |
<head> | |
<title>比较图片的相似度</title> | |
<link href="static/styles/style.css" rel="stylesheet" type="text/css"/> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
</head> | |
<body> | |
<div id="main"> | |
<h2>比较图片的相似度</h2> | |
<span class="result"> | |
<% | |
if(distance1 == -1) | |
out.println("原图片 与 图片1 必须大小相同!"); | |
if(distance2 == -1) | |
out.println("原图片 与 图片2 必须大小相同!"); | |
if(distance1<distance2) | |
out.println("图片1 与 原图片更相似"); | |
else if(distance2 == distance1) | |
out.println("图片1 与 图片2 与原图相似程度一样"); | |
else | |
out.println("图片2 与 原图片更相似"); | |
%> | |
</span> | |
<br /><input type="button" onclick="location.href='upload.jsp'" value="再测试" class="btn" /><br /> | |
原始图片:<br /> | |
<img src="upload/origin-rgb-0" /><br /> | |
图片1:<br /> | |
<img src="upload/origin-rgb-1" /><br /> | |
图片2:<br /> | |
<img src="upload/origin-rgb-2" /><br /> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.io.*" %> | |
<% | |
int picnum = (request.getParameter("picnum") == null) ? 0 : Integer.parseInt(request.getParameter("picnum")); | |
if(picnum > 2) | |
{ | |
// 上传图片的数量达到三次,进入比较的页面. | |
response.sendRedirect("compare.jsp"); | |
} | |
// 此处不明白,为何 request.getRealPath("/") 的结果不是 exp5_extra 目录 | |
String path = request.getRealPath("/"); | |
path = path + "exp5_extra/upload/"; | |
// 临时文件的文件名 | |
String tempName = new String("temp_file"); | |
File tempFile = new File(path + tempName); | |
FileOutputStream outFile = new FileOutputStream(tempFile); | |
// 获得输入流 | |
InputStream imgSrc = request.getInputStream(); | |
int n; | |
// 缓存字节数组 | |
byte b[] = new byte[1000]; | |
// 将文件读入 b 数组中 | |
while((n = imgSrc.read(b)) != -1) | |
{ | |
outFile.write(b, 0, n); | |
} | |
// 关闭流 | |
outFile.close(); | |
imgSrc.close(); | |
RandomAccessFile ranFile = new RandomAccessFile(tempFile, "r"); | |
// 刷去第一行的浏览器信息 | |
ranFile.readLine(); | |
// 刷去第二行的文件信息 | |
ranFile.readLine(); | |
// 获取文件的类型信息 | |
String filePath = ranFile.readLine(); | |
// 再刷 | |
ranFile.readLine(); | |
String headInfo = ranFile.readLine(); | |
if(filePath == null) | |
{ | |
filePath = ""; | |
} | |
else | |
{ | |
// 最近校园里的黑客好多= =,对文件的类型做个判断= = 防止被植马 | |
if(!(headInfo.charAt(0) == 'B' && headInfo.charAt(1) == 'M')) | |
{ | |
out.println("Hello Hacker!!"); | |
return ; // 停止执行程序 | |
} | |
// 保存的文件名 | |
String filename = "origin-rgb-" + (picnum++); | |
ranFile.seek(0); | |
long forthEnterPosition = 0; | |
int forth = 1; | |
while( (n = ranFile.readByte()) != -1 && (forth <= 4)) | |
{ | |
if(n == '\n') | |
{ | |
forthEnterPosition = ranFile.getFilePointer(); | |
forth++; | |
} | |
} | |
// 保存文件 | |
File saveFile = new File(path + filename); | |
RandomAccessFile saveRanFile = new RandomAccessFile(saveFile, "rw"); | |
ranFile.seek(ranFile.length()); | |
long endPosition = ranFile.getFilePointer(); | |
int j = 1; | |
while( (endPosition >= 0) && (j <= 4) ) | |
{ | |
endPosition--; | |
ranFile.seek(endPosition); | |
if(ranFile.readByte() == '\n') | |
j++; | |
} | |
ranFile.seek(forthEnterPosition); | |
long startPoint = ranFile.getFilePointer(); | |
while(startPoint < endPosition - 1) | |
{ | |
saveRanFile.write(ranFile.readByte()); | |
startPoint = ranFile.getFilePointer(); | |
} | |
// 跳转到下个页面 | |
response.sendRedirect("upload.jsp?picnum=" + (picnum++)); | |
// 关闭流 | |
saveRanFile.close(); | |
ranFile.close(); | |
tempFile.delete(); | |
} | |
%> | |
<% | |
int pn = (request.getParameter("picnum")== null) ? 0 : Integer.parseInt(request.getParameter("picnum")); | |
String info = "请上传 BMP 图片"; | |
if (pn > 2){ %> | |
<script> location.href = 'compare.jsp'; </script> | |
<% | |
} | |
if(pn == 0) | |
{ | |
info = "请上传原始 BMP 图片"; | |
} | |
if(pn == 1) | |
{ | |
info = "请上传比较图片1(BMP格式)"; | |
} | |
if(pn ==2) | |
{ | |
info = "请上传比较图片2(BMP格式)"; | |
} | |
%> | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
<html> | |
<head> | |
<title>比较图片的相似度</title> | |
<link href="static/styles/style.css" rel="stylesheet" type="text/css"/> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
</head> | |
<body> | |
<div id="main"> | |
<h2>比较图片的相似度</h2> | |
<span class="info"><%=info%></span> | |
<form method="post" enctype="multipart/form-data"> | |
<input type="file" name="upfile" size="30"> | |
<input type="submit" name="submit" value="上传"> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment