├── brdcompat
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── org
│ │ └── bonnyfone
│ │ └── brdcompat
│ │ ├── IBitmapRegionDecoder.java
│ │ ├── BitmapRegionDecoderNative.java
│ │ ├── BitmapRegionDecoderGinger.java
│ │ └── BitmapRegionDecoderCompat.java
├── build.gradle
├── proguard-rules.pro
└── brdcompat.iml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── BitmapRegionDecoderCompat.iml
├── gradlew.bat
├── README.md
└── gradlew
/brdcompat/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':brdcompat'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bonnyfone/brdcompat/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/brdcompat/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/
4 | .DS_Store
5 | /build
6 | build.gradle.bintray
7 | build.gradle.debug
8 | toggle_bintray.sh
9 | check_bintray.sh
10 | publish_bintray.sh
11 | brdcompat/build.gradle.bintray
12 | brdcompat/build.gradle.debug
13 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Oct 23 20:36:59 CEST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/brdcompat/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "22.0.1"
6 |
7 | defaultConfig {
8 | minSdkVersion 8
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | }
24 |
--------------------------------------------------------------------------------
/brdcompat/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /media/Mistero/android-sdk-linux/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/BitmapRegionDecoderCompat.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/brdcompat/src/main/java/org/bonnyfone/brdcompat/IBitmapRegionDecoder.java:
--------------------------------------------------------------------------------
1 | package org.bonnyfone.brdcompat;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.graphics.Rect;
6 |
7 | /**
8 | * Common interface for BitmapRegionDecoder
9 | */
10 | interface IBitmapRegionDecoder {
11 |
12 | /**
13 | * Decodes a rectangle region in the image specified by rect.
14 | * @param rect
15 | * @param options
16 | * @return
17 | */
18 | Bitmap decodeRegion(Rect rect, BitmapFactory.Options options);
19 |
20 | /**
21 | * Returns the original image's height
22 | * @return
23 | */
24 | int getHeight();
25 |
26 |
27 | /**
28 | * Returns the original image's width
29 | * @return
30 | */
31 | int getWidth();
32 |
33 | /**
34 | * Returns true if this region decoder has been recycled.
35 | * @return
36 | */
37 | boolean isRecycled();
38 |
39 | /**
40 | * Frees up the memory associated with this region decoder, and mark the region decoder as "dead",
41 | * meaning it will throw an exception if decodeRegion(), getWidth() or getHeight() is called.
42 | */
43 | void recycle();
44 | }
45 |
--------------------------------------------------------------------------------
/brdcompat/src/main/java/org/bonnyfone/brdcompat/BitmapRegionDecoderNative.java:
--------------------------------------------------------------------------------
1 | package org.bonnyfone.brdcompat;
2 |
3 | import android.annotation.TargetApi;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.graphics.Rect;
7 | import android.os.Build;
8 |
9 | import java.io.FileDescriptor;
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 |
13 | /**
14 | * Internal wrapper over the native API, allows to maintain type interoperability
15 | */
16 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
17 | class BitmapRegionDecoderNative implements IBitmapRegionDecoder {
18 |
19 | private android.graphics.BitmapRegionDecoder nativeDecoder;
20 |
21 | private BitmapRegionDecoderNative(android.graphics.BitmapRegionDecoder nativeDecoder){
22 | this.nativeDecoder = nativeDecoder;
23 | }
24 |
25 | @Override
26 | public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) {
27 | return nativeDecoder.decodeRegion(rect, options);
28 | }
29 |
30 | @Override
31 | public int getHeight() {
32 | return nativeDecoder.getHeight();
33 | }
34 |
35 | @Override
36 | public int getWidth() {
37 | return nativeDecoder.getWidth();
38 | }
39 |
40 | @Override
41 | public boolean isRecycled() {
42 | return nativeDecoder.isRecycled();
43 | }
44 |
45 | @Override
46 | public void recycle() {
47 | nativeDecoder.recycle();
48 | }
49 |
50 | public static BitmapRegionDecoderNative newInstance(String pathName, boolean isShareable) throws IOException {
51 | return new BitmapRegionDecoderNative(android.graphics.BitmapRegionDecoder.newInstance(pathName, isShareable));
52 | }
53 |
54 | public static BitmapRegionDecoderNative newInstance(FileDescriptor fd, boolean isShareable) throws IOException {
55 | return new BitmapRegionDecoderNative(android.graphics.BitmapRegionDecoder.newInstance(fd, isShareable));
56 | }
57 |
58 | public static BitmapRegionDecoderNative newInstance(byte[] data, int offset, int length, boolean isShareable) throws IOException {
59 | return new BitmapRegionDecoderNative(android.graphics.BitmapRegionDecoder.newInstance(data, offset, length, isShareable));
60 | }
61 |
62 | public static BitmapRegionDecoderNative newInstance(InputStream is, boolean isShareable) throws IOException {
63 | return new BitmapRegionDecoderNative(android.graphics.BitmapRegionDecoder.newInstance(is, isShareable));
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BitmapRegionDecoderCompat
2 | [](https://android-arsenal.com/details/1/2199) [  ](https://bintray.com/bonnyfone/maven/org.bonnyfone.brdcompat/_latestVersion)
3 |
4 | **BitmapRegionDecoderCompat** (*BRDCompat*) is the 'compat' version of the official [BitmapRegionDecoder] API.
5 |
6 | > *BitmapRegionDecoder* can be used to decode a rectangle region from an image; it is particularly useful when an original image is large and you only need parts of the image.
7 |
8 | The standard *BitmapRegionDecoder* (aka *BRD*) requires API level 10.
9 |
10 | **BRDCompat extends the compatibility down to API 8 and adds some extra useful methods**.
11 |
12 |
13 |
14 | Features
15 | ----
16 |
17 |
18 | - Extends compatibility down to **API 8** (from 10)
19 | - Adds new methods like **decodeBestRegion()**, which extracts the *best* image sub-region given the specified params (required size, gravity). The best region means the biggest visual portion of the (downsampled) original image given the required gravity and the output sizes/ratio.
20 |
21 | Download
22 | ----
23 | ### Gradle dependency
24 | Add the following 'compile' statement in your **build.gradle** file (requires *jCenter* repository):
25 | ```groovy
26 | dependencies {
27 | //...your dependecies
28 | compile 'org.bonnyfone:brdcompat:0.1'
29 | }
30 | ```
31 |
32 | How To...
33 | ----
34 |
35 |
36 | ### ...upgrade from the standard API
37 |
38 | If you are already using the *BitmapRegionDecoder* API, just change the base class name from *BitmapRegionDecoder* to *BitmapRegionDecoderCompat*.
39 |
40 | ```java
41 | //BitmapRegionDecoder brd = BitmapRegionDecoder.newInstance(...);
42 | BitmapRegionDecoderCompat brd = BitmapRegionDecoderCompat.newInstance(...);
43 | ```
44 |
45 |
46 | ### ...simulate the fallback for API < 10 on device/emulator running API 10+
47 | **For debug purpose only** you can force the library to work in backward-compatibility mode by invoking the following static method (be sure to do this before the creation of any instance of BRDCompat):
48 | ```java
49 | BitmapRegionDecoderCompat.setForceFallbackImplementation(true);
50 | ```
51 |
52 | Example
53 | ----
54 | Check out the [demo] for a simple usage example.
55 |
56 | 
57 |
58 | Contribute
59 | ----
60 | Bug-reports, feedback and pull requests are always welcome ;)
61 |
62 | License
63 | ----
64 |
65 | ```
66 | Copyright 2015, Stefano Bonetta.
67 | Licensed under the Apache License, Version 2.0 (the "License");
68 | you may not use this file except in compliance with the License.
69 | You may obtain a copy of the License at
70 |
71 | http://www.apache.org/licenses/LICENSE-2.0
72 |
73 | Unless required by applicable law or agreed to in writing, software
74 | distributed under the License is distributed on an "AS IS" BASIS,
75 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76 | See the License for the specific language governing permissions and
77 | limitations under the License.
78 | ```
79 |
80 | [BitmapRegionDecoder]:http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
81 | [demo]:https://github.com/bonnyfone/brdcompat_demo
82 |
--------------------------------------------------------------------------------
/brdcompat/src/main/java/org/bonnyfone/brdcompat/BitmapRegionDecoderGinger.java:
--------------------------------------------------------------------------------
1 | package org.bonnyfone.brdcompat;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.graphics.Rect;
6 |
7 | import java.io.FileDescriptor;
8 | import java.io.IOException;
9 | import java.io.InputStream;
10 |
11 | /**
12 | * Backward-compatible implementation of the BitmapRegionDecoder API
13 | */
14 | class BitmapRegionDecoderGinger implements IBitmapRegionDecoder {
15 |
16 | private Object encodedImage;
17 | private BitmapFactory.Options bitmapOptions;
18 |
19 | private int offset = -1;
20 | private int length = -1;
21 | private int width = -1;
22 | private int height = -1;
23 | private boolean isRecycled;
24 | private boolean decoderIsSharable;
25 |
26 | private BitmapRegionDecoderGinger(){}
27 |
28 | private BitmapRegionDecoderGinger(String pathName, boolean isShareable) throws IOException {
29 | encodedImage = pathName;
30 | init(isShareable);
31 | BitmapFactory.decodeFile(pathName, bitmapOptions);
32 | checkInit();
33 | }
34 |
35 | private BitmapRegionDecoderGinger(FileDescriptor fd, boolean isShareable) throws IOException {
36 | encodedImage = fd;
37 | init(isShareable);
38 | BitmapFactory.decodeFileDescriptor(fd, null, bitmapOptions);
39 | checkInit();
40 | }
41 |
42 | private BitmapRegionDecoderGinger(byte[] data, int offset, int length, boolean isShareable) throws IOException {
43 | encodedImage = data;
44 | this.offset = offset;
45 | this.length = length;
46 | init(isShareable);
47 | BitmapFactory.decodeByteArray(data, offset, length, bitmapOptions);
48 | checkInit();
49 | }
50 |
51 | private BitmapRegionDecoderGinger(InputStream is, boolean isShareable) throws IOException {
52 | encodedImage = is;
53 | init(isShareable);
54 | BitmapFactory.decodeStream(is, null, bitmapOptions);
55 | checkInit();
56 | }
57 |
58 | private void init(boolean isInputSharable){
59 | decoderIsSharable = isInputSharable;
60 | bitmapOptions = new BitmapFactory.Options();
61 | bitmapOptions.inJustDecodeBounds = true;
62 | bitmapOptions.inInputShareable = isInputSharable;
63 | }
64 |
65 | private void checkInit() throws IOException {
66 | width = bitmapOptions.outWidth;
67 | height = bitmapOptions.outHeight;
68 |
69 | if(width == -1 || height == -1){
70 | throw new IOException("Unable to decode image bounds.");
71 | }
72 | }
73 |
74 | /**
75 | * Fallback decode-region method
76 | * @param encodedImage
77 | * @param area
78 | * @param opts
79 | * @return
80 | */
81 | private Bitmap fallbackDecode(Object encodedImage, Rect area, BitmapFactory.Options opts) {
82 | int rectFactor = 1;
83 | if(opts != null){
84 | rectFactor = opts.inSampleSize >= 1 ? opts.inSampleSize : 1;
85 | //TODO more cases here
86 | }
87 |
88 | return Bitmap.createBitmap(decodeObjectToBitmap(encodedImage, opts), area.left/rectFactor, area.top/rectFactor, area.right/rectFactor - area.left/rectFactor, area.bottom/rectFactor - area.top/rectFactor);
89 | }
90 |
91 | private Bitmap decodeObjectToBitmap(Object encodedImage, BitmapFactory.Options opts){
92 | if(encodedImage instanceof byte[]){
93 | return BitmapFactory.decodeByteArray((byte[])encodedImage, offset, length, opts);
94 | }
95 | else if(encodedImage instanceof InputStream){
96 | return BitmapFactory.decodeStream((InputStream)encodedImage, null, opts);
97 | }
98 | else if(encodedImage instanceof String){
99 | return BitmapFactory.decodeFile((String)encodedImage, opts);
100 | }
101 | else if(encodedImage instanceof FileDescriptor){
102 | return BitmapFactory.decodeFileDescriptor((FileDescriptor)encodedImage, null, opts);
103 | }
104 | else
105 | return null;
106 | }
107 |
108 | @Override
109 | public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) {
110 | return fallbackDecode(encodedImage, rect, options);
111 | }
112 |
113 | @Override
114 | public int getHeight() {
115 | return height;
116 | }
117 |
118 | @Override
119 | public int getWidth() {
120 | return width;
121 | }
122 |
123 | @Override
124 | public boolean isRecycled() {
125 | return isRecycled;
126 | }
127 |
128 | @Override
129 | public void recycle() {
130 | isRecycled = true;
131 | bitmapOptions = null;
132 | encodedImage = null;
133 | width = -1;
134 | height = -1;
135 | offset = -1;
136 | length = -1;
137 | }
138 |
139 | public static BitmapRegionDecoderGinger newInstance(String pathName, boolean isShareable) throws IOException {
140 | return new BitmapRegionDecoderGinger(pathName, isShareable);
141 | }
142 |
143 | public static BitmapRegionDecoderGinger newInstance(FileDescriptor fd, boolean isShareable) throws IOException {
144 | return new BitmapRegionDecoderGinger(fd, isShareable);
145 | }
146 |
147 | public static BitmapRegionDecoderGinger newInstance(byte[] data, int offset, int length, boolean isShareable) throws IOException {
148 | return new BitmapRegionDecoderGinger(data, offset, length, isShareable);
149 | }
150 |
151 | public static BitmapRegionDecoderGinger newInstance(InputStream is, boolean isShareable) throws IOException {
152 | return new BitmapRegionDecoderGinger(is, isShareable);
153 | }
154 |
155 | }
156 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/brdcompat/brdcompat.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/brdcompat/src/main/java/org/bonnyfone/brdcompat/BitmapRegionDecoderCompat.java:
--------------------------------------------------------------------------------
1 | package org.bonnyfone.brdcompat;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.graphics.Rect;
6 | import android.os.Build;
7 | import android.view.Gravity;
8 |
9 | import java.io.FileDescriptor;
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 |
13 | /**
14 | * BitmapRegionDecoder can be used to decode a rectangle region from an image.
15 | * BitmapRegionDecoder is particularly useful when an original image is large and you only need parts of the image.
16 | * To create a BitmapRegionDecoder, call newInstance(...).
17 | * Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly to get a decoded Bitmap of the specified region.
18 | */
19 | public class BitmapRegionDecoderCompat implements IBitmapRegionDecoder {
20 |
21 | private static boolean FORCE_FALLBACK_IMPLEMENTATION = false;
22 |
23 | /**
24 | * Private empty constructor
25 | */
26 | private BitmapRegionDecoderCompat(){}
27 |
28 | /**
29 | * Constructor which wraps internal implementation
30 | * @param impl
31 | */
32 | private BitmapRegionDecoderCompat(IBitmapRegionDecoder impl){
33 | this.impl = impl;
34 | }
35 |
36 | /**
37 | * Internal implementation object
38 | */
39 | private IBitmapRegionDecoder impl;
40 |
41 |
42 | /**
43 | * Check if we are running on Gingerbread or later
44 | * @return
45 | */
46 | private static boolean isAPI10(){
47 | return !FORCE_FALLBACK_IMPLEMENTATION && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1;
48 | }
49 |
50 | /**
51 | * Force to use the fallback implementation of BitmapRegionDecoder.
52 | * Should be used only for debug purposes
53 | * @param forceFallback
54 | */
55 | public static void setForceFallbackImplementation(boolean forceFallback){
56 | FORCE_FALLBACK_IMPLEMENTATION = forceFallback;
57 | }
58 |
59 |
60 | /**
61 | * Create a BitmapRegionDecoder from a file path.
62 | * Currently only the JPEG and PNG formats are supported.
63 | *
64 | * @param pathName complete path name for the file to be decoded.
65 | * @param isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to
66 | * the input. If this is false, then the BitmapRegionDecoder will explicitly
67 | * make a copy of the input data, and keep that. Even if sharing is allowed,
68 | * the implementation may still decide to make a deep copy of the input data.
69 | * If an image is progressively encoded, allowing sharing may degrade the decoding speed.
70 | * @return BitmapRegionDecoder, or null if the image data could not be decoded.
71 | * @throws java.io.IOException if the image format is not supported or can not be decoded.
72 | */
73 | public static BitmapRegionDecoderCompat newInstance(String pathName, boolean isShareable) throws IOException {
74 | return new BitmapRegionDecoderCompat(isAPI10() ? BitmapRegionDecoderNative.newInstance(pathName, isShareable) : BitmapRegionDecoderGinger.newInstance(pathName, isShareable));
75 | }
76 |
77 |
78 | /**
79 | * Create a BitmapRegionDecoder from an input stream. The stream's position will be where ever it was after the encoded data was read.
80 | * Currently only the JPEG and PNG formats are supported.
81 | *
82 | * @param is The input stream that holds the raw data to be decoded into a BitmapRegionDecoder.
83 | * Prior to KITKAT, if is.markSupported() returns true, is.mark(1024) would be called.
84 | * As of KITKAT, this is no longer the case.
85 | *
86 | * @param isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to
87 | * the input. If this is false, then the BitmapRegionDecoder will explicitly
88 | * make a copy of the input data, and keep that. Even if sharing is allowed,
89 | * the implementation may still decide to make a deep copy of the input data.
90 | * If an image is progressively encoded, allowing sharing may degrade the decoding speed.
91 | *
92 | * @return BitmapRegionDecoder, or null if the image data could not be decoded.
93 | *
94 | * @throws java.io.IOException if the image format is not supported or can not be decoded.
95 | */
96 | public static BitmapRegionDecoderCompat newInstance(InputStream is, boolean isShareable) throws IOException {
97 | return new BitmapRegionDecoderCompat(isAPI10() ? BitmapRegionDecoderNative.newInstance(is, isShareable) : BitmapRegionDecoderGinger.newInstance(is, isShareable));
98 | }
99 |
100 | /**
101 | * Create a BitmapRegionDecoder from the file descriptor.
102 | * The position within the descriptor will not be changed when this returns, so the descriptor can be used again as is.
103 | * Currently only the JPEG and PNG formats are supported.
104 | *
105 | * @param fd The file descriptor containing the data to decode
106 | * @param isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to
107 | * the input. If this is false, then the BitmapRegionDecoder will explicitly
108 | * make a copy of the input data, and keep that. Even if sharing is allowed,
109 | * the implementation may still decide to make a deep copy of the input data.
110 | * If an image is progressively encoded, allowing sharing may degrade the decoding speed.
111 | *
112 | * @return BitmapRegionDecoder, or null if the image data could not be decoded.
113 | *
114 | * @throws java.io.IOException if the image format is not supported or can not be decoded.
115 | *
116 | */
117 | public static BitmapRegionDecoderCompat newInstance(FileDescriptor fd, boolean isShareable) throws IOException {
118 | return new BitmapRegionDecoderCompat(isAPI10() ? BitmapRegionDecoderNative.newInstance(fd, isShareable) : BitmapRegionDecoderGinger.newInstance(fd, isShareable));
119 | }
120 |
121 |
122 | /**
123 | * Create a BitmapRegionDecoder from the specified byte array.
124 | * Currently only the JPEG and PNG formats are supported.
125 | *
126 | * @param data byte array of compressed image data.
127 | * @param offset offset into data for where the decoder should begin parsing.
128 | * @param length the number of bytes, beginning at offset, to parse
129 | * @param isShareable If this is true, then the BitmapRegionDecoder may keep a shallow reference to
130 | * the input. If this is false, then the BitmapRegionDecoder will explicitly
131 | * make a copy of the input data, and keep that. Even if sharing is allowed,
132 | * the implementation may still decide to make a deep copy of the input data.
133 | * If an image is progressively encoded, allowing sharing may degrade the decoding speed.
134 | *
135 | * @return BitmapRegionDecoder, or null if the image data could not be decoded.
136 | *
137 | * @throws java.io.IOException if the image format is not supported or can not be decoded.
138 | *
139 | */
140 | public static BitmapRegionDecoderCompat newInstance(byte[] data, int offset, int length, boolean isShareable) throws IOException {
141 | return new BitmapRegionDecoderCompat(isAPI10() ? BitmapRegionDecoderNative.newInstance(data, offset, length, isShareable) : BitmapRegionDecoderGinger.newInstance(data, offset, length, isShareable));
142 | }
143 |
144 | /**
145 | * Decodes a rectangle region in the image specified by rect.
146 | * @param rect The rectangle that specified the region to be decode.
147 | * @param options null-ok; Options that control downsampling. inPurgeable is not supported.
148 | *
Limited support on API < 11.
149 | * @return The decoded bitmap, or null if the image data could not be decoded.
150 | */
151 | @Override
152 | public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) {
153 | return impl.decodeRegion(rect, options);
154 | }
155 |
156 | /**
157 | * Extracts the "best" rectangle region based on the specified dimensions.
158 | * The "best" region means the biggest region of the original (downsampled) image, given the required gravity and output sizes/ratio.
159 | * You should use this method when you have a big source image that have to be fit/be partially displayed.
160 | * This method will use Gravity.CENTER as default gravity.
161 | * @param requiredWidth the required Width
162 | * @param requiredHeight the required Height
163 | * @return
164 | */
165 | public Bitmap decodeBestRegion(int requiredWidth, int requiredHeight){
166 | return decodeBestRegion(requiredWidth, requiredHeight, Gravity.CENTER);
167 | }
168 |
169 | /**
170 | * Extracts the "best" rectangle region based on the specified dimensions.
171 | * The "best" region means the biggest region of the original (downsampled) image, given the required gravity and output sizes/ratio.
172 | * You should use this method when you have a big source image that have to be fit/be partially displayed.
173 | * @param requiredWidth the required Width
174 | * @param requiredHeight the required Height
175 | * @param gravity combination of Gravity.LEFT, Gravity.RIGHT, Gravity.TOP,
176 | * Gravity.BOTTOM, Gravity.CENTER, Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL
177 | * @return
178 | */
179 | public Bitmap decodeBestRegion(int requiredWidth, int requiredHeight, int gravity) {
180 |
181 | boolean upscaling = false; //TODO handle upscaling as an option
182 | int realWidth = getWidth();
183 | int realHeight = getHeight();
184 |
185 | BitmapFactory.Options options = new BitmapFactory.Options();
186 |
187 |
188 | /* -- Check for upscale -- */
189 | /* ----------------------- */
190 |
191 | if( ! (requiredWidth <= realWidth && requiredHeight <= realHeight ) ){ //image is smaller than box
192 | float widthRatio = (float)requiredWidth / realWidth;
193 | float heightRatio = (float)requiredHeight / realHeight;
194 | float destinationRatio = Math.max(widthRatio, heightRatio);
195 |
196 | requiredWidth = Math.round(((float)requiredWidth/destinationRatio));
197 | requiredHeight = Math.round(((float)requiredHeight/destinationRatio));
198 | upscaling = true;
199 | }
200 |
201 |
202 | /* -- Downsampling -- */
203 | /* ------------------ */
204 |
205 | boolean limitReached = false;
206 | int basePow = 0;
207 | int actualScale;
208 | while( !limitReached ){
209 | actualScale = (int) Math.pow(2, basePow+1);
210 | if( requiredWidth <= realWidth/actualScale && requiredHeight <= realHeight/actualScale){
211 | basePow++;
212 | }else{
213 | limitReached = true;
214 | }
215 | }
216 | //basePow+=1; //extra compression!
217 | options.inSampleSize = (int) Math.pow(2, basePow);
218 |
219 |
220 | int fromTop = 0;
221 | int fromLeft = 0;
222 |
223 | //Default is Gravity.CENTER
224 | if(gravity == Gravity.CENTER){
225 | fromTop = (realHeight - requiredHeight * options.inSampleSize) / 2;
226 | fromLeft = (realWidth - requiredWidth * options.inSampleSize) / 2;
227 | }
228 | else{
229 | int horizontalGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
230 | int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
231 |
232 | if(horizontalGravity == Gravity.LEFT)
233 | fromLeft = 0;
234 | else if(horizontalGravity == Gravity.RIGHT)
235 | fromLeft = realWidth - requiredWidth * options.inSampleSize;
236 | else if(horizontalGravity == Gravity.CENTER_HORIZONTAL)
237 | fromLeft = (realWidth - requiredWidth * options.inSampleSize) / 2;
238 |
239 | if(verticalGravity == Gravity.TOP)
240 | fromTop = 0;
241 | else if(verticalGravity == Gravity.BOTTOM)
242 | fromTop = realHeight - requiredHeight * options.inSampleSize;
243 | else if(verticalGravity == Gravity.CENTER_VERTICAL)
244 | fromTop = (realHeight - requiredHeight * options.inSampleSize) / 2;
245 | }
246 |
247 | Rect area = new Rect(fromLeft, fromTop, fromLeft + requiredWidth * options.inSampleSize, fromTop + requiredHeight * options.inSampleSize);
248 | return impl.decodeRegion(area, options);
249 |
250 | }
251 |
252 | /**
253 | * Returns the original image's height
254 | * @return
255 | */
256 | @Override
257 | public int getHeight() {
258 | return impl.getHeight();
259 | }
260 |
261 | /**
262 | * Returns the original image's width
263 | * @return
264 | */
265 | @Override
266 | public int getWidth() {
267 | return impl.getWidth();
268 | }
269 |
270 | /**
271 | * Returns true if this region decoder has been recycled.
272 | * @return true if the region decoder has been recycled
273 | */
274 | @Override
275 | public boolean isRecycled() {
276 | return impl.isRecycled();
277 | }
278 |
279 | /**
280 | * Frees up the memory associated with this region decoder, and mark the region decoder as "dead",
281 | * meaning it will throw an exception if decodeRegion(), getWidth() or getHeight() is called.
282 | */
283 | @Override
284 | public void recycle() {
285 | impl.recycle();
286 | }
287 | }
288 |
--------------------------------------------------------------------------------