Skip to content

Commit

Permalink
Added Options for custom parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay2211 committed Jan 5, 2019
1 parent 9f317d6 commit cf199fd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/fxn/pixsample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

}

Expand Down
71 changes: 57 additions & 14 deletions pix/src/main/java/com/fxn/pix/Options.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Expand All @@ -28,31 +71,31 @@ 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;
}

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;
}

}
37 changes: 19 additions & 18 deletions pix/src/main/java/com/fxn/pix/Pix.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ public void onClick(Img img, View view, int position) {
anim.setDuration(300);
anim.setAnimationListener(new Animation.AnimationListener() {

/**
* <p>Notifies the start of the animation.</p>
*
* @param animation The started animation.
*/
@Override
public void onAnimationStart(Animation animation) {

Expand All @@ -182,11 +177,6 @@ public void onAnimationEnd(Animation animation) {
sendButton.clearAnimation();
}

/**
* <p>Notifies the repetition of the animation.</p>
*
* @param animation The animation which was repeated.
*/
@Override
public void onAnimationRepeat(Animation animation) {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();


Expand Down Expand Up @@ -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(), ""));
Expand Down Expand Up @@ -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() {
Expand All @@ -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);

}
}
});
Expand All @@ -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<Img> INSTANTLIST = new ArrayList<>();
String header = "";
int limit = 100;
Expand Down
5 changes: 5 additions & 0 deletions pix/src/main/java/com/fxn/utility/ImageQuality.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.fxn.utility;

public enum ImageQuality {
LOW, REGULAR, HIGH
}
26 changes: 22 additions & 4 deletions pix/src/main/java/com/fxn/utility/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public class Utility {

public static int HEIGHT, WIDTH;

private String pathDir;

public static void setupStatusBarHidden(AppCompatActivity appCompatActivity) {
Expand Down Expand Up @@ -196,19 +197,20 @@ 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();
File photo = new File(dir, "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmSS", Locale.ENGLISH).format(new Date()) + ".jpg");
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) {
Expand All @@ -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 {

Expand Down

0 comments on commit cf199fd

Please sign in to comment.