-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use OpenCV for Android libraries for image processing
- Loading branch information
1 parent
6276d46
commit 0970689
Showing
114 changed files
with
26,557 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/src/main/java/efokschaner/infinityloopsolver/Debug.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package efokschaner.infinityloopsolver; | ||
|
||
|
||
import android.app.UiAutomation; | ||
import android.graphics.Bitmap; | ||
|
||
import org.opencv.android.Utils; | ||
import org.opencv.core.Mat; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.ProtocolException; | ||
import java.net.URL; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class Debug { | ||
public static void sendBitmap(Bitmap bitmap) { | ||
try { | ||
String timestamp = new SimpleDateFormat("HH_mm_ss").format(new Date()); | ||
URL url = new URL("http://10.0.2.2:8888/" + timestamp + ".png"); | ||
try { | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
try { | ||
conn.setDoOutput(true); | ||
conn.setChunkedStreamingMode(0); | ||
conn.setRequestMethod("POST"); | ||
conn.setRequestProperty("Content-Type", "application/octet-stream"); | ||
try (OutputStream ostream = conn.getOutputStream()) { | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
final int responseCode = conn.getResponseCode(); | ||
if (!(responseCode >= 200 && responseCode < 300)) { | ||
throw new AssertionError(String.format("Http response was: %d", responseCode)); | ||
} | ||
conn.getResponseMessage(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
while (in.readLine() != null){ | ||
// ignore contents | ||
} | ||
in.close(); | ||
} catch (ProtocolException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
conn.disconnect(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void sendMatrix(Mat m) { | ||
Bitmap b2 = Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888); | ||
Utils.matToBitmap(m, b2); | ||
sendBitmap(b2); | ||
b2.recycle(); | ||
} | ||
|
||
public static void sendScreenshot(UiAutomation uiAutomation) { | ||
Bitmap b = uiAutomation.takeScreenshot(); | ||
try{ | ||
sendBitmap(b); | ||
} finally { | ||
b.recycle(); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
app/src/main/java/efokschaner/infinityloopsolver/GameState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package efokschaner.infinityloopsolver; | ||
|
||
|
||
public class GameState { | ||
public static class Direction { | ||
public static final int NONE = 0; | ||
public static final int UP = 1; | ||
public static final int RIGHT = 1 << 1; | ||
public static final int DOWN = 1 << 2; | ||
public static final int LEFT = 1 << 3; | ||
|
||
public static final int[] ALL = {UP, RIGHT, DOWN, LEFT}; | ||
} | ||
|
||
// Represents offset from the canonical orientations | ||
public static class TileOrientation { | ||
public static final TileOrientation ZERO = new TileOrientation(0); | ||
public static final TileOrientation QUARTER = new TileOrientation(1); | ||
public static final TileOrientation HALF = new TileOrientation(2); | ||
public static final TileOrientation THREE_QUARTERS = new TileOrientation(3); | ||
|
||
private int mValue; | ||
|
||
TileOrientation(int value) { | ||
mValue = value; | ||
} | ||
|
||
public TileOrientation rotate(int num) { | ||
return new TileOrientation((mValue + num) % 3); | ||
} | ||
|
||
public TileOrientation rotate() { | ||
return new TileOrientation(0); | ||
} | ||
} | ||
|
||
public enum TileType { | ||
EMPTY(Direction.NONE, new TileOrientation[]{ | ||
TileOrientation.ZERO | ||
}), | ||
END(Direction.UP, new TileOrientation[]{ | ||
TileOrientation.ZERO, | ||
TileOrientation.QUARTER, | ||
TileOrientation.HALF, | ||
TileOrientation.THREE_QUARTERS | ||
}), | ||
LINE(Direction.UP| Direction.DOWN, new TileOrientation[]{ | ||
TileOrientation.ZERO, | ||
TileOrientation.QUARTER | ||
}), | ||
CORNER(Direction.UP| Direction.RIGHT, new TileOrientation[]{ | ||
TileOrientation.ZERO, | ||
TileOrientation.QUARTER, | ||
TileOrientation.HALF, | ||
TileOrientation.THREE_QUARTERS | ||
}), | ||
TEE(Direction.UP| Direction.RIGHT| Direction.DOWN, new TileOrientation[]{ | ||
TileOrientation.ZERO, | ||
TileOrientation.QUARTER, | ||
TileOrientation.HALF, | ||
TileOrientation.THREE_QUARTERS | ||
}), | ||
CROSS(Direction.UP| Direction.RIGHT| Direction.DOWN| Direction.LEFT, new TileOrientation[]{ | ||
TileOrientation.ZERO | ||
}); | ||
|
||
|
||
private final int mConnectionDirections; | ||
private final TileOrientation[] mPossibleOrientations; | ||
|
||
// connectionDirections are which directions the tile connects to in its default orientation | ||
// possibleOrientations allows us to cull orientations that are the same for the purposes of the game | ||
TileType(int connectionDirections, TileOrientation[] possibleOrientations) { | ||
mConnectionDirections = connectionDirections; | ||
mPossibleOrientations = possibleOrientations; | ||
} | ||
} | ||
|
||
public static class TileState { | ||
TileType type; | ||
TileOrientation orientation; | ||
boolean isOrientationSolved = false; | ||
} | ||
|
||
private TileState[][] mGrid; | ||
|
||
public GameState(TileState[][] grid) { | ||
mGrid = grid; | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/efokschaner/infinityloopsolver/ImageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package efokschaner.infinityloopsolver; | ||
|
||
import android.graphics.Bitmap; | ||
|
||
import org.opencv.android.Utils; | ||
import org.opencv.core.Mat; | ||
|
||
|
||
public class ImageProcessor { | ||
public static GameState getGameStateFromImage(Bitmap b) { | ||
Mat m = new Mat(); | ||
Utils.bitmapToMat(b, m); | ||
Debug.sendMatrix(m); | ||
return new GameState(new GameState.TileState[2][2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.