安卓感知哈希算法
来源:互联网 发布:深圳益讯网络 编辑:程序博客网 时间:2024/05/01 04:17
public static String getHash(Bitmap bmp){ /** * 第一步,缩小尺寸。 * 最快速的去除高频和细节,只保留结构明暗的方法就是缩小尺寸。 * 将图片缩小到8x8的尺寸,总共64个像素。摒弃不同尺寸、比例带来的图片差异 */int width = bmp.getWidth(); int height = bmp.getHeight(); int newWidth = 8; int newHeight = 8; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true); /** * 第二步,简化色彩。 * 将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。 */ int width1 = newbmp.getWidth(); //获取位图的宽 int height1 = newbmp.getHeight(); //获取位图的高 int []pixels = new int[width1 * height1]; //通过位图的大小创建像素点数组 newbmp.getPixels(pixels, 0, width1, 0, 0, width1, height1); int alpha = 0xFF << 24; for(int i = 0; i < height1; i++) { for(int j = 0; j < width1; j++) { int grey = pixels[width1 * i + j]; int red = ((grey & 0x00FF0000 ) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11); grey = alpha | (grey << 16) | (grey << 8) | grey; pixels[width1 * i + j] = grey; } } Bitmap result = Bitmap.createBitmap(width1, height1, Config.RGB_565); result.setPixels(pixels, 0, width1, 0, 0, width1, height1); /** * 第三步,计算平均值。 * 计算所有64个像素的灰度平均值。 */ int sum = 0; for(int i=0;i<result.getWidth();i++){ for(int j=0;j<result.getHeight();j++){ int x = result.getPixel(i, j); sum+=x; } } int ave = sum/(result.getWidth()*result.getHeight()); /** * 第四步,比较像素的灰度。 * 算法的精髓,简单、有趣,又充满深意。 * 将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。 */ StringBuffer sb = new StringBuffer(); for(int i=0;i<result.getWidth();i++){ for(int j=0;j<result.getHeight();j++){ int x = result.getPixel(i, j); if(x>ave){ sb.append("1"); }else{ sb.append("0"); } } } /** * 第五步,计算哈希值。 * 将上一步的比较结果,组合在一起成为一个字符串,这就是这张图片的指纹。组合的次序并不重要, * 只要保证所有图片都采用同样次序就行了(例如,自左到右、自顶向下、big-endian)。 */ return sb.toString();}
0 0
- 安卓感知哈希算法
- 安卓手机图片相似度对比--OpenCV感知哈希算法
- 感知哈希算法
- 感知哈希算法
- 感知哈希算法(pHash算法)
- 图片搜索之感知哈希算法
- 感知哈希算法--找到相似图片。
- 感知哈希算法 C#版
- 图像搜索--感知哈希算法
- iOS实现感知哈希算法
- 感知哈希算法介绍(pHash)
- 感知哈希算法Java实现
- 感知哈希算法的opencv实现
- 感知哈希算法原理与实现
- 感知哈希算法--python实现
- 感知哈希算法--实现图片搜索
- iOS实现感知哈希算法
- 以图搜图--感知哈希算法
- HDUOJ 1039
- Handler的定义:主要接受子线程发送的数据, 并用此数据配合主线程更新UI.
- runtime 机制
- 快速乘
- cocos2d-x-3.2 (一)两种方法画基本图形
- 安卓感知哈希算法
- github上优秀的android 开源项目
- 抽奖大转盘
- library search paths 路径带有空格,导致拆分成2个路径查询,出现ld: warning: directory not found for option错误
- Hdu 4539 郑厂长系列故事——排兵布阵 状态压缩
- tomcat6url请求400错误(%2F与%5C)
- 使用windows自带的录像功能psr.exe
- Android系统启动过程
- sql语句