diff --git a/.gitignore b/.gitignore
index 148ffae5..f02eb964 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,5 @@
*.iml
ScanDemoExample/local.properties
ScanDemoExample/build/intermediates/dex-cache/cache.xml
+
+ScanDemoExample/build/intermediates/dex-cache/cache.xml
diff --git a/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java b/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java
index a45db24c..98e627af 100644
--- a/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java
+++ b/ScanDemoExample/app/src/main/java/com/scanner/demo/MainActivity.java
@@ -4,109 +4,136 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
-import android.net.Uri;
import android.os.Bundle;
-import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
-import android.view.Menu;
-import android.view.MenuItem;
import android.view.View;
-import android.widget.Button;
+import android.view.View.OnClickListener;
import android.widget.ImageView;
import com.scanlibrary.ScanActivity;
-import com.scanlibrary.ScanConstants;
-import java.io.IOException;
+public class MainActivity extends ActionBarActivity implements OnClickListener {
-public class MainActivity extends ActionBarActivity {
+ // ===========================================================
+ // Constants
+ // ===========================================================
- private static final int REQUEST_CODE = 99;
- private Button scanButton;
- private Button cameraButton;
- private Button mediaButton;
- private ImageView scannedImageView;
+ private static final int REQUEST_CODE_SCAN = 47;
+
+ private static final String SAVED_SCANNED_HHOTO = "scanned_photo";
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private final ViewHolder viewHolder = new ViewHolder();
+
+ private String scannedPhoto;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ // ===========================================================
+ // Getters & Setters
+ // ===========================================================
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- init();
- }
+ viewHolder.prepare(findViewById(android.R.id.content));
- private void init() {
- scanButton = (Button) findViewById(R.id.scanButton);
- scanButton.setOnClickListener(new ScanButtonClickListener());
- cameraButton = (Button) findViewById(R.id.cameraButton);
- cameraButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_CAMERA));
- mediaButton = (Button) findViewById(R.id.mediaButton);
- mediaButton.setOnClickListener(new ScanButtonClickListener(ScanConstants.OPEN_MEDIA));
- scannedImageView = (ImageView) findViewById(R.id.scannedImage);
- }
-
- private class ScanButtonClickListener implements View.OnClickListener {
-
- private int preference;
-
- public ScanButtonClickListener(int preference) {
- this.preference = preference;
+ if (savedInstanceState != null) {
+ scannedPhoto = savedInstanceState.getString(SAVED_SCANNED_HHOTO);
}
- public ScanButtonClickListener() {
+ if (scannedPhoto != null) {
+ viewHolder.image.setImageBitmap(getBitmapFromLocation(scannedPhoto));
}
+ }
- @Override
- public void onClick(View v) {
- startScan(preference);
+ @Override
+ public void onClick(View v) {
+ if (v.equals(viewHolder.scabBtn)) {
+ onScanButtonClicked();
}
}
- protected void startScan(int preference) {
- Intent intent = new Intent(this, ScanActivity.class);
- intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
- startActivityForResult(intent, REQUEST_CODE);
+ @Override
+ protected void onResume() {
+ super.onResume();
+ viewHolder.scabBtn.setOnClickListener(this);
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ viewHolder.scabBtn.setOnClickListener(null);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
- Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
- Bitmap bitmap = null;
- try {
- bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
- getContentResolver().delete(uri, null, null);
- scannedImageView.setImageBitmap(bitmap);
- } catch (IOException e) {
- e.printStackTrace();
- }
+ if (requestCode == REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK) {
+ String imgPath = data.getStringExtra(ScanActivity.RESULT_IMAGE_PATH);
+ Bitmap bitmap = getBitmapFromLocation(imgPath);
+ viewHolder.image.setImageBitmap(bitmap);
+// Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
+// Bitmap bitmap = null;
+// try {
+// bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
+// getContentResolver().delete(uri, null, null);
+// viewHolder.image.setImageBitmap(bitmap);
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
}
}
- private Bitmap convertByteArrayToBitmap(byte[] data) {
- return BitmapFactory.decodeByteArray(data, 0, data.length);
+ @Override
+ protected void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putString(SAVED_SCANNED_HHOTO, scannedPhoto);
}
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ private void onScanButtonClicked() {
+ Intent intent = new Intent(this, ScanActivity.class);
+ intent.putExtra(ScanActivity.EXTRA_BRAND_IMG_RES, R.drawable.ic_crop_white_24dp);
+ intent.putExtra(ScanActivity.EXTRA_TITLE, "Crop Document");
+ intent.putExtra(ScanActivity.EXTRA_ACTION_BAR_COLOR, R.color.green);
+ intent.putExtra(ScanActivity.EXTRA_LANGUAGE, "ru");
+ startActivityForResult(intent, REQUEST_CODE_SCAN);
}
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
-
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
+ private Bitmap getBitmapFromLocation(String absLocation) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+ return BitmapFactory.decodeFile(absLocation, options);
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+
+ private static class ViewHolder {
- return super.onOptionsItemSelected(item);
+ ImageView image;
+ View scabBtn;
+
+ void prepare(View parent) {
+ image = (ImageView) parent.findViewById(R.id.image);
+ scabBtn = parent.findViewById(R.id.scan);
+ }
}
+
}
diff --git a/ScanDemoExample/app/src/main/res/layout/activity_main.xml b/ScanDemoExample/app/src/main/res/layout/activity_main.xml
index 3710726c..7806993b 100644
--- a/ScanDemoExample/app/src/main/res/layout/activity_main.xml
+++ b/ScanDemoExample/app/src/main/res/layout/activity_main.xml
@@ -1,4 +1,5 @@
-
+ android:layout_above="@+id/linearLayout"
+ android:layout_centerHorizontal="true"/>
+ android:layout_height="wrap_content"
+ android:layout_alignParentBottom="true">
-
-
-
-
-
+ android:text="Scan"/>
-
diff --git a/ScanDemoExample/app/src/main/res/values/colors.xml b/ScanDemoExample/app/src/main/res/values/colors.xml
new file mode 100644
index 00000000..e9b66995
--- /dev/null
+++ b/ScanDemoExample/app/src/main/res/values/colors.xml
@@ -0,0 +1,4 @@
+
+
+ #006600
+
diff --git a/ScanDemoExample/build/intermediates/dex-cache/cache.xml b/ScanDemoExample/build/intermediates/dex-cache/cache.xml
index 961b202d..29211b53 100644
--- a/ScanDemoExample/build/intermediates/dex-cache/cache.xml
+++ b/ScanDemoExample/build/intermediates/dex-cache/cache.xml
@@ -2,11 +2,11 @@
-
-
+ sha1="4b74cefe1f0c1b819e7260c8627a14674e37fd35">
+
-
-
+ sha1="2c91c949a45a21cdecf26e03951e46c7beec9ad8">
+
-
-
+ sha1="74310a3d25228a74911f4d4376df2dfd8449a3ca">
+
-
-
+
+
+ android:required="true"/>
+
+
+
+
+
+
-
-
-
-
+ android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/IScanner.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/IScanner.java
deleted file mode 100644
index 51c6add7..00000000
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/IScanner.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.scanlibrary;
-
-import android.net.Uri;
-
-/**
- * Created by jhansi on 04/04/15.
- */
-public interface IScanner {
-
- void onBitmapSelect(Uri uri);
-
- void onScanFinish(Uri uri);
-}
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java
deleted file mode 100644
index 0e84e631..00000000
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PickImageFragment.java
+++ /dev/null
@@ -1,162 +0,0 @@
-package com.scanlibrary;
-
-import android.app.Activity;
-import android.app.Fragment;
-import android.content.Intent;
-import android.content.res.AssetFileDescriptor;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.net.Uri;
-import android.os.Bundle;
-import android.provider.MediaStore;
-import android.util.Log;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.ImageButton;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-/**
- * Created by jhansi on 04/04/15.
- */
-public class PickImageFragment extends Fragment {
-
- private View view;
- private ImageButton cameraButton;
- private ImageButton galleryButton;
- private Uri fileUri;
- private IScanner scanner;
-
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- if (!(activity instanceof IScanner)) {
- throw new ClassCastException("Activity must implement IScanner");
- }
- this.scanner = (IScanner) activity;
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.pick_image_fragment, null);
- init();
- return view;
- }
-
- private void init() {
- cameraButton = (ImageButton) view.findViewById(R.id.cameraButton);
- cameraButton.setOnClickListener(new CameraButtonClickListener());
- galleryButton = (ImageButton) view.findViewById(R.id.selectButton);
- galleryButton.setOnClickListener(new GalleryClickListener());
- if (isIntentPreferenceSet()) {
- handleIntentPreference();
- }
- }
-
- private void handleIntentPreference() {
- int preference = getIntentPreference();
- if (preference == ScanConstants.OPEN_CAMERA) {
- openCamera();
- } else if (preference == ScanConstants.OPEN_MEDIA) {
- openMediaContent();
- }
- }
-
- private boolean isIntentPreferenceSet() {
- int preference = getArguments().getInt(ScanConstants.OPEN_INTENT_PREFERENCE, 0);
- return preference != 0;
- }
-
- private int getIntentPreference() {
- int preference = getArguments().getInt(ScanConstants.OPEN_INTENT_PREFERENCE, 0);
- return preference;
- }
-
-
- private class CameraButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- openCamera();
- }
- }
-
- private class GalleryClickListener implements View.OnClickListener {
- @Override
- public void onClick(View view) {
- openMediaContent();
- }
- }
-
- public void openMediaContent() {
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.addCategory(Intent.CATEGORY_OPENABLE);
- intent.setType("*/*");
- startActivityForResult(intent, ScanConstants.PICKFILE_REQUEST_CODE);
- }
-
- public void openCamera() {
- Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- File file = createImageFile();
- file.getParentFile().mkdirs();
- fileUri = Uri.fromFile(file);
- if (file != null) {
- cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
- startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
- }
- }
-
- private File createImageFile() {
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
- Date());
- File file = new File(ScanConstants.IMAGE_PATH, "IMG_" + timeStamp +
- ".jpg");
- return file;
- }
-
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d("", "onActivityResult" + resultCode);
- Bitmap bitmap = null;
- if (resultCode == Activity.RESULT_OK) {
- try {
- switch (requestCode) {
- case ScanConstants.START_CAMERA_REQUEST_CODE:
- bitmap = getBitmap(fileUri);
- break;
-
- case ScanConstants.PICKFILE_REQUEST_CODE:
- bitmap = getBitmap(data.getData());
- break;
- }
- } catch (Exception e
- ) {
- e.printStackTrace();
- }
- }
- if (bitmap != null) {
- postImagePick(bitmap);
- }
- }
-
- protected void postImagePick(Bitmap bitmap) {
- Uri uri = Utils.getUri(getActivity(), bitmap);
- bitmap.recycle();
- scanner.onBitmapSelect(uri);
- }
-
- private Bitmap getBitmap(Uri selectedimg) throws IOException {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 3;
- AssetFileDescriptor fileDescriptor = null;
- fileDescriptor =
- getActivity().getContentResolver().openAssetFileDescriptor(selectedimg, "r");
- Bitmap original
- = BitmapFactory.decodeFileDescriptor(
- fileDescriptor.getFileDescriptor(), null, options);
- return original;
- }
-}
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PolygonView.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PolygonView.java
index 362e4c76..d1726f4f 100644
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PolygonView.java
+++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/PolygonView.java
@@ -103,7 +103,7 @@ public Map getPoints() {
return getOrderedPoints(points);
}
- public Map getOrderedPoints(List points) {
+ public static Map getOrderedPoints(List points) {
PointF centerPoint = new PointF();
int size = points.size();
@@ -248,7 +248,7 @@ public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
- public boolean isValidShape(Map pointFMap) {
+ public static boolean isValidShape(Map pointFMap) {
return pointFMap.size() == 4;
}
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ProgressDialogFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ProgressDialogFragment.java
index 710d1d71..d8005399 100644
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ProgressDialogFragment.java
+++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ProgressDialogFragment.java
@@ -1,45 +1,34 @@
package com.scanlibrary;
-import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
-import android.view.KeyEvent;
-@SuppressLint("ValidFragment")
public class ProgressDialogFragment extends DialogFragment {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+ public static final String EXTRA_MESSAGE = "message";
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
- public String message;
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ ProgressDialog dialog = new ProgressDialog(getActivity());
+ if (getArguments() != null && getArguments().containsKey(EXTRA_MESSAGE)) {
+ dialog.setMessage(getArguments().getString(EXTRA_MESSAGE));
+ }
- public ProgressDialogFragment(String message) {
- this.message = message;
- }
+ return dialog;
+ }
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- final ProgressDialog dialog = new ProgressDialog(getActivity());
- dialog.setIndeterminate(true);
- dialog.setMessage(message);
- dialog.setCancelable(false);
- dialog.setCanceledOnTouchOutside(false);
- // Disable the back button
- OnKeyListener keyListener = new OnKeyListener() {
+ // ===========================================================
+ // Methods
+ // ===========================================================
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
-
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- return true;
- }
- return false;
- }
-
- };
- dialog.setOnKeyListener(keyListener);
- return dialog;
- }
-}
\ No newline at end of file
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ResultFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ResultFragment.java
deleted file mode 100644
index 29a77129..00000000
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ResultFragment.java
+++ /dev/null
@@ -1,128 +0,0 @@
-package com.scanlibrary;
-
-import android.app.Activity;
-import android.app.Fragment;
-import android.content.Intent;
-import android.graphics.Bitmap;
-import android.net.Uri;
-import android.os.Bundle;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.Button;
-import android.widget.ImageView;
-
-import java.io.IOException;
-
-/**
- * Created by jhansi on 29/03/15.
- */
-public class ResultFragment extends Fragment {
-
- private View view;
- private ImageView scannedImageView;
- private Button doneButton;
- private Bitmap original;
- private Button originalButton;
- private Button MagicColorButton;
- private Button grayModeButton;
- private Button bwButton;
- private Bitmap transformed;
-
- public ResultFragment() {
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.result_layout, null);
- init();
- return view;
- }
-
- private void init() {
- scannedImageView = (ImageView) view.findViewById(R.id.scannedImage);
- originalButton = (Button) view.findViewById(R.id.original);
- originalButton.setOnClickListener(new OriginalButtonClickListener());
- MagicColorButton = (Button) view.findViewById(R.id.magicColor);
- MagicColorButton.setOnClickListener(new MagicColorButtonClickListener());
- grayModeButton = (Button) view.findViewById(R.id.grayMode);
- grayModeButton.setOnClickListener(new GrayButtonClickListener());
- bwButton = (Button) view.findViewById(R.id.BWMode);
- bwButton.setOnClickListener(new BWButtonClickListener());
- Bitmap bitmap = getBitmap();
- setScannedImage(bitmap);
- doneButton = (Button) view.findViewById(R.id.doneButton);
- doneButton.setOnClickListener(new DoneButtonClickListener());
- }
-
- private Bitmap getBitmap() {
- Uri uri = getUri();
- try {
- original = Utils.getBitmap(getActivity(), uri);
- getActivity().getContentResolver().delete(uri, null, null);
- return original;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- private Uri getUri() {
- Uri uri = getArguments().getParcelable(ScanConstants.SCANNED_RESULT);
- return uri;
- }
-
- public void setScannedImage(Bitmap scannedImage) {
- scannedImageView.setImageBitmap(scannedImage);
- }
-
- private class DoneButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- Intent data = new Intent();
- Bitmap bitmap = transformed;
- if (bitmap == null) {
- bitmap = original;
- }
- Uri uri = Utils.getUri(getActivity(), bitmap);
- data.putExtra(ScanConstants.SCANNED_RESULT, uri);
- getActivity().setResult(Activity.RESULT_OK, data);
- original.recycle();
- System.gc();
- getActivity().finish();
- }
- }
-
- private class BWButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- transformed = ((ScanActivity) getActivity()).getBWBitmap(original);
- scannedImageView.setImageBitmap(transformed);
- }
- }
-
- private class MagicColorButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- transformed = ((ScanActivity) getActivity()).getMagicColorBitmap(original);
- scannedImageView.setImageBitmap(transformed);
- }
- }
-
- private class OriginalButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- transformed = original;
- scannedImageView.setImageBitmap(original);
- }
- }
-
- private class GrayButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- transformed = ((ScanActivity) getActivity()).getGrayBitmap(original);
- scannedImageView.setImageBitmap(transformed);
- }
- }
-
-}
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanActivity.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanActivity.java
index 8cc679e5..02056a49 100644
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanActivity.java
+++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanActivity.java
@@ -1,73 +1,87 @@
package com.scanlibrary;
import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentManager;
import android.app.FragmentTransaction;
+import android.content.res.Configuration;
import android.graphics.Bitmap;
-import android.net.Uri;
+import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
-/**
- * Created by jhansi on 28/03/15.
- */
-public class ScanActivity extends Activity implements IScanner {
+import java.util.Locale;
+
+public class ScanActivity extends Activity {
+
+
+ public static final String EXTRA_BRAND_IMG_RES = "title_img_res";
+ public static final String EXTRA_TITLE = "title";
+ public static final String EXTRA_LANGUAGE = "language";
+ public static final String EXTRA_ACTION_BAR_COLOR = "ab_color";
+ public static final String RESULT_IMAGE_PATH = ScanFragment.RESULT_IMAGE_PATH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.scan_layout);
- init();
- }
+ setContentView(R.layout.activity_scan);
- private void init() {
- PickImageFragment fragment = new PickImageFragment();
- Bundle bundle = new Bundle();
- bundle.putInt(ScanConstants.OPEN_INTENT_PREFERENCE, getPreferenceContent());
- fragment.setArguments(bundle);
- android.app.FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.add(R.id.content, fragment);
- fragmentTransaction.commit();
- }
+ int titleImgRes = getIntent().getExtras().getInt(EXTRA_BRAND_IMG_RES);
+ int abColor = getIntent().getExtras().getInt(EXTRA_ACTION_BAR_COLOR);
+ String title = getIntent().getExtras().getString(EXTRA_TITLE);
+ String locale = getIntent().getExtras().getString(EXTRA_LANGUAGE);
- protected int getPreferenceContent() {
- return getIntent().getIntExtra(ScanConstants.OPEN_INTENT_PREFERENCE, 0);
- }
+ if (locale != null) {
+ Locale l = new Locale(locale);
+ Locale.setDefault(l);
+ Configuration config = new Configuration();
+ config.locale = l;
+ getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
+ }
- @Override
- public void onBitmapSelect(Uri uri) {
- ScanFragment fragment = new ScanFragment();
- Bundle bundle = new Bundle();
- bundle.putParcelable(ScanConstants.SELECTED_BITMAP, uri);
- fragment.setArguments(bundle);
- android.app.FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.add(R.id.content, fragment);
- fragmentTransaction.addToBackStack(ScanFragment.class.toString());
- fragmentTransaction.commit();
+ if (title != null) setTitle(title);
+ if (titleImgRes != 0) getActionBar().setLogo(titleImgRes);
+
+ if (abColor != 0) {
+ getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(abColor)));
+ getActionBar().setDisplayShowTitleEnabled(false);
+ getActionBar().setDisplayShowTitleEnabled(true);
+ }
+
+ if (savedInstanceState == null) {
+ Bundle args = new Bundle();
+ if (getIntent().getExtras() != null) {
+ args.putAll(getIntent().getExtras());
+ }
+
+ FragmentManager fragMan = getFragmentManager();
+ Fragment f = new ScanFragment();
+ f.setArguments(args);
+ FragmentTransaction fragTransaction = fragMan.beginTransaction();
+ fragTransaction.replace(R.id.contaner, f, "scan_frag").commit();
+ }
}
@Override
- public void onScanFinish(Uri uri) {
- ResultFragment fragment = new ResultFragment();
- Bundle bundle = new Bundle();
- bundle.putParcelable(ScanConstants.SCANNED_RESULT, uri);
- fragment.setArguments(bundle);
- android.app.FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- fragmentTransaction.add(R.id.content, fragment);
- fragmentTransaction.addToBackStack(ResultFragment.class.toString());
- fragmentTransaction.commit();
+ public void onBackPressed() {
+ ScanFragment scanFragment = (ScanFragment) getFragmentManager().findFragmentByTag("scan_frag");
+ if (scanFragment != null) {
+ scanFragment.onBackPressed();
+ }
+
+ super.onBackPressed();
}
- public native Bitmap getScannedBitmap(Bitmap bitmap, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
- public native Bitmap getGrayBitmap(Bitmap bitmap);
+ public static native Bitmap getScannedBitmap(Bitmap bitmap, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
+
+ public static native Bitmap getGrayBitmap(Bitmap bitmap);
+
+ public static native Bitmap getMagicColorBitmap(Bitmap bitmap);
- public native Bitmap getMagicColorBitmap(Bitmap bitmap);
+ public static native Bitmap getBWBitmap(Bitmap bitmap);
- public native Bitmap getBWBitmap(Bitmap bitmap);
+ public static native float[] getPoints(Bitmap bitmap);
- public native float[] getPoints(Bitmap bitmap);
static {
System.loadLibrary("opencv_java");
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanConstants.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanConstants.java
deleted file mode 100644
index 7d266240..00000000
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanConstants.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.scanlibrary;
-
-import android.os.Environment;
-
-/**
- * Created by jhansi on 15/03/15.
- */
-public class ScanConstants {
-
- public final static int PICKFILE_REQUEST_CODE = 1;
- public final static int START_CAMERA_REQUEST_CODE = 2;
- public final static String OPEN_INTENT_PREFERENCE = "selectContent";
- public final static String IMAGE_BASE_PATH_EXTRA = "ImageBasePath";
- public final static int OPEN_CAMERA = 4;
- public final static int OPEN_MEDIA = 5;
- public final static String SCANNED_RESULT = "scannedResult";
- public final static String IMAGE_PATH = Environment
- .getExternalStorageDirectory().getPath() + "/scanSample";
-
- public final static String SELECTED_BITMAP = "selectedBitmap";
-}
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanFragment.java
index 109bfba4..e34f8b99 100644
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanFragment.java
+++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/ScanFragment.java
@@ -2,8 +2,10 @@
import android.app.Activity;
import android.app.Fragment;
-import android.app.FragmentManager;
+import android.content.Intent;
import android.graphics.Bitmap;
+import android.graphics.Bitmap.CompressFormat;
+import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
@@ -11,110 +13,358 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
+import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
+import android.widget.Toast;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-/**
- * Created by jhansi on 29/03/15.
- */
public class ScanFragment extends Fragment {
- private Button scanButton;
- private ImageView sourceImageView;
- private FrameLayout sourceFrame;
- private PolygonView polygonView;
- private View view;
+
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ public static final String RESULT_IMAGE_PATH = "imgPath";
+
+ private static final int TAKE_PHOTO_REQUEST_CODE = 815;
+ private static final String SAVED_ARG_TAKEN_PHOTO_LOCATION = "taken_photo_loc";
+
+ // ===========================================================
+ // Fields
+ // ===========================================================
+
+ private ViewHolder viewHolder = new ViewHolder();
private ProgressDialogFragment progressDialogFragment;
- private IScanner scanner;
- private Bitmap original;
+
+ private String takenPhotoLocation;
+ private Bitmap takenPhotoBitmap;
+ private Bitmap documentBitmap;
+
+ private Map points;
+
+ private boolean isCropMode = false;
+ private boolean isPointsDownScaled = false;
+
+ private int previousOreantation = -1;
+ private boolean isOreantationChanged = false;
+
+ // ===========================================================
+ // Constructors
+ // ===========================================================
+
+ // ===========================================================
+ // Getters & Setters
+ // ===========================================================
+
+ // ===========================================================
+ // Methods for/from SuperClass/Interfaces
+ // ===========================================================
+
@Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- if (!(activity instanceof IScanner)) {
- throw new ClassCastException("Activity must implement IScanner");
- }
- this.scanner = (IScanner) activity;
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setRetainInstance(true);
+ setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.scan_fragment_layout, null);
- init();
- return view;
+ return inflater.inflate(R.layout.fragment_scan, container, false);
}
- public ScanFragment() {
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ viewHolder.prepare(view);
+ super.onViewCreated(view, savedInstanceState);
+
+ int currentOreantation = Utils.getScreenOrientation(getActivity());
+ if (previousOreantation == -1) {
+ previousOreantation = currentOreantation;
+ isOreantationChanged = false;
+ } else if (previousOreantation == currentOreantation) {
+ isOreantationChanged = false;
+ } else {
+ previousOreantation = currentOreantation;
+ isOreantationChanged = true;
+ }
+
+ if (takenPhotoLocation == null) {
+ takePhoto();
+ } else {
+ if (documentBitmap != null) {
+ viewHolder.sourceImageView.setImageBitmap(documentBitmap);
+ }
+ }
+ if (isCropMode) {
+ viewHolder.sourceFrame.post(new Runnable() {
+ @Override
+ public void run() {
+ Bitmap scaledBitmap = scaleBitmap(takenPhotoBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight());
+ viewHolder.sourceImageView.setImageBitmap(scaledBitmap);
+
+ Bitmap tempBitmap = ((BitmapDrawable) viewHolder.sourceImageView.getDrawable()).getBitmap();
+ viewHolder.polygonView.setVisibility(View.VISIBLE);
+ int padding = (int) getResources().getDimension(R.dimen.scanPadding);
+ FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(tempBitmap.getWidth() + 2 * padding, tempBitmap.getHeight() + 2 * padding);
+ layoutParams.gravity = Gravity.CENTER;
+ viewHolder.polygonView.setLayoutParams(layoutParams);
+
+ if (isOreantationChanged) {
+ points = getOutlinePoints(tempBitmap);
+ isPointsDownScaled = true;
+ }
+ viewHolder.polygonView.setPoints(points);
+ }
+ });
+ }
}
- private void init() {
- sourceImageView = (ImageView) view.findViewById(R.id.sourceImageView);
- scanButton = (Button) view.findViewById(R.id.scanButton);
- scanButton.setOnClickListener(new ScanButtonClickListener());
- sourceFrame = (FrameLayout) view.findViewById(R.id.sourceFrame);
- polygonView = (PolygonView) view.findViewById(R.id.polygonView);
- sourceFrame.post(new Runnable() {
- @Override
- public void run() {
- original = getBitmap();
- if (original != null) {
- setBitmap(original);
- }
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
+ if (requestCode == TAKE_PHOTO_REQUEST_CODE) {
+ if (resultCode == Activity.RESULT_OK) {
+ onPhotoTaken();
+ } else if (resultCode == Activity.RESULT_CANCELED) {
+ removeFile(takenPhotoLocation);
+ getActivity().setResult(Activity.RESULT_CANCELED);
+ getActivity().finish();
}
- });
+ }
}
- private Bitmap getBitmap() {
- Uri uri = getUri();
- try {
- Bitmap bitmap = Utils.getBitmap(getActivity(), uri);
- getActivity().getContentResolver().delete(uri, null, null);
- return bitmap;
- } catch (IOException e) {
- e.printStackTrace();
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ outState.putString(SAVED_ARG_TAKEN_PHOTO_LOCATION, takenPhotoLocation);
+ super.onSaveInstanceState(outState);
+ }
+
+ private MenuItem cropBtn;
+
+ @Override
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+ inflater.inflate(R.menu.scan_menu, menu);
+
+ cropBtn = menu.findItem(R.id.crop);
+ cropBtn.setVisible(!isCropMode);
+ super.onCreateOptionsMenu(menu, inflater);
+ }
+
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+
+ if (item.getItemId() == R.id.crop) {
+ onCropButtonClicked();
+ return true;
+ } else if (item.getItemId() == R.id.done) {
+ onDoneButtonClicked();
+ return true;
}
- return null;
+ return super.onOptionsItemSelected(item);
}
- private Uri getUri() {
- Uri uri = getArguments().getParcelable(ScanConstants.SELECTED_BITMAP);
- return uri;
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ /**
+ * Called From activity
+ */
+ public void onBackPressed() {
+ releaseAllBitmaps();
+ if (takenPhotoLocation != null) {
+ removeFile(takenPhotoLocation);
+ }
}
- private void setBitmap(Bitmap original) {
- Bitmap scaledBitmap = scaledBitmap(original, sourceFrame.getWidth(), sourceFrame.getHeight());
- sourceImageView.setImageBitmap(scaledBitmap);
- Bitmap tempBitmap = ((BitmapDrawable) sourceImageView.getDrawable()).getBitmap();
- Map pointFs = getEdgePoints(tempBitmap);
- polygonView.setPoints(pointFs);
- polygonView.setVisibility(View.VISIBLE);
+ private void releaseAllBitmaps() {
+ if (takenPhotoBitmap != null) takenPhotoBitmap.recycle();
+ if (documentBitmap != null) documentBitmap.recycle();
+ }
+
+ private void onCropButtonClicked() {
+ cropBtn.setVisible(false);
+ isCropMode = true;
+
+ Bitmap scaledBitmap = scaleBitmap(takenPhotoBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight());
+ viewHolder.sourceImageView.setImageBitmap(scaledBitmap);
+
+ if (!isPointsDownScaled) downScalePoints(points, takenPhotoBitmap, scaledBitmap.getWidth(), scaledBitmap.getHeight());
+ isPointsDownScaled = true;
+ viewHolder.polygonView.setPoints(points);
+
+ Bitmap tempBitmap = ((BitmapDrawable) viewHolder.sourceImageView.getDrawable()).getBitmap();
+ viewHolder.polygonView.setVisibility(View.VISIBLE);
int padding = (int) getResources().getDimension(R.dimen.scanPadding);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(tempBitmap.getWidth() + 2 * padding, tempBitmap.getHeight() + 2 * padding);
layoutParams.gravity = Gravity.CENTER;
- polygonView.setLayoutParams(layoutParams);
+ viewHolder.polygonView.setLayoutParams(layoutParams);
+ }
+
+
+ private void onDoneButtonClicked() {
+ if (isCropMode) {
+ isCropMode = false;
+ cropBtn.setVisible(true);
+
+ Map points = viewHolder.polygonView.getPoints();
+ if (isScanPointsValid(points)) {
+ new DocumentFromBitmapTask(takenPhotoBitmap, points, viewHolder.sourceImageView.getWidth(), viewHolder.sourceImageView.getHeight()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+ } else {
+ showErrorDialog();
+ }
+
+ } else {
+ File scannedDocFile = createImageFile("scanned_doc");
+ saveBitmapToFile(scannedDocFile, documentBitmap);
+ removeFile(takenPhotoLocation);
+ releaseAllBitmaps();
+
+ Intent intent = new Intent();
+ intent.putExtra(RESULT_IMAGE_PATH, scannedDocFile.getAbsolutePath());
+ getActivity().setResult(Activity.RESULT_OK, intent);
+ getActivity().finish();
+ }
}
- private Map getEdgePoints(Bitmap tempBitmap) {
+ private boolean saveBitmapToFile(File file, Bitmap bitmap) {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(file);
+ bitmap.compress(CompressFormat.JPEG, 100, out);
+ // PNG is a lossless format, the compression factor (100) is ignored
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private void onPhotoTaken() {
+ takenPhotoBitmap = getBitmapFromLocation(takenPhotoLocation);
+
+ DocumentFromBitmapTask documentFromBitmapTask = new DocumentFromBitmapTask(takenPhotoBitmap, null, 0, 0);
+ documentFromBitmapTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+ }
+
+ private static Bitmap cropDocumentFromBitmap(Bitmap bitmap, Map points) {
+ float x1 = (points.get(0).x);
+ float x2 = (points.get(1).x);
+ float x3 = (points.get(2).x);
+ float x4 = (points.get(3).x);
+ float y1 = (points.get(0).y);
+ float y2 = (points.get(1).y);
+ float y3 = (points.get(2).y);
+ float y4 = (points.get(3).y);
+ Log.d("", "POints(" + x1 + "," + y1 + ")(" + x2 + "," + y2 + ")(" + x3 + "," + y3 + ")(" + x4 + "," + y4 + ")");
+ Bitmap _bitmap = ScanActivity.getScannedBitmap(bitmap, x1, y1, x2, y2, x3, y3, x4, y4);
+ return _bitmap;
+ }
+
+ private static void upScalePoints(Map points, Bitmap original, int scaledImgWidth, int scaledImgHeight) {
+ int width = original.getWidth();
+ int height = original.getHeight();
+ float xRatio = (float) width / scaledImgWidth;
+ float yRatio = (float) height / scaledImgHeight;
+
+ points.get(0).x = (points.get(0).x) * xRatio;
+ points.get(1).x = (points.get(1).x) * xRatio;
+ points.get(2).x = (points.get(2).x) * xRatio;
+ points.get(3).x = (points.get(3).x) * xRatio;
+ points.get(0).y = (points.get(0).y) * yRatio;
+ points.get(1).y = (points.get(1).y) * yRatio;
+ points.get(2).y = (points.get(2).y) * yRatio;
+ points.get(3).y = (points.get(3).y) * yRatio;
+ }
+
+ private static void downScalePoints(Map points, Bitmap original, int targetImgWidth, int targetImgHeight) {
+ int width = original.getWidth();
+ int height = original.getHeight();
+ float xRatio = (float) width / targetImgWidth;
+ float yRatio = (float) height / targetImgHeight;
+
+ points.get(0).x = (points.get(0).x) / xRatio;
+ points.get(1).x = (points.get(1).x) / xRatio;
+ points.get(2).x = (points.get(2).x) / xRatio;
+ points.get(3).x = (points.get(3).x) / xRatio;
+ points.get(0).y = (points.get(0).y) / yRatio;
+ points.get(1).y = (points.get(1).y) / yRatio;
+ points.get(2).y = (points.get(2).y) / yRatio;
+ points.get(3).y = (points.get(3).y) / yRatio;
+ }
+
+ private void takePhoto() {
+ Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
+
+ File photoFile = createImageFile("takendocphoto");
+ takenPhotoLocation = photoFile.getAbsolutePath();
+
+ takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
+ startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST_CODE);
+ }
+
+ private File createImageFile(String fileName) {
+ File storageDir = getActivity().getExternalFilesDir("images");
+ if (storageDir == null) {
+ throw new RuntimeException("Not able to get to External storage");
+ }
+ File image = new File(storageDir, fileName + ".jpg");
+
+ return image;
+ }
+
+ private void removeFile(String absoluteLocation) {
+ if (absoluteLocation == null) return;
+
+ File f = new File(absoluteLocation);
+ if (f.exists()) {
+ f.delete();
+ }
+ }
+
+ private Bitmap getBitmapFromLocation(String absLocation) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+ return BitmapFactory.decodeFile(absLocation, options);
+ }
+
+ private static Map getEdgePoints(Bitmap tempBitmap) {
List pointFs = getContourEdgePoints(tempBitmap);
Map orderedPoints = orderedValidEdgePoints(tempBitmap, pointFs);
return orderedPoints;
}
- private List getContourEdgePoints(Bitmap tempBitmap) {
- float[] points = ((ScanActivity) getActivity()).getPoints(tempBitmap);
+ private static List getContourEdgePoints(Bitmap tempBitmap) {
+ float[] points = ScanActivity.getPoints(tempBitmap);
float x1 = points[0];
float x2 = points[1];
float x3 = points[2];
@@ -133,7 +383,7 @@ private List getContourEdgePoints(Bitmap tempBitmap) {
return pointFs;
}
- private Map getOutlinePoints(Bitmap tempBitmap) {
+ private static Map getOutlinePoints(Bitmap tempBitmap) {
Map outlinePoints = new HashMap<>();
outlinePoints.put(0, new PointF(0, 0));
outlinePoints.put(1, new PointF(tempBitmap.getWidth(), 0));
@@ -142,98 +392,114 @@ private Map getOutlinePoints(Bitmap tempBitmap) {
return outlinePoints;
}
- private Map orderedValidEdgePoints(Bitmap tempBitmap, List pointFs) {
- Map orderedPoints = polygonView.getOrderedPoints(pointFs);
- if (!polygonView.isValidShape(orderedPoints)) {
+ private static Map orderedValidEdgePoints(Bitmap tempBitmap, List pointFs) {
+ Map orderedPoints = PolygonView.getOrderedPoints(pointFs);
+ if (!PolygonView.isValidShape(orderedPoints)) {
orderedPoints = getOutlinePoints(tempBitmap);
}
return orderedPoints;
}
- private class ScanButtonClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- Map points = polygonView.getPoints();
- if (isScanPointsValid(points)) {
- new ScanAsyncTask(points).execute();
- } else {
- showErrorDialog();
- }
- }
- }
-
private void showErrorDialog() {
- SingleButtonDialogFragment fragment = new SingleButtonDialogFragment(R.string.ok, getString(R.string.cantCrop), "Error", true);
- FragmentManager fm = getActivity().getFragmentManager();
- fragment.show(fm, SingleButtonDialogFragment.class.toString());
+ Toast.makeText(getActivity(), R.string.cant_crop, Toast.LENGTH_LONG).show();
}
private boolean isScanPointsValid(Map points) {
return points.size() == 4;
}
- private Bitmap scaledBitmap(Bitmap bitmap, int width, int height) {
+ private Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height), Matrix.ScaleToFit.CENTER);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}
- private Bitmap getScannedBitmap(Bitmap original, Map points) {
- int width = original.getWidth();
- int height = original.getHeight();
- float xRatio = (float) original.getWidth() / sourceImageView.getWidth();
- float yRatio = (float) original.getHeight() / sourceImageView.getHeight();
-
- float x1 = (points.get(0).x) * xRatio;
- float x2 = (points.get(1).x) * xRatio;
- float x3 = (points.get(2).x) * xRatio;
- float x4 = (points.get(3).x) * xRatio;
- float y1 = (points.get(0).y) * yRatio;
- float y2 = (points.get(1).y) * yRatio;
- float y3 = (points.get(2).y) * yRatio;
- float y4 = (points.get(3).y) * yRatio;
- Log.d("", "POints(" + x1 + "," + y1 + ")(" + x2 + "," + y2 + ")(" + x3 + "," + y3 + ")(" + x4 + "," + y4 + ")");
- Bitmap _bitmap = ((ScanActivity) getActivity()).getScannedBitmap(original, x1, y1, x2, y2, x3, y3, x4, y4);
- return _bitmap;
+
+ protected void showProgressDialog() {
+ Bundle args = new Bundle();
+ args.putString(ProgressDialogFragment.EXTRA_MESSAGE, getString(R.string.transforming));
+ progressDialogFragment = new ProgressDialogFragment();
+ progressDialogFragment.setArguments(args);
+ progressDialogFragment.show(getFragmentManager(), "progress_dialog");
}
- private class ScanAsyncTask extends AsyncTask {
+ protected void dismissDialog() {
+ ProgressDialogFragment progressDialogFragment = (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progress_dialog");
+ if (progressDialogFragment != null) progressDialogFragment.dismissAllowingStateLoss();
+ }
- private Map points;
+ private void onDocumentFromBitmapTaskFinished(DocumentFromBitmapTaskResult result) {
+ documentBitmap = result.bitmap;
+ points = result.points;
+ isPointsDownScaled = false;
- public ScanAsyncTask(Map points) {
+ viewHolder.sourceImageView.setImageBitmap(scaleBitmap(documentBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight()));
+
+ viewHolder.polygonView.setVisibility(View.GONE);
+ }
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+
+ private class DocumentFromBitmapTask extends AsyncTask {
+
+ private final Bitmap bitmap;
+ private final Map points;
+ private final int scaledImgHeight;
+ private final int scaledImgWidth;
+
+ public DocumentFromBitmapTask(Bitmap bitmap, Map points, int scaledImgWidth, int scaledImgHeight) {
+ this.bitmap = bitmap;
this.points = points;
+ this.scaledImgHeight = scaledImgHeight;
+ this.scaledImgWidth = scaledImgWidth;
}
@Override
protected void onPreExecute() {
- super.onPreExecute();
- showProgressDialog(getString(R.string.scanning));
+ showProgressDialog();
}
@Override
- protected Bitmap doInBackground(Void... params) {
- return getScannedBitmap(original, points);
+ protected DocumentFromBitmapTaskResult doInBackground(Void... params) {
+ DocumentFromBitmapTaskResult result = new DocumentFromBitmapTaskResult();
+
+ if (points != null) {
+ result.points = points;
+ upScalePoints(points, bitmap, scaledImgWidth, scaledImgHeight);
+ result.bitmap = cropDocumentFromBitmap(bitmap, points);
+ } else {
+ result.points = getEdgePoints(bitmap);
+ result.bitmap = cropDocumentFromBitmap(bitmap, result.points);
+ }
+
+ return result;
}
@Override
- protected void onPostExecute(Bitmap bitmap) {
- super.onPostExecute(bitmap);
+ protected void onPostExecute(DocumentFromBitmapTaskResult documentFromBitmapTaskResult) {
+ onDocumentFromBitmapTaskFinished(documentFromBitmapTaskResult);
dismissDialog();
- Uri uri = Utils.getUri(getActivity(), bitmap);
- bitmap.recycle();
- scanner.onScanFinish(uri);
}
}
- protected void showProgressDialog(String message) {
- progressDialogFragment = new ProgressDialogFragment(message);
- FragmentManager fm = getFragmentManager();
- progressDialogFragment.show(fm, ProgressDialogFragment.class.toString());
+ private static class DocumentFromBitmapTaskResult {
+ Bitmap bitmap;
+ Map points;
}
- protected void dismissDialog() {
- progressDialogFragment.dismissAllowingStateLoss();
+ private static class ViewHolder {
+ private ImageView sourceImageView;
+ private FrameLayout sourceFrame;
+ private PolygonView polygonView;
+
+ void prepare(View parent) {
+ sourceImageView = (ImageView) parent.findViewById(R.id.sourceImageView);
+ sourceFrame = (FrameLayout) parent.findViewById(R.id.sourceFrame);
+ polygonView = (PolygonView) parent.findViewById(R.id.polygonView);
+ }
}
-}
\ No newline at end of file
+
+}
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/SingleButtonDialogFragment.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/SingleButtonDialogFragment.java
deleted file mode 100644
index ce0c066b..00000000
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/SingleButtonDialogFragment.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package com.scanlibrary;
-
-import android.annotation.SuppressLint;
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.app.DialogFragment;
-import android.content.DialogInterface;
-import android.os.Bundle;
-
-@SuppressLint("ValidFragment")
-public class SingleButtonDialogFragment extends DialogFragment {
-
- protected int positiveButtonTitle;
- protected String message;
- protected String title;
- protected boolean isCancelable;
-
- public SingleButtonDialogFragment(int positiveButtonTitle,
- String message, String title, boolean isCancelable) {
- this.positiveButtonTitle = positiveButtonTitle;
- this.message = message;
- this.title = title;
- this.isCancelable = isCancelable;
- }
-
-
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
- .setTitle(title)
- .setCancelable(isCancelable)
- .setMessage(message)
- .setPositiveButton(positiveButtonTitle,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
-
- }
- });
-
- return builder.create();
- }
-}
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/Utils.java b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/Utils.java
index b83c677e..1faf536c 100644
--- a/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/Utils.java
+++ b/ScanDemoExample/scanlibrary/src/main/java/com/scanlibrary/Utils.java
@@ -1,31 +1,80 @@
package com.scanlibrary;
-import android.content.Context;
-import android.graphics.Bitmap;
-import android.net.Uri;
-import android.provider.MediaStore;
+import android.app.Activity;
+import android.content.pm.ActivityInfo;
+import android.util.DisplayMetrics;
+import android.util.Log;
+import android.view.Surface;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-
-/**
- * Created by jhansi on 05/04/15.
- */
public class Utils {
+ private static final String TAG = Utils.class.getSimpleName();
+
private Utils() {
}
- public static Uri getUri(Context context, Bitmap bitmap) {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
- String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
- return Uri.parse(path);
- }
+ public static int getScreenOrientation(Activity activity) {
+ int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
+ DisplayMetrics dm = new DisplayMetrics();
+ activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
+ int width = dm.widthPixels;
+ int height = dm.heightPixels;
+ int orientation;
+ // if the device's natural orientation is portrait:
+ if ((rotation == Surface.ROTATION_0
+ || rotation == Surface.ROTATION_180) && height > width ||
+ (rotation == Surface.ROTATION_90
+ || rotation == Surface.ROTATION_270) && width > height) {
+ switch (rotation) {
+ case Surface.ROTATION_0:
+ orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ break;
+ case Surface.ROTATION_90:
+ orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+ break;
+ case Surface.ROTATION_180:
+ orientation =
+ ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
+ break;
+ case Surface.ROTATION_270:
+ orientation =
+ ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
+ break;
+ default:
+ Log.e(TAG, "Unknown screen orientation. Defaulting to " +
+ "portrait.");
+ orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ break;
+ }
+ }
+ // if the device's natural orientation is landscape or if the device
+ // is square:
+ else {
+ switch (rotation) {
+ case Surface.ROTATION_0:
+ orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+ break;
+ case Surface.ROTATION_90:
+ orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+ break;
+ case Surface.ROTATION_180:
+ orientation =
+ ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
+ break;
+ case Surface.ROTATION_270:
+ orientation =
+ ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
+ break;
+ default:
+ Log.e(TAG, "Unknown screen orientation. Defaulting to " +
+ "landscape.");
+ orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+ break;
+ }
+ }
- public static Bitmap getBitmap(Context context, Uri uri) throws IOException {
- Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
- return bitmap;
+ return orientation;
}
+
}
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_crop_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_crop_white_24dp.png
new file mode 100644
index 00000000..9bfca358
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_crop_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_done_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_done_white_24dp.png
new file mode 100644
index 00000000..c278b6c2
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-hdpi/ic_done_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/camera.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/camera.png
deleted file mode 100644
index 4cfe5473..00000000
Binary files a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/camera.png and /dev/null differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/gallery.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/gallery.png
deleted file mode 100644
index 02e65a33..00000000
Binary files a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/gallery.png and /dev/null differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_crop_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_crop_white_24dp.png
new file mode 100644
index 00000000..8e78d8e9
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_crop_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_done_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_done_white_24dp.png
new file mode 100644
index 00000000..6d84e143
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-mdpi/ic_done_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_crop_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_crop_white_24dp.png
new file mode 100644
index 00000000..859cdf78
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_crop_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_done_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_done_white_24dp.png
new file mode 100644
index 00000000..3b2b65d2
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xhdpi/ic_done_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_crop_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_crop_white_24dp.png
new file mode 100644
index 00000000..c0246cf2
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_crop_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_done_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_done_white_24dp.png
new file mode 100644
index 00000000..0ebb5555
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxhdpi/ic_done_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_crop_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_crop_white_24dp.png
new file mode 100644
index 00000000..3c027ef2
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_crop_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_done_white_24dp.png b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_done_white_24dp.png
new file mode 100644
index 00000000..d670618c
Binary files /dev/null and b/ScanDemoExample/scanlibrary/src/main/res/drawable-xxxhdpi/ic_done_white_24dp.png differ
diff --git a/ScanDemoExample/scanlibrary/src/main/res/layout/activity_scan.xml b/ScanDemoExample/scanlibrary/src/main/res/layout/activity_scan.xml
new file mode 100644
index 00000000..8c642dff
--- /dev/null
+++ b/ScanDemoExample/scanlibrary/src/main/res/layout/activity_scan.xml
@@ -0,0 +1,8 @@
+
+
+
+
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/layout/scan_fragment_layout.xml b/ScanDemoExample/scanlibrary/src/main/res/layout/fragment_scan.xml
similarity index 60%
rename from ScanDemoExample/scanlibrary/src/main/res/layout/scan_fragment_layout.xml
rename to ScanDemoExample/scanlibrary/src/main/res/layout/fragment_scan.xml
index 71ff5e77..5eedbeac 100644
--- a/ScanDemoExample/scanlibrary/src/main/res/layout/scan_fragment_layout.xml
+++ b/ScanDemoExample/scanlibrary/src/main/res/layout/fragment_scan.xml
@@ -1,24 +1,13 @@
-
-
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@android:color/black"
+ android:orientation="vertical">
@@ -34,7 +23,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
- android:adjustViewBounds="true" />
+ android:adjustViewBounds="true"/>
+ android:visibility="gone"/>
diff --git a/ScanDemoExample/scanlibrary/src/main/res/layout/pick_image_fragment.xml b/ScanDemoExample/scanlibrary/src/main/res/layout/pick_image_fragment.xml
deleted file mode 100644
index edb0e907..00000000
--- a/ScanDemoExample/scanlibrary/src/main/res/layout/pick_image_fragment.xml
+++ /dev/null
@@ -1,35 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/layout/result_layout.xml b/ScanDemoExample/scanlibrary/src/main/res/layout/result_layout.xml
deleted file mode 100644
index 569f0d44..00000000
--- a/ScanDemoExample/scanlibrary/src/main/res/layout/result_layout.xml
+++ /dev/null
@@ -1,81 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/layout/scan_layout.xml b/ScanDemoExample/scanlibrary/src/main/res/layout/scan_layout.xml
deleted file mode 100644
index dce546f5..00000000
--- a/ScanDemoExample/scanlibrary/src/main/res/layout/scan_layout.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/menu/scan_menu.xml b/ScanDemoExample/scanlibrary/src/main/res/menu/scan_menu.xml
new file mode 100644
index 00000000..1ea9943a
--- /dev/null
+++ b/ScanDemoExample/scanlibrary/src/main/res/menu/scan_menu.xml
@@ -0,0 +1,17 @@
+
+
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/values-lt/strings.xml b/ScanDemoExample/scanlibrary/src/main/res/values-lt/strings.xml
new file mode 100644
index 00000000..cc564237
--- /dev/null
+++ b/ScanDemoExample/scanlibrary/src/main/res/values-lt/strings.xml
@@ -0,0 +1,7 @@
+
+
+ Baigti
+ Transformuoti
+ Negalima iškirpti dokumento, pakeiskite taškus
+ Transformuojama...
+
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/values-ru/strings.xml b/ScanDemoExample/scanlibrary/src/main/res/values-ru/strings.xml
new file mode 100644
index 00000000..a61f59cd
--- /dev/null
+++ b/ScanDemoExample/scanlibrary/src/main/res/values-ru/strings.xml
@@ -0,0 +1,7 @@
+
+
+ Завершить
+ Обрезать
+ Не можем обрезать изображение, меняйте пункты
+ Трансформируется...
+
\ No newline at end of file
diff --git a/ScanDemoExample/scanlibrary/src/main/res/values/strings.xml b/ScanDemoExample/scanlibrary/src/main/res/values/strings.xml
index 9e11f732..ded5ef8c 100644
--- a/ScanDemoExample/scanlibrary/src/main/res/values/strings.xml
+++ b/ScanDemoExample/scanlibrary/src/main/res/values/strings.xml
@@ -1,12 +1,9 @@
- ScanLibrary
- Hello world!
- Settings
- Scanning
- Done
- Scan
- Cant crop the image, change the points
- Ok
+ ScanLibrary
+ Finish
+ Crop
+ Can\'t crop the image, change the points
+ Transforming...