Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#485 - add an option to maintain aspect ratio/stretch image #488

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions app/src/main/java/swati4star/createpdf/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package swati4star.createpdf.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
Expand Down Expand Up @@ -84,13 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
// Check if images are received
handleReceivedImagesIntent(fragment);



mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int count = mSharedPreferences.getInt(LAUNCH_COUNT, 0);
if (count > 0 && count % 15 == 0)
mFeedbackUtils.rateUs();
mSharedPreferences.edit().putInt(LAUNCH_COUNT, count + 1).apply();

getRuntimePermissions();
}

/**
Expand Down Expand Up @@ -324,4 +328,23 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
public void setNavigationViewSelection(int index) {
mNavigationView.getMenu().getItem(index).setChecked(true);
}

private boolean getRuntimePermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA},
0);
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@
import static swati4star.createpdf.util.Constants.DEFAULT_BORDER_WIDTH;
import static swati4star.createpdf.util.Constants.DEFAULT_COMPRESSION;
import static swati4star.createpdf.util.Constants.DEFAULT_IMAGE_BORDER_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_IMAGE_SCALETYPE_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_PAGE_SIZE;
import static swati4star.createpdf.util.Constants.DEFAULT_PAGE_SIZE_TEXT;
import static swati4star.createpdf.util.Constants.DEFAULT_QUALITY_VALUE;
import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_ASPECT_RATIO;
import static swati4star.createpdf.util.Constants.OPEN_SELECT_IMAGES;
import static swati4star.createpdf.util.Constants.RESULT;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
Expand All @@ -81,6 +83,8 @@
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
import static swati4star.createpdf.util.ImageEnhancementOptionsUtils.getEnhancementOptions;
import static swati4star.createpdf.util.ImageUtils.mImageScaleType;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation;
import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction;
import static swati4star.createpdf.util.StringUtils.showSnackbar;
Expand Down Expand Up @@ -218,6 +222,7 @@ void startAddingImages() {
void createPdf() {
mPdfOptions.setImagesUri(mImagesUri);
mPdfOptions.setPageSize(PageSizeUtils.mPageSize);
mPdfOptions.setImageScaleType(mImageScaleType);
MaterialDialog.Builder builder = createCustomDialog(mActivity,
R.string.creating_pdf, R.string.enter_file_name);
builder.input(getString(R.string.example), null, (dialog, input) -> {
Expand Down Expand Up @@ -370,21 +375,24 @@ public void onItemClick(int position) {
mPageSizeUtils.showPageSizeDialog(R.layout.set_page_size_dialog, false);
break;
case 5:
showImageScaleTypeDialog(mActivity, false);
break;
case 6:
startActivityForResult(PreviewActivity.getStartIntent(mActivity, mImagesUri),
INTENT_REQUEST_PREVIEW_IMAGE);
break;
case 6:
case 7:
addBorder();
break;
case 7:
case 8:
startActivityForResult(RearrangeImages.getStartIntent(mActivity, mImagesUri),
INTENT_REQUEST_REARRANGE_IMAGE);
break;
case 8:
case 9:
saveCurrentImage();
createPdf();
break;
case 9:
case 10:
addMargins();
break;
}
Expand Down Expand Up @@ -602,6 +610,9 @@ private void resetValues() {
mImageCounter = 0;
showEnhancementOptions();
mNoOfImages.setVisibility(View.GONE);
mImageScaleType = mSharedPreferences.getString(DEFAULT_IMAGE_SCALETYPE_TEXT,
IMAGE_SCALE_TYPE_ASPECT_RATIO);
mPdfOptions.setMargins(0, 0, 0, 0);
}

/**
Expand Down Expand Up @@ -667,15 +678,6 @@ void addMargins() {
mMarginLeft = Integer.parseInt(left.getText().toString());
mPdfOptions.setMargins(mMarginTop, mMarginBottom, mMarginRight, mMarginLeft);
})).build();
View view = materialDialog.getCustomView();
EditText top = view.findViewById(R.id.topMarginEditText);
EditText bottom = view.findViewById(R.id.bottomMarginEditText);
EditText right = view.findViewById(R.id.rightMarginEditText);
EditText left = view.findViewById(R.id.leftMarginEditText);
top.setText(Integer.toString(mMarginTop));
bottom.setText(Integer.toString(mMarginBottom));
right.setText(Integer.toString(mMarginRight));
left.setText(Integer.toString(mMarginLeft));
materialDialog.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static swati4star.createpdf.util.Constants.DEFAULT_COMPRESSION;
import static swati4star.createpdf.util.Constants.STORAGE_LOCATION;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
import static swati4star.createpdf.util.SettingsOptions.ImageEnhancementOptionsUtils.getEnhancementOptions;
import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation;
import static swati4star.createpdf.util.StringUtils.showSnackbar;
Expand Down Expand Up @@ -95,8 +96,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
showSnackbar(mActivity, R.string.storage_location_modified);
storageLocation.setText(mSharedPreferences.getString(STORAGE_LOCATION,
getDefaultStorageLocation()));
} else {
showSnackbar(mActivity, R.string.error_occurred);
}
break;
}
Expand Down Expand Up @@ -130,6 +129,9 @@ public void onItemClick(int position) {
case 4:
setTheme();
break;
case 5:
showImageScaleTypeDialog(mActivity, true);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ImageToPDFOptions extends PDFOptions {
private int mMarginBottom = 0;
private int mMarginRight = 0;
private int mMarginLeft = 0;
private String mImageScaleType;

public ImageToPDFOptions() {
super();
Expand Down Expand Up @@ -63,4 +64,12 @@ public int getMarginRight() {
public int getMarginLeft() {
return mMarginLeft;
}

public String getImageScaleType() {
return mImageScaleType;
}

public void setImageScaleType(String mImageScaleType) {
this.mImageScaleType = mImageScaleType;
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Constants {
public static final int DEFAULT_QUALITY_VALUE = 30;
public static final int DEFAULT_BORDER_WIDTH = 0;
public static final String STORAGE_LOCATION = "storage_location";
public static final String DEFAULT_IMAGE_SCALETYPE_TEXT = "image_scale_type";
public static final String IMAGE_SCALE_TYPE_STRETCH = "stretch_image";
public static final String IMAGE_SCALE_TYPE_ASPECT_RATIO = "maintain_aspect_ratio";

public static final String BUNDLE_DATA = "bundle_data";
public static final String REORDER_PAGES = "reorder_pages";
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/swati4star/createpdf/util/CreatePdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import swati4star.createpdf.interfaces.OnPDFCreatedInterface;
import swati4star.createpdf.model.ImageToPDFOptions;

import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_ASPECT_RATIO;
import static swati4star.createpdf.util.Constants.appName;
import static swati4star.createpdf.util.Constants.pdfExtension;

Expand All @@ -41,6 +42,7 @@ public class CreatePdf extends AsyncTask<String, String, String> {
private int mMarginBottom;
private int mMarginRight;
private int mMarginLeft;
private String mImagescaleType;

public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
OnPDFCreatedInterface onPDFCreated) {
Expand All @@ -57,6 +59,7 @@ public CreatePdf(ImageToPDFOptions mImageToPDFOptions, String parentPath,
this.mMarginBottom = mImageToPDFOptions.getMarginBottom();
this.mMarginRight = mImageToPDFOptions.getMarginRight();
this.mMarginLeft = mImageToPDFOptions.getMarginLeft();
this.mImagescaleType = mImageToPDFOptions.getImageScaleType();
mPath = parentPath;
}

Expand Down Expand Up @@ -126,21 +129,20 @@ protected String doInBackground(String... params) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(mImagesUri.get(i), bmOptions);

Rectangle imageSize = ImageUtils.calculateFitSize(bitmap.getWidth() + mBorderWidth,
bitmap.getHeight() + mBorderWidth, documentRect);
image.scaleAbsolute(imageSize);

Log.v("Stage 6", "Image path adding");

float pageWidth = document.getPageSize().getWidth() - (mMarginLeft + mMarginRight);
float pageHeight = document.getPageSize().getHeight() - (mMarginBottom + mMarginTop);
image.scaleToFit(pageWidth, pageHeight);
if (mImagescaleType.equals(IMAGE_SCALE_TYPE_ASPECT_RATIO))
image.scaleToFit(pageWidth, pageHeight);
else
image.scaleAbsolute(pageWidth, pageHeight);

image.setAbsolutePosition(
(documentRect.getWidth() - image.getScaledWidth()) / 2,
(documentRect.getHeight() - image.getScaledHeight()) / 2);
Log.v("Stage 7", "Image Alignments");

Log.v("Stage 7", "Image Alignments");
document.add(image);

document.newPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public static ArrayList<EnhancementOptionsEntity> getEnhancementOptions(Context
context.getResources().getDrawable(R.drawable.ic_page_size_24dp),
context.getResources().getString(R.string.set_page_size_text)));

options.add(new EnhancementOptionsEntity(
context.getResources().getDrawable(R.drawable.ic_aspect_ratio_black_24dp),
context.getResources().getString(R.string.image_scale_type)));

options.add(new EnhancementOptionsEntity(
context.getResources().getDrawable(R.drawable.ic_play_circle_outline_black_24dp),
context.getResources().getString(R.string.preview_image_to_pdf)));
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package swati4star.createpdf.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
Expand All @@ -8,13 +11,26 @@
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.itextpdf.text.Rectangle;

import java.io.File;

import swati4star.createpdf.R;

import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_ASPECT_RATIO;
import static swati4star.createpdf.util.Constants.IMAGE_SCALE_TYPE_STRETCH;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;

public class ImageUtils {

public static String mImageScaleType;

/**
* Calculates the optimum size for an image, such that it scales to fit whilst retaining its aspect ratio
*
Expand Down Expand Up @@ -90,4 +106,34 @@ public static Bitmap getRoundBitmapFromPath(String path) {
if (bitmap == null) return null;
return ImageUtils.getRoundBitmap(bitmap);
}

public static void showImageScaleTypeDialog(Context context, Boolean saveValue) {

int layout = R.layout.image_scale_type_dialog;
if (saveValue)
layout = R.layout.image_scale_type_dialog_default;

SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
MaterialDialog.Builder builder = createCustomDialogWithoutContent((Activity) context,
R.string.image_scale_type);
MaterialDialog materialDialog =
builder.customView(layout, true)
.onPositive((dialog1, which) -> {
View view = dialog1.getCustomView();
RadioGroup radioGroup = view.findViewById(R.id.scale_type);
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == R.id.aspect_ratio)
mImageScaleType = IMAGE_SCALE_TYPE_ASPECT_RATIO;
else
mImageScaleType = IMAGE_SCALE_TYPE_STRETCH;

CheckBox mSetAsDefault = view.findViewById(R.id.cbSetDefault);
if (saveValue || mSetAsDefault.isChecked()) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(Constants.DEFAULT_IMAGE_SCALETYPE_TEXT, mImageScaleType);
editor.apply();
}
}).build();
materialDialog.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public static ArrayList<EnhancementOptionsEntity> getEnhancementOptions(Context
sharedPreferences.getString(Constants.DEFAULT_THEME_TEXT,
Constants.DEFAULT_THEME))));

options.add(new EnhancementOptionsEntity(
context.getResources().getDrawable(R.drawable.ic_aspect_ratio_black_24dp),
context.getString(R.string.image_scale_type)));

return options;
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_aspect_ratio_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,12h-2v3h-3v2h5v-5zM7,9h3L10,7L5,7v5h2L7,9zM21,3L3,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2L23,5c0,-1.1 -0.9,-2 -2,-2zM21,19.01L3,19.01L3,4.99h18v14.02z"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_menu_gallery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FFFFFFFF"
android:fillColor="#000000"
android:pathData="M22,16V4c0,-1.1 -0.9,-2 -2,-2H8c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2zm-11,-4l2.03,2.71L16,11l4,5H8l3,-4zM2,6v14c0,1.1 0.9,2 2,2h14v-2H4V6H2z" />
</vector>
Loading