From b3ce109a22298302a60a0f9ead3b0efe0363959e Mon Sep 17 00:00:00 2001 From: pverscha Date: Wed, 7 Jun 2017 19:57:34 +0200 Subject: [PATCH] MagicCrop is now fully working --- .../abyx/aurora/ExampleInstrumentedTest.java | 26 ++++++-- app/src/androidTest/res/drawable/delhaize.png | Bin 0 -> 13949 bytes .../java/be/abyx/aurora/ColourParser.java | 50 --------------- .../main/java/be/abyx/aurora/ImageUtils.java | 60 ++++++++++++------ 4 files changed, 62 insertions(+), 74 deletions(-) create mode 100644 app/src/androidTest/res/drawable/delhaize.png delete mode 100644 app/src/main/java/be/abyx/aurora/ColourParser.java diff --git a/app/src/androidTest/java/be/abyx/aurora/ExampleInstrumentedTest.java b/app/src/androidTest/java/be/abyx/aurora/ExampleInstrumentedTest.java index 7578ac8..72ba739 100644 --- a/app/src/androidTest/java/be/abyx/aurora/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/be/abyx/aurora/ExampleInstrumentedTest.java @@ -64,7 +64,7 @@ public void testSimpleGradientAurora() throws Exception { Bitmap gradient = factory.createAuroraBasedUponColour(output, 400, 800); - saveImageToExternalStorage(gradient); + saveImageToExternalStorage(gradient, Bitmap.CompressFormat.JPEG); } @Test @@ -78,23 +78,39 @@ public void testBlurryAurora() throws Exception { Bitmap gradient = factory.createAuroraBasedUponDrawable(redImage, new BlurryAurora(appContext), 1200, 1920); - saveImageToExternalStorage(gradient); + saveImageToExternalStorage(gradient, Bitmap.CompressFormat.JPEG); } - private void saveImageToExternalStorage(Bitmap finalBitmap) { + @Test + public void testMagicCrop() throws Exception { + Context appContext = InstrumentationRegistry.getTargetContext(); + + Drawable logo = getInstrumentation().getContext().getResources().getDrawable(be.abyx.aurora.test.R.drawable.delhaize, null); + ImageUtils utils = new ImageUtils(appContext); + + Bitmap cropped = utils.magicCrop(((BitmapDrawable) logo).getBitmap(), Color.WHITE, 0.75f); + saveImageToExternalStorage(cropped, Bitmap.CompressFormat.PNG); + } + + private void saveImageToExternalStorage(Bitmap finalBitmap, Bitmap.CompressFormat format) { File myDir = getInstrumentation().getContext().getExternalFilesDir("gradient"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); - String fname = "Image-" + n + ".jpg"; + String fname; + if (format.equals(Bitmap.CompressFormat.JPEG)) { + fname = "Image-" + n + ".jpg"; + } else { + fname = "Image-" + n + ".png"; + } File file = new File(myDir, fname); System.out.println(file.getAbsolutePath()); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); - finalBitmap.compress(Bitmap.CompressFormat.JPEG, 98, out); + finalBitmap.compress(format, 100, out); out.flush(); out.close(); } diff --git a/app/src/androidTest/res/drawable/delhaize.png b/app/src/androidTest/res/drawable/delhaize.png new file mode 100644 index 0000000000000000000000000000000000000000..0f3e4a6febda06a6470705e5a8d917332e0fb621 GIT binary patch literal 13949 zcmaL8cRZDE{4jo7386$~Q%16qvW}IMO*9b44#&(o_DYgu?`)1;C)PW#kt+VSqF&J6N z&6fl}6t9%^T_ETx-PuKCm4O4ZAV$}Rdah3#EnVGBonJ!tEga2Wa;VswTD{bHX=>r= z)c#T$g2X6Q6z@Ovz^_ec*%=N_oH~sim@Aw72*ee|z%i>5U&{|%;zOBc+p^@ye*0o~k-dK5H}wZi*a?{v?OB?Xx$2zQK*zVAW& zYW~zToD@Q%b4&N@hn*Ls#KKW&ajqG1;;nINkUSS4WW^IPq!^tqN9f@7Q&J&^9t&-+ z`>$#c4$GF*F$TK2CkLy%jJYHbn(y$}T3wH z6eQmjm~S{!*rk(2av6#`Kd-_cZ41k$fuiC}E^I91c3Sppsw$uTx0^oR8P=LCd78}- zJmY<5_=>Pjz@cqq|nH0 zSXEC*MmA#+Z-4p&pUU0es=sS|doqv8`BO%5!&jRR)}a6s@z|8a5})#~*WCv3zm&vk zi#DI!HOPnmYIfUh9zR}c-Y)jG$mnw!CxT`sYF+j3Y!vTLYKa{v&`eh-W5(h;rrP|X z2l(cIC@7sHiYcK_+$v5$LxB2Z43ykRzM7tBY@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LT*f~CpO z6{OtpAAjbP(vr6gV=~(&ggvtbW$dZ%+Ev^|LuzO`cRO^75pMTbts}qtM0XaiIbQ2} z0I*^-a2W?J)jEDUb3_4J+!KpE8ouS*2II)ScS)yt5(K|!x|pkI+&s{6k7t*8wI?JbUj zIxZ>QqdJLIP2GfM)(bE+sk3b8%$|-Vzz7 zyKg0PQmR3@weGiTde9oh2I(yU#gBQV!_+#8&|hq0KdIh(#omfmL&>@1{7y63BQvOb zyh$Yg_2qcCnr?l)&%+b$?@?6fwdM;cNLCS{1}b4ajxSEUccB0Z&;bq3B78mR(IR+0 z(>Xn|m=QKTu_!>V>d6@mjVFU%3B|$WcIAQKHNq;T@hv8q3+JH#<+DEbW?DP9;M1D@ zx&%=#X>c@%^QCcW_}wH1P6FuIhL@(tbwocu2XR@q%{BbOTbx2h)lBp7txr`TaApa4mbZx_FOoY*w=?{G|=SIi@FsD;H1q|pVHqsoc+ zs&UUbC!l}RiUWoDtbydvEAAC3ddtEAQyeAKc;?2Iz@Y&9^@MQ;{3QoyhC@*La+k#!VfP%4OGODSpmqa6Y6E0e)S8@MmJvffjl=0LG0j8B|efyuIJa=fJ#}&X| z^r)3H@fz`@A_u!-uSarILIHxlb>Lvf6gbl|Qg7~qxcbe_?ICfb6fJ}+^tPCaXb$<+ zRc(PQd9mnwuzpx9+^`LHP-^#~sB^2F?l;r@)C_PXHuhV;07={jH-AHoYJkt=lKJix z-^>xZ+<{k*uJ1vODNz`3B1uE5(p7*^1RDR3Kzgs--KHP|z3NP;pInb7Kii~{6Qq#z z?k^EK;M%Fz*FOM9roeHQ$9qHp_FFuKJfl4EsYhQ0XHr!#;iv_J(?$P|daw_}>`WZ6 qRq)fIh}nybLiZ4+`RA1Q0;I)mdSO1`gH#@BDwZgiy#8O;38%0C literal 0 HcmV?d00001 diff --git a/app/src/main/java/be/abyx/aurora/ColourParser.java b/app/src/main/java/be/abyx/aurora/ColourParser.java deleted file mode 100644 index 1463466..0000000 --- a/app/src/main/java/be/abyx/aurora/ColourParser.java +++ /dev/null @@ -1,50 +0,0 @@ -package be.abyx.aurora; - -import android.graphics.Color; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -/** - * Parse colour data that's stored inside of a JSON-file with a specific format. - * - * @author Pieter Verschaffelt - */ -public class ColourParser { - public List parseFile(InputStream data) throws IOException, JSONException { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[4096]; - int length; - - while ((length = data.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - - String JSON = result.toString("UTF-8"); - - JSONObject parsedJSON = new JSONObject(JSON); - JSONArray parsedColours = parsedJSON.getJSONArray("data"); - - List output = new ArrayList<>(); - - for (int i = 0; i < parsedColours.length(); i++) { - JSONObject obj = parsedColours.getJSONObject(i); - JSONObject rgbObj = obj.getJSONObject("rgb"); - - int red = rgbObj.getInt("r"); - int green = rgbObj.getInt("g"); - int blue = rgbObj.getInt("b"); - - output.add(Color.argb(255, red, green, blue)); - } - - return output; - } -} diff --git a/app/src/main/java/be/abyx/aurora/ImageUtils.java b/app/src/main/java/be/abyx/aurora/ImageUtils.java index 36d1ada..b2e2756 100644 --- a/app/src/main/java/be/abyx/aurora/ImageUtils.java +++ b/app/src/main/java/be/abyx/aurora/ImageUtils.java @@ -26,43 +26,65 @@ public ImageUtils(Context context) { * * @param input A Bitmap that should be automatically cropped. * @param colour The colour of the edges that should be removed. + * @param tolerance How much a colour can deviate from the real given value and should still + * be removed (A value between 0 and 1 where 1 indicates full tolerance). + * @return Bitmap A new Bitmap whose corners are removed. */ - public void magicCrop(Bitmap input, int colour) { - BitmapFactory.Options opts = new BitmapFactory.Options(); - opts.inScaled = false; - - Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), R.raw.aurora_fancy_template, opts); - - int width = originalBitmap.getWidth(); - int height = originalBitmap.getHeight(); + public Bitmap magicCrop(Bitmap input, int colour, float tolerance) { + int width = input.getWidth(); + int height = input.getHeight(); int[] pixels = new int[width * height]; - originalBitmap.getPixels(pixels, 0, width, 0, 0, width, height); + input.getPixels(pixels, 0, width, 0, 0, width, height); // We're working with a Stack-based approach of the Flood Fill algorithm to determine // the edges that should get removed. Stack coordinates = new Stack<>(); // Start filling from the four corners of the image - coordinates.push(new ImageCoordinate(0, 0)); coordinates.push(new ImageCoordinate(0, height - 1)); coordinates.push(new ImageCoordinate(width - 1, height - 1)); coordinates.push(new ImageCoordinate(width - 1, 0)); + coordinates.push(new ImageCoordinate(0, 0)); + + int referenceRed = Color.red(colour); + int referenceGreen = Color.green(colour); + int referenceBlue = Color.blue(colour); + int sum = referenceBlue + referenceGreen + referenceRed; + float treshold = 3 * 255 * tolerance; while (!coordinates.isEmpty()) { ImageCoordinate current = coordinates.pop(); - if (pixels[current.getY() * height + current.getX()] == colour) { - pixels[current.getY() * height + current.getX()] = Color.TRANSPARENT; + if (validPosition(current.getX(), current.getY(), width, height) && pixels[current.getY() * width + current.getX()] == colour) { + int pixel = pixels[current.getY() * width + current.getX()]; + int red = Color.red(pixel); + int blue = Color.blue(pixel); + int green = Color.green(pixel); + + float difference = Math.abs((red + blue + green) - sum); - // Reuse current ImageCoordinate-object. - current.setY(current.getY() + 1); - coordinates.push(current); - coordinates.push(new ImageCoordinate(current.getX(), current.getY() - 1)); - coordinates.push(new ImageCoordinate(current.getX() + 1, current.getY())); - coordinates.push(new ImageCoordinate(current.getX() - 1, current.getY())); + if (difference <= treshold) { + pixels[current.getY() * width + current.getX()] = Color.TRANSPARENT; + + // Reuse current ImageCoordinate-object. + coordinates.push(new ImageCoordinate(current.getX() + 1, current.getY())); + coordinates.push(new ImageCoordinate(current.getX() - 1, current.getY())); + coordinates.push(new ImageCoordinate(current.getX(), current.getY() - 1)); + current.setY(current.getY() + 1); + coordinates.push(current); + } } } - input.setPixels(pixels, 0, width, 0, 0, width, height); + Bitmap output = input.copy(input.getConfig(), true); + output.setHasAlpha(true); + output.setPixels(pixels, 0, width, 0, 0, width, height); + + return output; + } + + + private boolean validPosition(int x, int y, int width, int height) { + return x >= 0 && x < width && y >= 0 && y < height; } }