Skip to content

Commit

Permalink
Merge branch 'Rotation' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejLukasevic committed Jan 27, 2016
2 parents 59340a3 + 2f18774 commit bbf7293
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 32 deletions.
27 changes: 17 additions & 10 deletions ScanDemoExample/build/intermediates/dex-cache/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
<items version="2" >

<item
jar="D:\AndroidSDK\extras\android\m2repository\com\android\support\support-annotations\21.0.3\support-annotations-21.0.3.jar"
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\com.android.support\support-v4\21.0.3\jars\classes.jar"
jumboMode="false"
revision="23.0.2"
sha1="4b74cefe1f0c1b819e7260c8627a14674e37fd35">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\support-annotations-21.0.3-34a57f82165fcc191123ae4d70b3d0379c72c265.jar" />
sha1="2c91c949a45a21cdecf26e03951e46c7beec9ad8">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-66995c60b970d702830aa0605612c69aa1b9a355.jar" />
</item>
<item
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\21.0.3\jars\classes.jar"
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\com.davemorrissey.labs\subsampling-scale-image-view\2.3.0\jars\classes.jar"
jumboMode="false"
revision="23.0.2"
sha1="81d42bf983a8741f4888a6e333e454e4a5e0eeb7">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-17c37e4d74c16a2b8d488cc0c2d716655daf0c83.jar" />
sha1="705ffc2c856ea563f08d87cc59e797eb37c69e3f">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-a4ac2123659a362aa8d67a4343a18538a740a075.jar" />
</item>
<item
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\com.android.support\support-v4\21.0.3\jars\classes.jar"
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\21.0.3\jars\classes.jar"
jumboMode="false"
revision="23.0.2"
sha1="2c91c949a45a21cdecf26e03951e46c7beec9ad8">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-66995c60b970d702830aa0605612c69aa1b9a355.jar" />
sha1="81d42bf983a8741f4888a6e333e454e4a5e0eeb7">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-17c37e4d74c16a2b8d488cc0c2d716655daf0c83.jar" />
</item>
<item
jar="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\exploded-aar\ScanDemoExample\scanlibrary\unspecified\jars\classes.jar"
jumboMode="false"
revision="23.0.2"
sha1="74310a3d25228a74911f4d4376df2dfd8449a3ca">
sha1="1ca346c89899db689554b90d197c36ee298bd2e3">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\classes-64d90915416580e77d24bb965361fe9e8c7cb330.jar" />
</item>
<item
Expand All @@ -36,5 +36,12 @@
sha1="01ec05bfbafcc07646ba813000bf2ef11742dd03">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\internal_impl-21.0.3-88add6b1a8b650f07c625608257884906b95b511.jar" />
</item>
<item
jar="D:\AndroidSDK\extras\android\m2repository\com\android\support\support-annotations\21.0.3\support-annotations-21.0.3.jar"
jumboMode="false"
revision="23.0.2"
sha1="4b74cefe1f0c1b819e7260c8627a14674e37fd35">
<dex dex="D:\Android Workspace\AndroidDocumentScanner\ScanDemoExample\app\build\intermediates\pre-dexed\debug\support-annotations-21.0.3-34a57f82165fcc191123ae4d70b3d0379c72c265.jar" />
</item>

</items>
2 changes: 2 additions & 0 deletions ScanDemoExample/scanlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Library for Image View That have Ability to zoom...
compile 'com.davemorrissey.labs:subsampling-scale-image-view:2.3.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import android.widget.ImageView;
import android.widget.Toast;

import com.davemorrissey.labs.subscaleview.ScaleImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -60,10 +62,8 @@ public class ScanFragment extends Fragment {
private Map<Integer, PointF> points;

private boolean isCropMode = false;
private boolean isPointsDownScaled = false;

private int previousOreantation = -1;
private boolean isOreantationChanged = false;

// ===========================================================
// Constructors
Expand Down Expand Up @@ -98,19 +98,18 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
int currentOreantation = Utils.getScreenOrientation(getActivity());
if (previousOreantation == -1) {
previousOreantation = currentOreantation;
isOreantationChanged = false;
} else if (previousOreantation == currentOreantation) {
isOreantationChanged = false;
} else {
previousOreantation = currentOreantation;
isOreantationChanged = true;
} else if (previousOreantation != currentOreantation) {
points = null;
}

if (takenPhotoLocation == null) {
takePhoto();
} else {
if (documentBitmap != null) {
viewHolder.sourceImageView.setImageBitmap(documentBitmap);
viewHolder.sourceImageView.setVisibility(View.INVISIBLE);
viewHolder.scaleImageView.setImageBitmap(documentBitmap);
viewHolder.scaleImageView.setVisibility(View.VISIBLE);
}
}

Expand All @@ -128,9 +127,8 @@ public void run() {
layoutParams.gravity = Gravity.CENTER;
viewHolder.polygonView.setLayoutParams(layoutParams);

if (isOreantationChanged) {
if (points == null) {
points = getOutlinePoints(tempBitmap);
isPointsDownScaled = true;
}
viewHolder.polygonView.setPoints(points);
}
Expand Down Expand Up @@ -158,13 +156,17 @@ public void onSaveInstanceState(Bundle outState) {
}

private MenuItem cropBtn;
private MenuItem rotateBtn;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.scan_menu, menu);

cropBtn = menu.findItem(R.id.crop);
rotateBtn = menu.findItem(R.id.rotate);

cropBtn.setVisible(!isCropMode);
rotateBtn.setVisible(!isCropMode);
super.onCreateOptionsMenu(menu, inflater);
}

Expand All @@ -178,6 +180,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else if (item.getItemId() == R.id.done) {
onDoneButtonClicked();
return true;
} else if (item.getItemId() == R.id.rotate) {
onRotateButtonClicked();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand All @@ -203,28 +208,50 @@ private void releaseAllBitmaps() {

private void onCropButtonClicked() {
cropBtn.setVisible(false);
rotateBtn.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);
viewHolder.sourceImageView.setVisibility(View.VISIBLE);
viewHolder.scaleImageView.setVisibility(View.GONE);

Bitmap tempBitmap = ((BitmapDrawable) viewHolder.sourceImageView.getDrawable()).getBitmap();
viewHolder.polygonView.setVisibility(View.VISIBLE);

points = getEdgePoints(tempBitmap);

viewHolder.polygonView.setPoints(points);
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);
}

private void onRotateButtonClicked() {
Bitmap takenPhotoBitmapTmp = Utils.rotateBitmap(takenPhotoBitmap, -90);
takenPhotoBitmap.recycle();
takenPhotoBitmap = takenPhotoBitmapTmp;

Bitmap documentBitmapTmp = Utils.rotateBitmap(documentBitmap, -90);
documentBitmap.recycle();
documentBitmap = documentBitmapTmp;

Bitmap scaledBitmap = scaleBitmap(documentBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight());
viewHolder.sourceImageView.setImageBitmap(scaledBitmap);
viewHolder.sourceImageView.setVisibility(View.INVISIBLE);
viewHolder.scaleImageView.setImageBitmap(scaledBitmap);
viewHolder.scaleImageView.setVisibility(View.VISIBLE);
// Bitmap tempBitmap = ((BitmapDrawable) viewHolder.sourceImageView.getDrawable()).getBitmap();
points = getOutlinePoints(viewHolder.sourceFrame);
}


private void onDoneButtonClicked() {
if (isCropMode) {
isCropMode = false;
cropBtn.setVisible(true);
rotateBtn.setVisible(true);

Map<Integer, PointF> points = viewHolder.polygonView.getPoints();
if (isScanPointsValid(points)) {
Expand Down Expand Up @@ -392,6 +419,15 @@ private static Map<Integer, PointF> getOutlinePoints(Bitmap tempBitmap) {
return outlinePoints;
}

private static Map<Integer, PointF> getOutlinePoints(View view) {
Map<Integer, PointF> outlinePoints = new HashMap<>();
outlinePoints.put(0, new PointF(0, 0));
outlinePoints.put(1, new PointF(view.getWidth(), 0));
outlinePoints.put(2, new PointF(0, view.getHeight()));
outlinePoints.put(3, new PointF(view.getWidth(), view.getHeight()));
return outlinePoints;
}

private static Map<Integer, PointF> orderedValidEdgePoints(Bitmap tempBitmap, List<PointF> pointFs) {
Map<Integer, PointF> orderedPoints = PolygonView.getOrderedPoints(pointFs);
if (!PolygonView.isValidShape(orderedPoints)) {
Expand Down Expand Up @@ -431,9 +467,12 @@ protected void dismissDialog() {
private void onDocumentFromBitmapTaskFinished(DocumentFromBitmapTaskResult result) {
documentBitmap = result.bitmap;
points = result.points;
isPointsDownScaled = false;

viewHolder.sourceImageView.setImageBitmap(scaleBitmap(documentBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight()));
Bitmap scaledBitmap = scaleBitmap(documentBitmap, viewHolder.sourceFrame.getWidth(), viewHolder.sourceFrame.getHeight());
viewHolder.sourceImageView.setImageBitmap(scaledBitmap);
viewHolder.sourceImageView.setVisibility(View.INVISIBLE);
viewHolder.scaleImageView.setImageBitmap(scaledBitmap);
viewHolder.scaleImageView.setVisibility(View.VISIBLE);

viewHolder.polygonView.setVisibility(View.GONE);
}
Expand Down Expand Up @@ -463,6 +502,7 @@ protected void onPreExecute() {

@Override
protected DocumentFromBitmapTaskResult doInBackground(Void... params) {
System.gc();
DocumentFromBitmapTaskResult result = new DocumentFromBitmapTaskResult();

if (points != null) {
Expand Down Expand Up @@ -491,11 +531,13 @@ private static class DocumentFromBitmapTaskResult {

private static class ViewHolder {
private ImageView sourceImageView;
private ScaleImageView scaleImageView;
private FrameLayout sourceFrame;
private PolygonView polygonView;

void prepare(View parent) {
sourceImageView = (ImageView) parent.findViewById(R.id.sourceImageView);
scaleImageView = (ScaleImageView) parent.findViewById(R.id.scaleImage);
sourceFrame = (FrameLayout) parent.findViewById(R.id.sourceFrame);
polygonView = (PolygonView) parent.findViewById(R.id.polygonView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
Expand Down Expand Up @@ -77,4 +79,11 @@ public static int getScreenOrientation(Activity activity) {
return orientation;
}

public static Bitmap rotateBitmap(Bitmap source, float angle) {
System.gc();
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions ScanDemoExample/scanlibrary/src/main/res/layout/fragment_scan.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">

<FrameLayout
android:layout_width="match_parent"
Expand All @@ -24,6 +24,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>

</FrameLayout>

<com.scanlibrary.PolygonView
Expand All @@ -34,5 +35,12 @@
android:visibility="gone"/>
</FrameLayout>

<com.davemorrissey.labs.subscaleview.ScaleImageView
android:id="@+id/scaleImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"/>


</RelativeLayout>
</FrameLayout>
6 changes: 6 additions & 0 deletions ScanDemoExample/scanlibrary/src/main/res/menu/scan_menu.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/rotate"
android:icon="@drawable/ic_rotate_90_degrees_ccw_white_24dp"
android:showAsAction="always"
android:title="@string/rotate"/>

<item
android:id="@+id/crop"
android:icon="@drawable/ic_crop_white_24dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<string name="finish">Baigti</string>
<string name="crop">Transformuoti</string>
<string name="rotate">Pasukti</string>
<string name="cant_crop">Negalima iškirpti dokumento, pakeiskite taškus</string>
<string name="transforming">Transformuojama...</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<string name="finish">Завершить</string>
<string name="crop">Обрезать</string>
<string name="rotate">Повернуть</string>
<string name="cant_crop">Не можем обрезать изображение, меняйте пункты</string>
<string name="transforming">Трансформируется...</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="document_scann_lib_name" translatable="false">ScanLibrary</string>
<string name="finish">Finish</string>
<string name="crop">Crop</string>
<string name="rotate">Rotate</string>
<string name="cant_crop">Can\'t crop the image, change the points</string>
<string name="transforming">Transforming...</string>
</resources>

0 comments on commit bbf7293

Please sign in to comment.