MonkeyRunner을 이용해서 이미지를 분석할 수 있는데.(속도면에서 엄청 늦은 경우가 존재한다.)

 이것을 이용하기 위해서는 부분적으로 사진(이미지)을 잘라내서 사용하는 방법을 알아야 될 것이다.


 그래서 SubImage을 사용하는 방법을 작성하고자 한다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.awt.image.BufferedImage;
 
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.IChimpImage;
 
import com.android.chimpchat.adb.AdbChimpDevice;
 
import com.android.chimpchat.core.ChimpImageBase;
 
public class MonkeyTest {
    public static void main(String[] args) {
        
        IChimpDevice device = null;
        
        try
        {
            // sdk/platform-tools has to be in PATH env variable in order to find adb
            device = new AdbBackend().waitForConnection();            
        }
        catch(java.lang.NullPointerException e)
        {
            System.out.println("Error");
            System.exit(-1);
        }
        
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
 
        // Take a snapshot and save to out.png
        IChimpImage subImage = device.takeSnapshot().getSubImage(4040250250);
        subImage.writeToFile("out.png""png");
        
        device.dispose();
        
        System.exit(0);
    }
}
cs



 여기서 getSubImage는


 x위치, y위치, 넓이, 높이 이런 순으로 작성하면 된다.


 단, 여기서 생성되는 사각형이 현재 이미지의 전체의 위치로 옮겨 가면 안된다.


 예을 들어서 가로 100, 세로 100인 이미지에서 x위치 99에 넓이 100 이렇게 하면, 오작동이 일어나게 된다.

Posted by JunkMam
,

 sameAs라는 메소드가 있고, 그것을 구현하는 내용을 한번 작성해봤다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.awt.image.BufferedImage;
 
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.IChimpImage;
 
import com.android.chimpchat.adb.AdbChimpDevice;
 
import com.android.chimpchat.core.ChimpImageBase;
 
public class MonkeyTest {
    public static void main(String[] args) {
        
        IChimpDevice device = null;
        
        try
        {
            // sdk/platform-tools has to be in PATH env variable in order to find adb
            device = new AdbBackend().waitForConnection();            
        }
        catch(java.lang.NullPointerException e)
        {
            System.out.println("Error");
            System.exit(-1);
        }
        
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
 
        // Take a snapshot and save to out.png
        //device.takeSnapshot().writeToFile("out.png", "png");
        
        IChimpImage deviceImage = device.takeSnapshot();
        deviceImage.writeToFile("out_1.png""png");
        IChimpImage fileImage = ChimpImageBase.loadImageFromFile("out.png");
        fileImage.writeToFile("out_2.png""png");
        
        sameAs(fileImage,deviceImage,0.9);
        
        if(fileImage.sameAs(deviceImage, 0.75))
        {
            System.out.println("Sames");
        }
        else
        {
            System.out.println("Not");
        }
 
        device.dispose();
        
        System.exit(0);
    }
    
    public static boolean sameAs(IChimpImage other, IChimpImage sames, double percent){
        
        BufferedImage otherImage = other.getBufferedImage();
        BufferedImage samesImage = sames.getBufferedImage();
        
        //Easy size check
        if(otherImage.getWidth()!= samesImage.getWidth()){
            return false;
        }
        
        if(otherImage.getHeight()!=samesImage.getHeight()){
            return false;
        }
        
        int[] otherPixel = new int[1];
        int[] samesPixel = new int[1];
        
        int width = samesImage.getWidth();
        int height = samesImage.getHeight();
        
        int numDiffPixels = 0;
        // Now, go through pixel-by-pixel and check that the images are the same;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (samesImage.getRGB(x, y) != otherImage.getRGB(x, y)) {
                    System.out.println(samesImage.getRGB(x, y)+":"+otherImage.getRGB(x, y));
                    numDiffPixels++;
                }
            }
        }
        double numberPixels = (height * width);
        double diffPercent = numDiffPixels / numberPixels;
        
        return percent <= 1.0 - diffPercent;
    }
}
cs



 하지만, 왜 차이가 생기는지는 잘 모르겠다.


 값이 동일하게 나온 것이라고 생각하는데...


 일단, 이렇게 구현을 할 수 있다면, 특정 위치를 조작해서 처리할 수 있는 방법도 만들 수 있다.

Posted by JunkMam
,

 MonkeyRunner을 연결해서 사용하는 것이 있게 된다.


 여기서, 이미지를 받아 들이고, 이미지를 비교하는 소스를 제작했다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.IChimpImage;
 
import com.android.chimpchat.adb.AdbChimpDevice;
 
import com.android.chimpchat.core.ChimpImageBase;
 
public class MonkeyTest {
    public static void main(String[] args) {
        
        IChimpDevice device = null;
        
        try
        {
            // sdk/platform-tools has to be in PATH env variable in order to find adb
            device = new AdbBackend().waitForConnection();            
        }
        catch(java.lang.NullPointerException e)
        {
            System.out.println("Error");
            System.exit(-1);
        }
        
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
 
        // Take a snapshot and save to out.png
        //device.takeSnapshot().writeToFile("out.png", "png");
        
        IChimpImage deviceImage = device.takeSnapshot();
        deviceImage.writeToFile("out_1.png""png");
        IChimpImage fileImage = ChimpImageBase.loadImageFromFile("out.png");
        fileImage.writeToFile("out_2.png""png");
        
        if(fileImage.sameAs(deviceImage, 0.75))
        {
            System.out.println("Sames");
        }
        else
        {
            System.out.println("Not");
        }
 
        device.dispose();
        
        System.exit(0);
    }
}
cs


 여기서 sameAs라는 것에서 1.0에 가까우면, 원본이랑 동일하게 보는 것이 맞다. 하지만, 제대로 동작이 안되는 경우가 있다.


 여기서, MonkeyRunner라는 분석이라고하여서 해당 소스를 올린 블로그가 있다.


 참조하면 도움이 될 것이다.


 http://goodtogreate.tistory.com/entry/monkeyrunner%EC%9D%98-%EB%B6%84%EC%84%9D

Posted by JunkMam
,

 MonkeyRunner를 Java에 연결해서 사용이 가능하다. 라는 식으로 예제 소스와 내용을 설명해주었다.


 이것을 제대로 Java의 소스를 작성하는 방법을 포스팅 할려고 한다.


 먼저, Eclipse을 이용해서 Java Project을 만들어 놓는다.



 프로젝트에서 Properties(환경 설정)에 들어가서 환경을 설정하는 창을 띄운다.



 여기서 Java Build Path(Java에서 Library등을 가지고오게하는 설정이다.)에서 들어가면, Library을 설정할 수 있는 탭이 있다.(이미 추가를 시킨 상태이다.)


 여기서 [Android SDK 경로]\tools\lib\MonkeyRunner.jar 을 가지고온다.


 여기서 MonkeyRunner라는 것은 공개 소스로 제작되어 있는 부분이 있다.(그리고 MonkeyRunner가 아닌, chimpchat.jar을 가지고와서 사용하는게 더 올바른 방식이다.


 MonkeyRunner는 Java + Python인 상태이기 때문에, Python의 정보를 가지고 있지 않는다면, 사용을 제대로 할 수 없는 것을 알 수 있다.

 하지만, MonkeyRunner에서 있는 Python을 분석해서 chimpchar에게 정보를 보내는것을 알 수 있기 때문에, 다음 링크를 읽으면서 분석하면, 이해에 도움이 될 것이다.


 링크 : https://android.googlesource.com/platform/tools/swt/+/android-4.4_r1.1/monkeyrunner/src/main/java/com/android/monkeyrunner


 이제, 다음과 같은 소스를 보여주면서, 자세하게 설명을 하도록 하겠다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.IChimpImage;
 
public class MonkeyTest {
    public static void main(String[] args) {
        IChimpDevice device = null;
        try
        {
            // sdk/platform-tools has to be in PATH env variable in order to find adb
            device = new AdbBackend().waitForConnection();            
        }
        catch(java.lang.NullPointerException e)
        {
            System.out.printf("Error");
            System.exit(-1);
        }
 
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
 
        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("out.png""png");
 
        device.dispose();
        
        System.exit(0);
    }
}
cs


 이것에서 device라는 것은 ChimpDevice라는 것으로, Adb에 연결하는 방식으로,


 Python에서 있는 MonkeyDevice와 비슷하다고 보면, 이해가 될 것이다.


 MonkeyRunner는 AdbBackend라는 클래스와 동일하다고 보면 될 것이다.


 AdbBackend라는 클래스는 ADB와 연결되서 통신하는 클래스이다.


 여기서, waitForConnection은 2가지의 함수가 존재하는데.


 waitForConnection()과 waitForConnection(String)이 있다.


 여기서 String에는 ADB에서 있는 Device의 명칭을 작성하면, 해당 Device을 연결 할 수 있게 된다.


 여기서 device의 정보를 얻기 위해서는 ddmuilib이라는 것을 이용해야된다.


 거기에 대해서는 또 다른 방법을 찾아야 될 것이므로, 나중에 하도록 한다.


 Adb의 정보가 없다면, NullPointerException이라는 오류가 발생하는 경우가 있다.

 이 경우에 예외 처리하는게 좋다.


 현재 Device의 명칭을 알기 위해서 device.getProperty을 이용해서 알아보게 만들 수 있으며.


 device.takeSnapshot()을 이용해서 그림을 가지고 올 수 있다.


 writeToFile(String1, String2)에서 String1은 파일 명칭/경로를 뜻하는 것이고, String2는 파일의 포맷이다. 기본적으로 PNG을 기본적으로 가지고 있으며, PNG을 제외한, JPG가 적용이 가능하다.


 device.dispose();는 연결을 끊는 것이다.


 윗 방식을 이용하면, 스샷을 찍어서 png 포맷을 가진 out.png 파일이 생성된다.

Posted by JunkMam
,

 안드로이드 SDK을 업그레이드 혹은 설치하면서 자동으로 설치되는 것이 MonkeyRunner라고 했다.


 이것을 Java에 연결할 수도 있는데, 그 이유는 jar로 MonkeyRunner을 설정해놓았기 때문이다.


 MonkeyRunner는 python와 Java을 같이 연결해서 사용하는 방식으로 Jython이라는 것이다.


 그래서 MonkeyRunner.bat을 이용해서 Python으로 구성한 소스를 동작 시킬 수 있게 할 수 있다.


 Java을 MonkeyRunner로 연결하기 위해서 다음 링크를 보면 도움이 될 것이다.


 링크 : http://stackoverflow.com/questions/6686085/how-can-i-make-a-java-app-using-the-monkeyrunner-api


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
 
public class MonkeyTest {
    public static void main(String[] args) {
        // sdk/platform-tools has to be in PATH env variable in order to find adb
        IChimpDevice device = new AdbBackend().waitForConnection();
 
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
 
        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("out.png"null);
 
        device.dispose();
    }
}
cs


 윗 예시 소스는 윗 링크에서 가지고온 소스이다.


 여기서, MonkeyRunner가 아닌, MonkeyDevice라는 게 있다.


 원래, MonkeyRunner라는 것은 ADB을 연결해서 처리해주는 중간자 역할을 하는 클래스다.


 그래서 MonkeyRunner을 사용해도 되고, adb을 바로 연결하는 AdbBackend라는 녀석을 사용해도 된다.


 나머지는 MonkeyRunner의 예시와 유사하다.


 단, 연결을 끊고 다시 연결하는 행위를 할 수 있다.(MonkeyRunner는 Python을 다 읽고 난 후에 알아서 종료하게 된다.)


Posted by JunkMam
,