用base64编码图片,比较base64编码能实现,不过图片越大,base64编码的字符串长度特长,45KB左右的字符串长度有6W多。
下面是图片转base64和base64转图片的代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class Base64 {
public static void main(String[] args)
{
String strImg = GetImageStr();
GenerateImage(strImg);
}
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "d:\\111.jpg";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
public static boolean GenerateImage(String imgStr)
{//对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d:\\222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
下面是图片转base64和base64转图片的代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class Base64 {
public static void main(String[] args)
{
String strImg = GetImageStr();
GenerateImage(strImg);
}
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "d:\\111.jpg";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
public static boolean GenerateImage(String imgStr)
{//对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d:\\222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
base64的用途主要还是为了不方便使用字节(例如有些网络传输,不方便使用不可打印字符)的情况下使用的。
base64的用途主要还是为了不方便使用字节(例如有些网络传输,不方便使用不可打印字符)的情况下使用的。
是我对base64还不够了解,感谢您的意见。
好多种方法:指纹的Hamming distance,颜色分布(如RGB直方图)、内容特征(轮廓提取如canny)
不过还是有点复杂,我想到了几个问题……
格式不同的同一图片怎样判断?
只有少量像素不同的图片在视觉效果上是基本一致的,可能应判定相同,那么阈值应当是多少?
图像被拉伸、放缩等处理后能否认定是同图?
图像的亮度、饱和度调整后是否能认定是同图?
可以翻阅谷歌识图、图像识别配准特征提取相关文献资料。
【1】【2】【3】