blob: 6260502769a4c174bbdae01d5292cc1d1bd09c15 [file] [log] [blame]
Jack Yoo66d21442015-10-27 16:27:49 -07001/*
jinwu4fafa7a2017-04-07 13:27:48 +08002 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Jack Yoo66d21442015-10-27 16:27:49 -07003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package com.android.gallery3d.filtershow.filters;
31
Jack Yooe94e7662016-05-18 11:34:04 -070032import android.app.ProgressDialog;
Jack Yoo66d21442015-10-27 16:27:49 -070033import android.graphics.Bitmap;
34import android.util.Log;
Jack Yooe94e7662016-05-18 11:34:04 -070035import android.view.View;
36import android.widget.SeekBar;
Jack Yoo66d21442015-10-27 16:27:49 -070037import android.widget.Toast;
38
Likai Dingc83b5b72016-05-25 14:48:41 +080039import org.codeaurora.gallery.R;
Jack Yoo66d21442015-10-27 16:27:49 -070040import com.android.gallery3d.app.GalleryActivity;
41import com.android.gallery3d.filtershow.FilterShowActivity;
Jack Yooe94e7662016-05-18 11:34:04 -070042import com.android.gallery3d.filtershow.controller.Parameter;
Jack Yoo66d21442015-10-27 16:27:49 -070043import com.android.gallery3d.filtershow.editors.TrueScannerEditor;
44import com.android.gallery3d.filtershow.imageshow.ImageTrueScanner;
45import com.android.gallery3d.filtershow.imageshow.MasterImage;
Jack Yooe94e7662016-05-18 11:34:04 -070046import com.android.gallery3d.ui.MenuExecutor;
Jack Yoo66d21442015-10-27 16:27:49 -070047import static com.android.gallery3d.filtershow.imageshow.ImageTrueScanner.*;
48
49public class TrueScannerActs extends SimpleImageFilter {
50 public static final String SERIALIZATION_NAME = "TrueScannerActs";
jinwu4fafa7a2017-04-07 13:27:48 +080051 //The minimum resolution that TrueScanner library supports is VGA, i.e. 640x480.
52 public static final int MIN_WIDTH = 640;
53 public static final int MIN_HEIGHT = 480;
54 private static boolean rotating = false;
Jack Yoo66d21442015-10-27 16:27:49 -070055 private static final boolean DEBUG = false;
56 private static boolean isTrueScannerEnabled = true;
57 private static boolean isPointsAcquired;
58 private final static int POINTS_LEN = 8;
59 private static int[] mAcquiredPoints = new int[POINTS_LEN+2];
60 protected boolean isWhiteBoard = false;
61 private boolean mLocked = false;
62 private Bitmap rectifiedImage = null;
Jack Yoo66d21442015-10-27 16:27:49 -070063 private int[] oldPts = new int[POINTS_LEN];
Jack Yooe94e7662016-05-18 11:34:04 -070064 private ProgressDialog mProgressDialog;
Jack Yoo66d21442015-10-27 16:27:49 -070065
66 private void printDebug(String str) {
67 if(DEBUG)
68 android.util.Log.d("TrueScanner", str);
69 }
70
71 public static boolean isTrueScannerEnabled() {
72 return isTrueScannerEnabled;
73 }
74
jinwu4fafa7a2017-04-07 13:27:48 +080075 public static boolean setRotating(boolean isRotating) {
76 return rotating = isRotating;
77 }
78
Jack Yoo66d21442015-10-27 16:27:49 -070079 public TrueScannerActs() {
80 mName = "TrueScannerActs";
81 isPointsAcquired = false;
82 }
83
84 public FilterRepresentation getDefaultRepresentation() {
85 FilterBasicRepresentation representation = (FilterBasicRepresentation) super.getDefaultRepresentation();
86 representation.setName("TrueScanner");
87 representation.setSerializationName(SERIALIZATION_NAME);
88 representation.setFilterClass(TrueScannerActs.class);
89 representation.setTextId(R.string.truescanner_normal);
90 representation.setMinimum(0);
91 representation.setMaximum(10);
92 representation.setValue(0);
93 representation.setDefaultValue(0);
94 representation.setSupportsPartialRendering(false);
95 representation.setEditorId(TrueScannerEditor.ID);
96
97 return representation;
98 }
99
Jack Yooe94e7662016-05-18 11:34:04 -0700100 private native int[] processImage(Bitmap orgBitmap, Bitmap rectifiedBitmap, int[] cornerPts);
101 private native int enhanceImage(Bitmap orgBitmap, Bitmap rectifiedBitmap);
Jack Yoo66d21442015-10-27 16:27:49 -0700102
103 private synchronized boolean acquireLock(boolean isAcquiring) {
104 if(mLocked != isAcquiring) {
105 mLocked = isAcquiring;
106 return true;
107 }
108 return false;
109 }
110
Jack Yooe94e7662016-05-18 11:34:04 -0700111 private void showProgressDialog() {
Kedi Xudb643b42016-08-24 15:48:09 +0800112 if (null != sActivity) {
113 sActivity.runOnUiThread(new Runnable() {
114 public void run() {
zhuw0fa8e4a2017-12-12 16:56:10 +0800115 if (!sActivity.isFinishing()) {
116 mProgressDialog = ProgressDialog.show(sActivity,
117 "", "Processing...", true, false);
118 mProgressDialog.show();
119 }
Kedi Xudb643b42016-08-24 15:48:09 +0800120 }
121 });
122 }
Jack Yooe94e7662016-05-18 11:34:04 -0700123 }
124
125 private void dismissProgressDialog() {
Kedi Xudb643b42016-08-24 15:48:09 +0800126 if (null != sActivity) {
127 sActivity.runOnUiThread(new Runnable() {
128 public void run() {
129 if (mProgressDialog != null && mProgressDialog.isShowing()) {
130 mProgressDialog.dismiss();
131 mProgressDialog = null;
132 }
Jack Yoo66d21442015-10-27 16:27:49 -0700133 }
Kedi Xudb643b42016-08-24 15:48:09 +0800134 });
135 }
Jack Yoo66d21442015-10-27 16:27:49 -0700136 }
137
138 @Override
139 public Bitmap apply(Bitmap bitmap, float not_use, int quality) {
140 if(bitmap == null)
141 return null;
jinwu4fafa7a2017-04-07 13:27:48 +0800142 int w = bitmap.getWidth();
143 int h = bitmap.getHeight();
144 if (w < h) {
145 w = h;
146 h = bitmap.getWidth();
147 }
148 if(w <= MIN_WIDTH || h <= MIN_HEIGHT)
Jack Yoo66d21442015-10-27 16:27:49 -0700149 return bitmap;
Jack Yooe94e7662016-05-18 11:34:04 -0700150 if(ImageTrueScanner.getCordsUIState()) {
151 return bitmap;
152 }
Jack Yoo66d21442015-10-27 16:27:49 -0700153 if(!acquireLock(true)) {
Kedi Xudb643b42016-08-24 15:48:09 +0800154 showToast("Still processing the previous request... ", Toast.LENGTH_LONG);
Jack Yoo66d21442015-10-27 16:27:49 -0700155 return bitmap;
156 }
Jack Yooe94e7662016-05-18 11:34:04 -0700157 new Throwable().printStackTrace();
Jack Yoo66d21442015-10-27 16:27:49 -0700158 int[] pts = ImageTrueScanner.getDeterminedPoints();
159 int[] resultPts = new int[POINTS_LEN];
160 float xScale = ((float)bitmap.getWidth())/pts[POINTS_LEN];
161 float yScale = ((float)bitmap.getHeight())/pts[POINTS_LEN+1];
162 for(int i=0; i < POINTS_LEN; i++) {
163 if(i%2 == 0)
164 resultPts[i] = (int)((pts[i] - pts[POINTS_LEN+2])*xScale);
165 else
166 resultPts[i] = (int)((pts[i] - pts[POINTS_LEN+3])*yScale);
167 }
jinwu4fafa7a2017-04-07 13:27:48 +0800168 if (rotating && rectifiedImage != null) {
169 acquireLock(false);
170 rotating = false;
171 return rectifiedImage;
172 }
173 rectifiedImage = Bitmap.createBitmap(
174 bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Jack Yooe94e7662016-05-18 11:34:04 -0700175 int[] outputSize = processImage(bitmap, rectifiedImage, resultPts);
jinwu4fafa7a2017-04-07 13:27:48 +0800176 rectifiedImage = Bitmap.createBitmap(
177 rectifiedImage, 0, 0, outputSize[0], outputSize[1]);
Jack Yooe94e7662016-05-18 11:34:04 -0700178 if(ImageTrueScanner.getRemoveGlareButtonStatus()) {
jinwu4fafa7a2017-04-07 13:27:48 +0800179 Bitmap enhancedImage = Bitmap.createBitmap(
180 outputSize[0], outputSize[1], Bitmap.Config.ARGB_8888);
Jack Yooe94e7662016-05-18 11:34:04 -0700181 showProgressDialog();
182 enhanceImage(rectifiedImage, enhancedImage);
183 dismissProgressDialog();
184 rectifiedImage = enhancedImage;
Jack Yoo66d21442015-10-27 16:27:49 -0700185 }
Jack Yoo66d21442015-10-27 16:27:49 -0700186 acquireLock(false);
Jack Yoo66d21442015-10-27 16:27:49 -0700187
188 return rectifiedImage;
189 }
190
191 static {
192 try {
Jack Yooe94e7662016-05-18 11:34:04 -0700193 System.loadLibrary("jni_truescanner_v2");
Jack Yoo66d21442015-10-27 16:27:49 -0700194 isTrueScannerEnabled = true;
195 } catch(UnsatisfiedLinkError e) {
196 isTrueScannerEnabled = false;
197 }
198 }
199}