From cf199fdb0b2ce51a5115b2b46d800fdf136e663d Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Sun, 6 Jan 2019 01:16:26 +0530 Subject: [PATCH] Added Options for custom parameters --- .../java/com/fxn/pixsample/MainActivity.java | 2 +- pix/src/main/java/com/fxn/pix/Options.java | 71 +++++++++++++++---- pix/src/main/java/com/fxn/pix/Pix.java | 37 +++++----- .../java/com/fxn/utility/ImageQuality.java | 5 ++ .../main/java/com/fxn/utility/Utility.java | 26 +++++-- 5 files changed, 104 insertions(+), 37 deletions(-) create mode 100644 pix/src/main/java/com/fxn/utility/ImageQuality.java diff --git a/app/src/main/java/com/fxn/pixsample/MainActivity.java b/app/src/main/java/com/fxn/pixsample/MainActivity.java index 5655cfe..73032f4 100644 --- a/app/src/main/java/com/fxn/pixsample/MainActivity.java +++ b/app/src/main/java/com/fxn/pixsample/MainActivity.java @@ -31,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) { myAdapter = new MyAdapter(this); recyclerView.setAdapter(myAdapter); findViewById(R.id.fab).setOnClickListener((View view) -> - Pix.start(MainActivity.this, Options.init().setRequestCode(100).setCount(6))); + Pix.start(MainActivity.this, Options.init().setRequestCode(100).setCount(2).setFrontfacing(true))); } diff --git a/pix/src/main/java/com/fxn/pix/Options.java b/pix/src/main/java/com/fxn/pix/Options.java index 3fc6966..044788e 100644 --- a/pix/src/main/java/com/fxn/pix/Options.java +++ b/pix/src/main/java/com/fxn/pix/Options.java @@ -1,23 +1,66 @@ package com.fxn.pix; +import com.fxn.utility.ImageQuality; + import java.io.Serializable; public class Options implements Serializable { - private static Options options; private int count = 1; private int requestCode = 0; private String path = "/DCIM/Camera"; + private int imageQuality = 40; + private int height = 0, width = 0; + private boolean frontfacing = false; private Options() { } public static Options init() { - Options.options = new Options(); - return Options.options; + return new Options(); + } + + public int getImageQuality() { + return imageQuality; + } + + public Options setImageQuality(final ImageQuality imageQuality) { + if (imageQuality == ImageQuality.LOW) { + this.imageQuality = 20; + } else if (imageQuality == ImageQuality.HIGH) { + this.imageQuality = 80; + } else { + this.imageQuality = 40; + } + return this; + } + + public void setImageResolution(int height, int width) { + if (height == 0 || width == 0) { + throw new NullPointerException("width or height can not be 0"); + } + this.height = height; + this.width = width; + } + + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + public boolean isFrontfacing() { + return this.frontfacing; + } + + public Options setFrontfacing(boolean frontfacing) { + this.frontfacing = frontfacing; + return this; } - private static void check() { - if (Options.options == null) { + private void check() { + if (this == null) { throw new NullPointerException("call init() method to initialise Options class"); } } @@ -28,12 +71,12 @@ public int getCount() { public Options setCount(int count) { check(); - Options.options.count = count; - return Options.options; + this.count = count; + return this; } public int getRequestCode() { - if (requestCode == 0) { + if (this.requestCode == 0) { throw new NullPointerException("requestCode in Options class is null"); } return requestCode; @@ -41,18 +84,18 @@ public int getRequestCode() { public Options setRequestCode(int requestcode) { check(); - Options.options.requestCode = requestcode; - return Options.options; + this.requestCode = requestcode; + return this; } public String getPath() { - return path; + return this.path; } - public static Options setPath(String path) { + public Options setPath(String path) { check(); - Options.options.path = path; - return Options.options; + this.path = path; + return this; } } diff --git a/pix/src/main/java/com/fxn/pix/Pix.java b/pix/src/main/java/com/fxn/pix/Pix.java index 6c8f4da..6cdc811 100644 --- a/pix/src/main/java/com/fxn/pix/Pix.java +++ b/pix/src/main/java/com/fxn/pix/Pix.java @@ -166,11 +166,6 @@ public void onClick(Img img, View view, int position) { anim.setDuration(300); anim.setAnimationListener(new Animation.AnimationListener() { - /** - *

Notifies the start of the animation.

- * - * @param animation The started animation. - */ @Override public void onAnimationStart(Animation animation) { @@ -182,11 +177,6 @@ public void onAnimationEnd(Animation animation) { sendButton.clearAnimation(); } - /** - *

Notifies the repetition of the animation.

- * - * @param animation The animation which was repeated. - */ @Override public void onAnimationRepeat(Animation animation) { @@ -244,7 +234,6 @@ public void onLongClick(Img img, View view, int position) { }; private FrameLayout flash; private ImageView front; - private boolean isback = true; private int flashDrawable; private View.OnTouchListener onCameraTouchListner = new View.OnTouchListener() { @Override @@ -455,6 +444,12 @@ public int getSpanSize(int position) { recyclerView.setAdapter(mainImageAdapter); recyclerView.addItemDecoration(new HeaderItemDecoration(this, mainImageAdapter)); mHandleView.setOnTouchListener(this); + final CameraConfiguration cameraConfiguration = new CameraConfiguration(); + if (options.isFrontfacing()) { + fotoapparat.switchTo(LensPositionSelectorsKt.front(), cameraConfiguration); + } else { + fotoapparat.switchTo(LensPositionSelectorsKt.back(), cameraConfiguration); + } onClickMethods(); @@ -483,7 +478,7 @@ public Unit invoke(Bitmap bitmap) { if (bitmap != null) { Log.e("my pick", bitmap.toString()); synchronized (bitmap) { - File photo = Utility.writeImage(bitmap, options.getPath()); + File photo = Utility.writeImage(bitmap, options.getPath(), options.getImageQuality(), options.getWidth(), options.getHeight()); Log.e("my pick saved", bitmap.toString() + " -> " + photo.length() / 1024); selectionList.clear(); selectionList.add(new Img("", "", photo.getAbsolutePath(), "")); @@ -562,7 +557,6 @@ public void onAnimationEnd(Animator animation) { front.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - final CameraConfiguration cameraConfiguration = new CameraConfiguration(); final ObjectAnimator oa1 = ObjectAnimator.ofFloat(front, "scaleX", 1f, 0f).setDuration(150); final ObjectAnimator oa2 = ObjectAnimator.ofFloat(front, "scaleX", 0f, 1f).setDuration(150); oa1.addListener(new AnimatorListenerAdapter() { @@ -574,12 +568,16 @@ public void onAnimationEnd(Animator animation) { } }); oa1.start(); - if (isback) { - isback = false; - fotoapparat.switchTo(LensPositionSelectorsKt.front(), cameraConfiguration); - } else { - isback = true; + Log.e("isFrontfacing", "-> " + options.isFrontfacing()); + if (options.isFrontfacing()) { + options.setFrontfacing(false); + final CameraConfiguration cameraConfiguration = new CameraConfiguration(); fotoapparat.switchTo(LensPositionSelectorsKt.back(), cameraConfiguration); + } else { + final CameraConfiguration cameraConfiguration = new CameraConfiguration(); + options.setFrontfacing(true); + fotoapparat.switchTo(LensPositionSelectorsKt.front(), cameraConfiguration); + } } }); @@ -588,6 +586,9 @@ public void onAnimationEnd(Animator animation) { private void updateImages() { mainImageAdapter.clearList(); Cursor cursor = Utility.getCursor(Pix.this); + if (cursor == null) { + return; + } ArrayList INSTANTLIST = new ArrayList<>(); String header = ""; int limit = 100; diff --git a/pix/src/main/java/com/fxn/utility/ImageQuality.java b/pix/src/main/java/com/fxn/utility/ImageQuality.java new file mode 100644 index 0000000..c99d7da --- /dev/null +++ b/pix/src/main/java/com/fxn/utility/ImageQuality.java @@ -0,0 +1,5 @@ +package com.fxn.utility; + +public enum ImageQuality { + LOW, REGULAR, HIGH +} diff --git a/pix/src/main/java/com/fxn/utility/Utility.java b/pix/src/main/java/com/fxn/utility/Utility.java index 3f32ebe..95e3ced 100644 --- a/pix/src/main/java/com/fxn/utility/Utility.java +++ b/pix/src/main/java/com/fxn/utility/Utility.java @@ -37,6 +37,7 @@ public class Utility { public static int HEIGHT, WIDTH; + private String pathDir; public static void setupStatusBarHidden(AppCompatActivity appCompatActivity) { @@ -196,7 +197,7 @@ public static void vibe(Context c, long l) { ((Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(l); } - public static File writeImage(Bitmap bitmap, String path) { + public static File writeImage(Bitmap bitmap, String path, int quality, int newWidth, int newHeight) { File dir = new File(Environment.getExternalStorageDirectory(), path); if (!dir.exists()) dir.mkdir(); @@ -204,11 +205,12 @@ public static File writeImage(Bitmap bitmap, String path) { if (photo.exists()) { photo.delete(); } - + if (newWidth != 0 && newHeight != 0) { + bitmap = getResizedBitmap(bitmap, newWidth, newHeight); + } try { FileOutputStream fos = new FileOutputStream(photo.getPath()); - - bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fos); + bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos); // fos.write(jpeg); fos.close(); } catch (Exception e) { @@ -217,6 +219,22 @@ public static File writeImage(Bitmap bitmap, String path) { return photo; } + public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { + int width = bm.getWidth(); + int height = bm.getHeight(); + float scaleWidth = ((float) newWidth) / width; + float scaleHeight = ((float) newHeight) / height; + // CREATE A MATRIX FOR THE MANIPULATION + Matrix matrix = new Matrix(); + // RESIZE THE BIT MAP + matrix.postScale(scaleWidth, scaleHeight); + + // "RECREATE" THE NEW BITMAP + Bitmap resizedBitmap = Bitmap.createBitmap( + bm, 0, 0, width, height, matrix, false); + return resizedBitmap; + } + public static Bitmap getScaledBitmap(int maxWidth, Bitmap rotatedBitmap) { try {