임시

ㅇ임시

 

package com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class SecondClass {

 public static void main(String[] args) {
 
 
  //그림1을 얻어옴
  //BufferedImage bimg=txtToImg(“경제”, “돋움”, 12);//네이버
  BufferedImage bimg=txtToImg(“애플워치”, “굴림”, 12);
 
  //스크린샷 찍음
  BufferedImage scrn=screenShot();
 
  //그림1과 스크린샷을 비교
  boolean includ=includeImg(scrn, bimg);
 
  System.out.println(includ);
 }
 //////////////////////////////////////////////////////////////
 //글자를 그림으로 변경
 public static BufferedImage txtToImg(String str,String fontName,int fontSize){
  //int fontSize=i+1;
  //String str=”휴지통”;
 
  BufferedImage img = new BufferedImage(fontSize*str.length(), (int)(fontSize*1), BufferedImage.TYPE_INT_RGB);
 
  Graphics2D graphics = img.createGraphics();
 
  //흰색으로 색칠
  graphics.fillRect(0, 0, fontSize*str.length(), (int)(fontSize*1));
  //graphics.setBackground(new Color(255,100,50));
  graphics.setColor(new Color(0,0,0));
 
  //글자를 출력
  graphics.setFont(new Font(fontName, 0, fontSize));
  graphics.drawString(str, 0, (int)(fontSize*0.9));
 
  //graphics.setFont(new Font(“돋움”, 0, fontSize));
  //graphics.drawString(str, 0, (int)(fontSize*0.9));
 
 
  File file=new File(“c:/a/make.bmp”);
  try{
    ImageIO.write(img, “bmp”, file);
  }catch(Exception ex){System.out.println(ex.getMessage());}
 
  return img;
 }
 ///////////////////////////////////////////////////////////
 //화면 스크린샷을 버퍼드 이미지에 저장하는 메소드
 public static BufferedImage screenShot(){
  //스크린샷하기
  BufferedImage scrn=null;//버퍼드이미지 선언
  try{
   //해상도 구하기
   Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
  
   //스크린샷 찍기
   Robot robot=new Robot();
   scrn=robot.createScreenCapture(new Rectangle(0, 0, (int)screenSize.getWidth(), (int)screenSize.getHeight()));
  
   //스크린샷 파일화
   //ImageIO.write(scrn, “bmp”, new File(“c:/a/res.bmp”));
  
  }catch(Exception ex){System.out.println(ex.getMessage());}
  return scrn;
 }
 ////////////////////////////////////////////////////////////////////////
 public static boolean checkAround(BufferedImage img,int x,int y,int mode){
  //하나라도 원하는 색상이 있으면 true를 돌려줌
  boolean res=false;
 
  int startX=x;
  int startY=y;
  int endX=x;
  int endY=y;
  int gap=1;
  try{
   //갭 조절
   if(x-gap>0){startX=x-gap;}else{startX=x;}
   if(y-gap>0){startY=y-gap;}else{startY=y;}
   if(x+gap<=img.getWidth()-1){endX=x+gap;}else{endX=img.getWidth()-1;}
   if(y+gap<=img.getHeight()-1){endY=y+gap;}else{endY=img.getHeight()-1;}
  
   for(int i=startX;i<=endX;i++){
    for(int j=startY;j<=endY;j++){
     if(mode==0){//흑색
      if(isDark(img.getRGB(i, j))){
       res=true;
       break;
      }
     }
     else if(mode==1){//흰색
      if(isLight(img.getRGB(i, j))){
       res=true;
       break;
      }
     }
    }
   
   }
  }catch(Exception ex){System.out.println(ex.getMessage()+” “+startX+” “+endX+” “+startY+” “+endY);}
  return res;
 }
////////////////////////////////////////////////////////////////////
 public static boolean isDark(int rgb){
  boolean res=false;
  Color c=new Color(rgb);
  if(c.getRed()+c.getGreen()+c.getBlue() < 300){
   res=true;
  }
   
  return res;
 }
 
 public static boolean isLight(int rgb){
  boolean res=false;
  Color c=new Color(rgb);
  if(c.getRed()+c.getGreen()+c.getBlue() > 700){
   res=true;
  }
   
  return res;
 }
 
 ////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////
 //큰 그림에 작은 그림이 속하는지(포함하는지) 검사하는 메소드
 public static boolean includeImg(BufferedImage bigImg, BufferedImage smImg){
  boolean res=false;
 
  for(int xs=0;xs<bigImg.getWidth()-smImg.getWidth();xs++){
   for(int ys=0;ys<bigImg.getHeight()-smImg.getHeight();ys++){
 
    //System.out.println(xs+” “+ys+”를 검사”);
    for(int i=xs;i<xs+smImg.getWidth();i++){
     for(int j=ys;j<ys+smImg.getHeight();j++){
      //System.out.println(i+” “+j);
      //같으면
      int bigRGB=bigImg.getRGB(i, j);
      int smallRGB=smImg.getRGB(i-xs, j-ys);
     
      Color bigClr=new Color(bigRGB);
      Color smallClr=new Color(smallRGB);
//      if(bigClr.getBlue()==0){
//       System.out.println(bigRGB+ ” “+bigClr);
//      }

      if(smallRGB==-1){//작은 그림이 흰색이라면 배경
       //같은 흰색인지 검사
       if(checkAround(bigImg, i, j, 1)){//하나라도 흰색이 있으면
       //같은 흰색계열이라면 오케이
        res=true;
        continue;
       }
       else{
       //어두운 계열이라면 취소
        res=false;
        break;
       }
      }
      else{//작은 그림이 흰색이 아니라면 글자임.
       //같은 검은색인지 검사
       if(checkAround(bigImg, i, j, 0)){//하나라도 검은색이 있으면
        res=true;
        continue;
       }
       else{
        res=false;
        break;
       }
      }
     }
     if(res==false){break;}//다르면 나감
    }
    if(res==true){
     
     System.out.println(“발견좌표:”+xs+”,”+ys+” w:”+smImg.getWidth()+” h:”+smImg.getHeight());
    
       BufferedImage scrn=null;//버퍼드이미지 선언
       try{
        //스크린샷 찍기
        Robot robot=new Robot();
        scrn=robot.createScreenCapture(new Rectangle(xs, ys, smImg.getWidth(), smImg.getHeight()));
       
        //스크린샷 파일화
        ImageIO.write(scrn, “bmp”, new File(“c:/a/result.bmp”));
       
       }catch(Exception ex){System.out.println(ex.getMessage());}
   
     break;
    }
   }
   if(res==true){break;}
  }
  return res;
 }

}