|
@@ -0,0 +1,96 @@
|
|
|
|
+package edesarrollos.printer;
|
|
|
|
+
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.graphics.Bitmap;
|
|
|
|
+import android.graphics.Canvas;
|
|
|
|
+import android.graphics.Matrix;
|
|
|
|
+import android.graphics.Paint;
|
|
|
|
+import android.util.DisplayMetrics;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by NotMe on 04/05/2015.
|
|
|
|
+ */
|
|
|
|
+public class ImageUtils {
|
|
|
|
+
|
|
|
|
+ public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
|
|
|
|
+ try {
|
|
|
|
+ Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.RGB_565);
|
|
|
|
+
|
|
|
|
+ float ratioX = newWidth / (float) bitmap.getWidth();
|
|
|
|
+ float ratioY = newHeight / (float) bitmap.getHeight();
|
|
|
|
+ float middleX = newWidth / 2.0f;
|
|
|
|
+ float middleY = newHeight / 2.0f;
|
|
|
|
+
|
|
|
|
+ Matrix scaleMatrix = new Matrix();
|
|
|
|
+ scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
|
|
|
|
+
|
|
|
|
+ Canvas canvas = new Canvas(scaledBitmap);
|
|
|
|
+ canvas.setMatrix(scaleMatrix);
|
|
|
|
+ canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
|
|
|
|
+
|
|
|
|
+ return scaledBitmap;
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return bitmap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
|
|
|
|
+ try {
|
|
|
|
+ Matrix rotateMatrix = new Matrix();
|
|
|
|
+ rotateMatrix.postRotate(angle);
|
|
|
|
+
|
|
|
|
+ return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, true);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return bitmap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Bitmap cropBitmapToBoundingBox(Bitmap picToCrop, int unusedSpaceColor) {
|
|
|
|
+ int[] pixels = new int[picToCrop.getHeight() * picToCrop.getWidth()];
|
|
|
|
+ int marginTop = 0, marginBottom = 0, marginLeft = 0, marginRight = 0, i;
|
|
|
|
+ picToCrop.getPixels(pixels, 0, picToCrop.getWidth(), 0, 0,
|
|
|
|
+ picToCrop.getWidth(), picToCrop.getHeight());
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < pixels.length; i++) {
|
|
|
|
+ if (pixels[i] != unusedSpaceColor) {
|
|
|
|
+ marginTop = i / picToCrop.getWidth();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ outerLoop1: for (i = 0; i < picToCrop.getWidth(); i++) {
|
|
|
|
+ for (int j = i; j < pixels.length; j += picToCrop.getWidth()) {
|
|
|
|
+ if (pixels[j] != unusedSpaceColor) {
|
|
|
|
+ marginLeft = j % picToCrop.getWidth();
|
|
|
|
+ break outerLoop1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (i = pixels.length - 1; i >= 0; i--) {
|
|
|
|
+ if (pixels[i] != unusedSpaceColor) {
|
|
|
|
+ marginBottom = (pixels.length - i) / picToCrop.getWidth();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ outerLoop2: for (i = pixels.length - 1; i >= 0; i--) {
|
|
|
|
+ for (int j = i; j >= 0; j -= picToCrop.getWidth()) {
|
|
|
|
+ if (pixels[j] != unusedSpaceColor) {
|
|
|
|
+ marginRight = picToCrop.getWidth()
|
|
|
|
+ - (j % picToCrop.getWidth());
|
|
|
|
+ break outerLoop2;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return Bitmap.createBitmap(picToCrop, marginLeft, marginTop,
|
|
|
|
+ picToCrop.getWidth() - marginLeft - marginRight,
|
|
|
|
+ picToCrop.getHeight() - marginTop - marginBottom);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|