zoukankan      html  css  js  c++  java
  • Java实现图像对比类

    
    
    package com.function;
    
    import java.awt.image.BufferedImage;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.*;
    import javax.imageio.ImageIO;
    
    /*
     * 这个是一个照片比对功能实现类
     */
    public class Tools_PhotoMatching {
        public int mactching(String contrast_photo ,String test_photo) {
            int contras_rgb[] = new int[3];
            int test_rgb[] = new int[3];
            int k = 0;
            File contrastfile = new File(contrast_photo);  //对比照片路径
            File testfile = new File(test_photo);    //测试照片路径
            BufferedImage contrasphoto = null;
            BufferedImage testphoto = null;
            try {
                contrasphoto = ImageIO.read(contrastfile);
                testphoto = ImageIO.read(testfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int contraswidth = contrasphoto.getWidth();
            int contrasheight = contrasphoto.getHeight();        
            int testwidth = testphoto.getWidth();
            int testheight = testphoto.getHeight();    
            //File file = new File("C:\Users\prize\Desktop\TOOLS\DCIM\Camera\log.txt");
            //File file1 = new File("C:\Users\prize\Desktop\TOOLS\DCIM\Camera\log1.txt");
            try {
                //FileWriter fileWriter = new  FileWriter(file);
                //FileWriter fileWriter1 = new  FileWriter(file1);
                //BufferedWriter bufferedWriter  = new BufferedWriter(fileWriter);
                //BufferedWriter bufferedWriter2 = new BufferedWriter(fileWriter1);        
            for (int i = 0; i < testwidth; i++) {
                for (int j = 0; j < testheight; j++) {                
                    //StringBuffer astringBuffer1 = new StringBuffer();
                    //StringBuffer bstringBuffer1 = new StringBuffer();                
                    int pixel1 = contrasphoto.getRGB(i, j);
                    int pixle2= testphoto.getRGB(i, j);
                    contras_rgb[0] = (pixel1 & 0xff0000) >> 16;  //将值转化成16进制
                    contras_rgb[1] = (pixel1 & 0xff00) >> 8;  
                    contras_rgb[2] = (pixel1 & 0xff);             
                    //astringBuffer1.append(i+"行"+j+"列   R="+contras_rgb[0]+"G="+contras_rgb[1]+"B="+contras_rgb[2] );
                    //bufferedWriter.write(astringBuffer1.toString());
                    //bufferedWriter.newLine();                            
                    test_rgb[0] = (pixle2 & 0xff0000) >> 16;  
                    test_rgb[1] = (pixle2 & 0xff00) >> 8;  
                    test_rgb[2] = (pixle2 & 0xff);
                    //bstringBuffer1.append(i+"行"+j+"列   R="+test_rgb[0]+"G="+test_rgb[1]+"B="+test_rgb[2]);                
                    //bufferedWriter2.write(bstringBuffer1.toString());
                    //bufferedWriter2.newLine();                
                    int Difference_R = Math.abs(contras_rgb[0]-test_rgb[0]);  //相减并且求绝对值
                    int Difference_G= Math.abs(contras_rgb[1]-test_rgb[1]);
                    int Difference_B= Math.abs(contras_rgb[2]-test_rgb[2]);
                    //System.out.println("第"+j+"次:"+Math.abs(Difference));                
                    final int R = 35; //35
                    final int L = 130;
                    int Q = Difference_R+Difference_G+Difference_B;
                    if (Difference_R > R && Difference_G > R && Q>L) {    //判断每个值的浮动不超过35            
                        k++;
                    }else if (Difference_G >R && Difference_B>R && Q>L) {
                        k++;            
                    }else if (Difference_R > R && Difference_B>R && Q>L) {
                        k++;
                        
                    }                                    
                }
                
            }
            //bufferedWriter.close();
            //bufferedWriter2.close();
            //fileWriter.close();
            //fileWriter1.close();
            
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        
            return k;        
        }
    
    }
     



  • 相关阅读:
    UE4 ios环境搭建备忘
    我所理解的打击感 逐帧分析过几十款游戏的开发者经验分享
    可重入锁和不可重入锁
    Java中JDK和JRE的区别是什么?它们的作用分别是什么?
    Java开发岗位面试题
    详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
    docker:轻量级图形页面管理工具Portainer
    Docker整合dockerfly实现UI界面管理(单机版)
    docker for mac的JSON配置文件中的hosts项修改后无法生效
    Maven [ERROR] 不再支持源选项 5。请使用 6 或更高版本
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/9708645.html
Copyright © 2011-2022 走看看