Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 1 | /* |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 30 | package com.android.gallery3d.filtershow.filters; |
| 31 | |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 32 | import android.app.ProgressDialog; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
| 34 | import android.util.Log; |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 35 | import android.view.View; |
| 36 | import android.widget.SeekBar; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 37 | import android.widget.Toast; |
| 38 | |
Likai Ding | c83b5b7 | 2016-05-25 14:48:41 +0800 | [diff] [blame] | 39 | import org.codeaurora.gallery.R; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 40 | import com.android.gallery3d.app.GalleryActivity; |
| 41 | import com.android.gallery3d.filtershow.FilterShowActivity; |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 42 | import com.android.gallery3d.filtershow.controller.Parameter; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 43 | import com.android.gallery3d.filtershow.editors.TrueScannerEditor; |
| 44 | import com.android.gallery3d.filtershow.imageshow.ImageTrueScanner; |
| 45 | import com.android.gallery3d.filtershow.imageshow.MasterImage; |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 46 | import com.android.gallery3d.ui.MenuExecutor; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 47 | import static com.android.gallery3d.filtershow.imageshow.ImageTrueScanner.*; |
| 48 | |
| 49 | public class TrueScannerActs extends SimpleImageFilter { |
| 50 | public static final String SERIALIZATION_NAME = "TrueScannerActs"; |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 51 | //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 Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 55 | 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 Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 63 | private int[] oldPts = new int[POINTS_LEN]; |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 64 | private ProgressDialog mProgressDialog; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 65 | |
| 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 | |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 75 | public static boolean setRotating(boolean isRotating) { |
| 76 | return rotating = isRotating; |
| 77 | } |
| 78 | |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 79 | 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 Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 100 | private native int[] processImage(Bitmap orgBitmap, Bitmap rectifiedBitmap, int[] cornerPts); |
| 101 | private native int enhanceImage(Bitmap orgBitmap, Bitmap rectifiedBitmap); |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 102 | |
| 103 | private synchronized boolean acquireLock(boolean isAcquiring) { |
| 104 | if(mLocked != isAcquiring) { |
| 105 | mLocked = isAcquiring; |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 111 | private void showProgressDialog() { |
Kedi Xu | db643b4 | 2016-08-24 15:48:09 +0800 | [diff] [blame] | 112 | if (null != sActivity) { |
| 113 | sActivity.runOnUiThread(new Runnable() { |
| 114 | public void run() { |
zhuw | 0fa8e4a | 2017-12-12 16:56:10 +0800 | [diff] [blame^] | 115 | if (!sActivity.isFinishing()) { |
| 116 | mProgressDialog = ProgressDialog.show(sActivity, |
| 117 | "", "Processing...", true, false); |
| 118 | mProgressDialog.show(); |
| 119 | } |
Kedi Xu | db643b4 | 2016-08-24 15:48:09 +0800 | [diff] [blame] | 120 | } |
| 121 | }); |
| 122 | } |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private void dismissProgressDialog() { |
Kedi Xu | db643b4 | 2016-08-24 15:48:09 +0800 | [diff] [blame] | 126 | 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 Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 133 | } |
Kedi Xu | db643b4 | 2016-08-24 15:48:09 +0800 | [diff] [blame] | 134 | }); |
| 135 | } |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Bitmap apply(Bitmap bitmap, float not_use, int quality) { |
| 140 | if(bitmap == null) |
| 141 | return null; |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 142 | 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 Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 149 | return bitmap; |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 150 | if(ImageTrueScanner.getCordsUIState()) { |
| 151 | return bitmap; |
| 152 | } |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 153 | if(!acquireLock(true)) { |
Kedi Xu | db643b4 | 2016-08-24 15:48:09 +0800 | [diff] [blame] | 154 | showToast("Still processing the previous request... ", Toast.LENGTH_LONG); |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 155 | return bitmap; |
| 156 | } |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 157 | new Throwable().printStackTrace(); |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 158 | 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 | } |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 168 | 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 Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 175 | int[] outputSize = processImage(bitmap, rectifiedImage, resultPts); |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 176 | rectifiedImage = Bitmap.createBitmap( |
| 177 | rectifiedImage, 0, 0, outputSize[0], outputSize[1]); |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 178 | if(ImageTrueScanner.getRemoveGlareButtonStatus()) { |
jinwu | 4fafa7a | 2017-04-07 13:27:48 +0800 | [diff] [blame] | 179 | Bitmap enhancedImage = Bitmap.createBitmap( |
| 180 | outputSize[0], outputSize[1], Bitmap.Config.ARGB_8888); |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 181 | showProgressDialog(); |
| 182 | enhanceImage(rectifiedImage, enhancedImage); |
| 183 | dismissProgressDialog(); |
| 184 | rectifiedImage = enhancedImage; |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 185 | } |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 186 | acquireLock(false); |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 187 | |
| 188 | return rectifiedImage; |
| 189 | } |
| 190 | |
| 191 | static { |
| 192 | try { |
Jack Yoo | e94e766 | 2016-05-18 11:34:04 -0700 | [diff] [blame] | 193 | System.loadLibrary("jni_truescanner_v2"); |
Jack Yoo | 66d2144 | 2015-10-27 16:27:49 -0700 | [diff] [blame] | 194 | isTrueScannerEnabled = true; |
| 195 | } catch(UnsatisfiedLinkError e) { |
| 196 | isTrueScannerEnabled = false; |
| 197 | } |
| 198 | } |
| 199 | } |