├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── vcs.xml
├── modules.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── styles.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── dimens.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── frag_bmp_merge_center.xml
│ │ │ │ ├── frag_bmp_merge_angle.xml
│ │ │ │ └── frag_bmp_merge_offset.xml
│ │ │ ├── drawable
│ │ │ │ └── round_rectangle_white.xml
│ │ │ └── menu
│ │ │ │ └── menu_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── cooltechworks
│ │ │ └── bitmapmerger
│ │ │ ├── MainActivity.java
│ │ │ ├── ui
│ │ │ └── fragments
│ │ │ │ ├── BitmapCenterFragment.java
│ │ │ │ ├── BitmapAngleFragment.java
│ │ │ │ └── BitmapOffsetFragment.java
│ │ │ └── tasks
│ │ │ ├── BitmapMergerTask.java
│ │ │ └── BitmapDecoderTask.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── cooltechworks
│ │ └── bitmapmerger
│ │ └── ApplicationTest.java
├── build.gradle
├── proguard-rules.pro
└── app.iml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── .travis.yml
├── gradle.properties
├── BitmapMerger.iml
├── LICENSE.txt
├── gradlew.bat
├── README.md
└── gradlew
/.idea/.name:
--------------------------------------------------------------------------------
1 | BitmapMerger
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharish/BitmapMerger/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | /captures
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharish/BitmapMerger/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharish/BitmapMerger/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharish/BitmapMerger/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharish/BitmapMerger/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
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.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #008000
5 | #e4ecfb
6 | #3f51b5
7 | #9c27b0
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/cooltechworks/bitmapmerger/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk:
3 | - oraclejdk7
4 | - oraclejdk8
5 | android:
6 | components:
7 | - build-tools-22.0.1
8 | - android-22
9 | - extra-android-support
10 | - extra-android-m2repository
11 | licenses:
12 | - '.+'
13 |
14 | before_install:
15 | - export JAVA7_HOME=/usr/lib/jvm/java-7-oracle
16 | - export JAVA8_HOME=/usr/lib/jvm/java-8-oracle
17 | - export JAVA_HOME=$JAVA7_HOME
18 | script:
19 | - ./gradlew clean assemble || ./gradlew clean assemble
20 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Bitmap Merger Demo
3 |
4 | Merging with angle away from axis
5 | Merging at center
6 |
7 | Scale factor: %.2f
8 | Angle : %d
9 | From Left: %d px
10 | From Top: %d px
11 | Merging with offsets
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 22
5 | buildToolsVersion "22.0.1"
6 |
7 | defaultConfig {
8 | applicationId "com.cooltechworks.bitmapmerger"
9 | minSdkVersion 9
10 | targetSdkVersion 22
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | compile 'com.android.support:appcompat-v7:22.1.1'
25 | }
26 |
--------------------------------------------------------------------------------
/app/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 /Users/sharish/Library/Android/sdk/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 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/round_rectangle_white.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
11 |
16 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
22 |
--------------------------------------------------------------------------------
/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
--------------------------------------------------------------------------------
/BitmapMerger.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 cooltechworks
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 | 5dp
7 | 10dp
8 | 20dp
9 | 25dp
10 | 30dp
11 |
12 | -5dp
13 | -10dp
14 | -20dp
15 | -25dp
16 | -30dp
17 |
18 | 18sp
19 | 20sp
20 | 24sp
21 | 28sp
22 | 32sp
23 |
24 |
25 |
26 |
27 | 8dp
28 | 5dp
29 | 3px
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger;
2 |
3 | import android.os.Bundle;
4 | import android.support.v4.app.Fragment;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.Menu;
7 | import android.view.MenuItem;
8 |
9 | import com.cooltechworks.bitmapmerger.tasks.BitmapMergerTask;
10 | import com.cooltechworks.bitmapmerger.ui.fragments.BitmapAngleFragment;
11 | import com.cooltechworks.bitmapmerger.ui.fragments.BitmapCenterFragment;
12 | import com.cooltechworks.bitmapmerger.ui.fragments.BitmapOffsetFragment;
13 |
14 |
15 | public class MainActivity extends AppCompatActivity {
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_main);
21 | showFragment(BitmapMergerTask.BitmapMergeOptions.MERGE_AT_CENTER);
22 | }
23 |
24 | @Override
25 | public boolean onCreateOptionsMenu(Menu menu) {
26 | // Inflate the menu; this adds items to the action bar if it is present.
27 | getMenuInflater().inflate(R.menu.menu_main, menu);
28 | return true;
29 | }
30 |
31 | @Override
32 | public boolean onOptionsItemSelected(MenuItem item) {
33 | // Handle action bar item clicks here. The action bar will
34 | // automatically handle clicks on the Home/Up button, so long
35 | // as you specify a parent activity in AndroidManifest.xml.
36 | int id = item.getItemId();
37 |
38 | switch (id) {
39 | case R.id.action_angle:
40 | showFragment(BitmapMergerTask.BitmapMergeOptions.MERGE_AT_ANGLE_OFF);
41 | break;
42 | case R.id.action_offset:
43 | showFragment(BitmapMergerTask.BitmapMergeOptions.MERGE_FROM_TOP_LEFT);
44 | break;
45 | case R.id.action_center:
46 | showFragment(BitmapMergerTask.BitmapMergeOptions.MERGE_AT_CENTER);
47 | }
48 |
49 | return super.onOptionsItemSelected(item);
50 | }
51 |
52 | private void showFragment(BitmapMergerTask.BitmapMergeOptions mergeOptions) {
53 |
54 | Fragment fragment;
55 |
56 | switch (mergeOptions) {
57 | case MERGE_AT_ANGLE_OFF:
58 | fragment = new BitmapAngleFragment();
59 | break;
60 | case MERGE_FROM_TOP_LEFT:
61 | fragment = new BitmapOffsetFragment();
62 | break;
63 | default:
64 | fragment = new BitmapCenterFragment();
65 | }
66 |
67 | getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
68 |
69 |
70 | }
71 |
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/frag_bmp_merge_center.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
12 |
13 |
22 |
23 |
24 |
29 |
30 |
39 |
40 |
41 |
51 |
52 |
53 |
54 |
62 |
63 |
64 |
74 |
75 |
79 |
80 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/frag_bmp_merge_angle.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
12 |
13 |
22 |
23 |
24 |
29 |
30 |
39 |
40 |
41 |
51 |
52 |
53 |
54 |
59 |
60 |
68 |
69 |
79 |
80 |
81 |
82 |
90 |
91 |
92 |
102 |
103 |
107 |
108 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | [](https://android-arsenal.com/details/1/2085) [](https://travis-ci.org/cooltechworks/BitmapMerger/)
3 |
4 |
5 | # BitmapMerger
6 | Bitmap Merger is a simple project help you to merge two bitmaps without memory exceptions. The bitmaps are processed in background threads thereby taking the
7 | load away from UI thread. Along with merge, it also contains the image decoder for decoding images from resources/disk and are sampled
8 | to prevent OutOfMemoryError.
9 |
10 | Examples of Bitmap Merger with this project
11 |
12 | Merging at angle away | Merging at center | Merging with offsets
13 | ------------ | ------------- | -------------
14 |  |  | 
15 | Moon moving around the earth for various angles | Stamp at center of the document being scaled | Balloon flying in a beach with various offset values
16 |
17 |
18 | ###Usage :
19 |
20 | ####Merging at angle away
21 |
22 | ```java
23 | int angle = 90; // your angle here
24 | float scale = 0.5f; // scaling option for merging the image
25 | Bitmap baseBitmap; // your base bitmap here
26 | Bitmap mergeBitmap; // your merging bitmap here
27 | ImageView imgView; // your image view for displaying the merged bitmaps.
28 | BitmapMergerTask task = new BitmapMergerTask();
29 | task.setBaseBitmap(baseBitmap)
30 | .setMergeBitmap(mergeBitmap)
31 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
32 | @Override
33 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
34 | if(imgView != null) {
35 | imgView.setImageBitmap(mergedBitmap);
36 | }
37 | }
38 | })
39 | .setScale(scale)
40 | .setAngle(angle)
41 | .merge();
42 |
43 | ```
44 |
45 | ####Merging at center
46 |
47 | ```java
48 | float scale = 0.5f; // scaling option for merging the image
49 | Bitmap baseBitmap; // your base bitmap here
50 | Bitmap mergeBitmap; // your merging bitmap here
51 | ImageView imgView; // your image view for displaying the merged bitmaps.
52 | BitmapMergerTask task = new BitmapMergerTask();
53 | task.setBaseBitmap(baseBitmap)
54 | .setMergeBitmap(mergeBitmap)
55 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
56 | @Override
57 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
58 | if(imgView != null) {
59 | imgView.setImageBitmap(mergedBitmap);
60 | }
61 | }
62 | })
63 | .setScale(scale)
64 | .merge();
65 |
66 | ```
67 |
68 | ####Merging with offsets from top left
69 |
70 | ```java
71 | int leftOffset = 0; // your left offset in pixels
72 | int topOffset = 0; // your top offset in pixels
73 | float scale = 0.5f; // scaling option for merging the image
74 | Bitmap baseBitmap; // your base bitmap here
75 | Bitmap mergeBitmap; // your merging bitmap here
76 | ImageView imgView; // your image view for displaying the merged bitmaps.
77 | BitmapMergerTask task = new BitmapMergerTask();
78 | task.setBaseBitmap(baseBitmap)
79 | .setMergeBitmap(mergeBitmap)
80 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
81 | @Override
82 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
83 | if(imgView != null) {
84 | imgView.setImageBitmap(mergedBitmap);
85 | }
86 | }
87 | })
88 | .setScale(scale)
89 | .setOffsets(leftOffset,topOffset)
90 | .merge();
91 |
92 | ```
93 |
94 | Developed By
95 | ============
96 |
97 | * Harish Sridharan -
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/frag_bmp_merge_offset.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
11 |
12 |
13 |
22 |
23 |
28 |
29 |
30 |
35 |
36 |
45 |
46 |
47 |
56 |
57 |
58 |
59 |
64 |
65 |
74 |
75 |
76 |
86 |
87 |
88 |
89 |
90 |
95 |
96 |
103 |
104 |
114 |
115 |
116 |
117 |
125 |
126 |
127 |
137 |
138 |
142 |
143 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
--------------------------------------------------------------------------------
/app/app.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 |
95 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/ui/fragments/BitmapCenterFragment.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger.ui.fragments;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.graphics.Bitmap;
6 | import android.net.Uri;
7 | import android.os.Bundle;
8 | import android.support.v4.app.Fragment;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.ImageView;
13 | import android.widget.SeekBar;
14 | import android.widget.TextView;
15 |
16 | import com.cooltechworks.bitmapmerger.R;
17 | import com.cooltechworks.bitmapmerger.tasks.BitmapDecoderTask;
18 | import com.cooltechworks.bitmapmerger.tasks.BitmapMergerTask;
19 |
20 |
21 | public class BitmapCenterFragment extends Fragment {
22 |
23 |
24 | private static final int SELECT_PHOTO_1 = 1;
25 | private static final int SELECT_PHOTO_2 = 2;
26 |
27 | private Bitmap mBaseBitmap, mMergeBitmap;
28 | private float mScale = 0.5f;
29 |
30 | private BitmapMergerTask mBitmapMergerTask;
31 |
32 | private View mRootView;
33 |
34 |
35 | @Override
36 | public View onCreateView(LayoutInflater inflater, ViewGroup root,Bundle savedInstanceState) {
37 |
38 |
39 | mRootView = inflater.inflate(R.layout.frag_bmp_merge_center,root,false);
40 |
41 | View.OnClickListener listener = new View.OnClickListener() {
42 | @Override
43 | public void onClick(View v) {
44 |
45 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
46 | photoPickerIntent.setType("image/*");
47 |
48 | switch (v.getId()) {
49 | case R.id.base_image_label:
50 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_1);
51 | break;
52 | case R.id.merge_image_label:
53 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_2);
54 | break;
55 |
56 | }
57 | }
58 | };
59 |
60 | View baseLabelView = mRootView.findViewById(R.id.base_image_label);
61 | View mergeLabelView = mRootView.findViewById(R.id.merge_image_label);
62 |
63 | baseLabelView.setOnClickListener(listener);
64 | mergeLabelView.setOnClickListener(listener);
65 |
66 | SeekBar seekBar = (SeekBar) mRootView.findViewById(R.id.scale_size);
67 | seekBar.setProgress((int) (mScale * 100));
68 |
69 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
70 |
71 |
72 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
73 | @Override
74 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
75 | mScale = progress / 100f;
76 |
77 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
78 | refresh();
79 |
80 | }
81 |
82 | @Override
83 | public void onStartTrackingTouch(SeekBar seekBar) {
84 |
85 | }
86 |
87 | @Override
88 | public void onStopTrackingTouch(SeekBar seekBar) {
89 |
90 | }
91 | });
92 |
93 | refresh();
94 |
95 | return mRootView;
96 |
97 | }
98 |
99 | public void refresh() {
100 |
101 | final ImageView imgView = (ImageView) mRootView.findViewById(R.id.image_holder);
102 |
103 | TextView baseLabelTextView = (TextView) mRootView.findViewById(R.id.base_image_label);
104 | TextView mergeLabelTextView = (TextView) mRootView.findViewById(R.id.merge_image_label);
105 |
106 |
107 | if (mMergeBitmap != null && mBaseBitmap != null) {
108 |
109 | //// If you're working with larger bitmaps and continuously changing the scale value, you might notice the lag between slider change
110 | //// and the image position. To get rid of that, uncomment the following codes.
111 | // if (mBitmapMergerTask != null && mBitmapMergerTask.getStatus() == BitmapMergerTask.Status.RUNNING) {
112 | // mBitmapMergerTask.cancel(true);
113 | // }
114 |
115 |
116 | mBitmapMergerTask = new BitmapMergerTask();
117 | mBitmapMergerTask.setBaseBitmap(mBaseBitmap)
118 | .setMergeBitmap(mMergeBitmap)
119 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
120 | @Override
121 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
122 | imgView.setImageBitmap(mergedBitmap);
123 | }
124 | })
125 | .setScale(mScale)
126 | .merge();
127 |
128 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
129 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
130 |
131 |
132 | }
133 | else {
134 |
135 | // if the first bitmap was null, highlight base bitmap text view, otherwise highlight the merge bitmap text view
136 | if(mBaseBitmap != null) {
137 | imgView.setImageBitmap(mBaseBitmap);
138 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
139 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
140 | }
141 | else {
142 | baseLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
143 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
144 | }
145 |
146 | }
147 |
148 | }
149 |
150 | @Override
151 | public void onActivityResult(final int requestCode, int resultCode, Intent imageReturnedIntent) {
152 | super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
153 |
154 | switch (requestCode) {
155 | case SELECT_PHOTO_1:
156 | case SELECT_PHOTO_2:
157 | if (resultCode == Activity.RESULT_OK) {
158 | final Uri imageUri = imageReturnedIntent.getData();
159 |
160 | int dimens[] = getDimens(R.id.image_holder);
161 |
162 | new BitmapDecoderTask()
163 | .setDecodingImageReference(imageUri, getActivity().getContentResolver())
164 | .setRequiredWidth(dimens[0])
165 | .setRequiredHeight(dimens[1])
166 | .setListener(new BitmapDecoderTask.OnDecodeListener() {
167 | @Override
168 | public void onDecode(BitmapDecoderTask task, Bitmap bitmap) {
169 |
170 | if (requestCode == SELECT_PHOTO_1) {
171 |
172 | mBaseBitmap = bitmap;
173 |
174 | } else {
175 |
176 | mMergeBitmap = bitmap;
177 |
178 | }
179 | refresh();
180 | }
181 | })
182 | .decode();
183 |
184 | }
185 | }
186 | }
187 |
188 |
189 | public int[] getDimens(int resId) {
190 | int width = mRootView.findViewById(resId).getMeasuredWidth();
191 | int height = mRootView.findViewById(resId).getMeasuredHeight();
192 |
193 | return new int[]{width, height};
194 |
195 | }
196 |
197 | }
198 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/tasks/BitmapMergerTask.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger.tasks;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.os.AsyncTask;
7 |
8 | /**
9 | * Created by Harish Sridharan on 29/06/15.
10 | */
11 |
12 |
13 | /**
14 | * BitmapMergerTask is background asynchronous task merging two bitmaps by placing one bitmap (called mergeBitmap)
15 | * over another (called baseBitmap).
16 | *
17 | * Merging can be done in three ways,
18 | *
19 | * 1. Merging at the center (along with resize options)
20 | * 2. Merging at an angle away from the axis of the image at center point
21 | * 3. Merging the bitmaps with offsets from top left corner.
22 | */
23 | public class BitmapMergerTask extends AsyncTask {
24 |
25 | class BitmapMergerTaskException extends RuntimeException {
26 | BitmapMergerTaskException(String msg) {
27 | super(msg);
28 | }
29 | }
30 |
31 | @Override
32 | protected Bitmap doInBackground(Void... params) {
33 |
34 | if(mBaseBitmap == null) {
35 | throw new BitmapMergerTaskException("Base bitmap not set");
36 | }
37 |
38 | if(mMergeBitmap == null) {
39 | throw new BitmapMergerTaskException("Merge bitmap not set");
40 | }
41 |
42 | switch (mMergeOptions) {
43 |
44 | case MERGE_AT_ANGLE_OFF:
45 | return mergeAtAngle();
46 | case MERGE_FROM_TOP_LEFT:
47 | return mergeFromTopLeft();
48 | default:
49 | return mergeAtCenter();
50 | }
51 | }
52 |
53 | public void onPostExecute(Bitmap bitmap) {
54 | if(mMergeListener != null) {
55 | mMergeListener.onMerge(this,bitmap);
56 | }
57 | }
58 |
59 | public interface OnMergeListener {
60 | void onMerge(BitmapMergerTask task, Bitmap mergedBitmap);
61 | }
62 |
63 | public enum BitmapMergeOptions {
64 | MERGE_AT_CENTER,
65 | MERGE_AT_ANGLE_OFF,
66 | MERGE_FROM_TOP_LEFT,
67 | }
68 |
69 | private Bitmap mBaseBitmap;
70 | private Bitmap mMergeBitmap;
71 | private float mScale = 0.5f;
72 | private int mAngle = 0;
73 | private int mTopOffset = 0;
74 | private int mLeftOffset = 0;
75 | private BitmapMergeOptions mMergeOptions = BitmapMergeOptions.MERGE_AT_CENTER;
76 | private OnMergeListener mMergeListener;
77 |
78 |
79 | /**
80 | * Sets the scaling of the merge image.
81 | * @param scale - float value from 0.0 to 1.0 represents the scale.
82 | * @return the related BitmapMergerTask
83 | */
84 | public BitmapMergerTask setScale(float scale) {
85 | this.mScale = scale;
86 | return this;
87 | }
88 |
89 | /**
90 | * Sets the base bitmap image.
91 | * @param mBaseBitmap - base bitmap
92 | * @return the related BitmapMergerTask
93 | */
94 | public BitmapMergerTask setBaseBitmap(Bitmap mBaseBitmap) {
95 | this.mBaseBitmap = mBaseBitmap;
96 | return this;
97 | }
98 |
99 | /**
100 | * Sets the merge bitmap image.
101 | * @param mMergeBitmap - merging bitmap image.
102 | * @return the related BitmapMergerTask
103 | */
104 | public BitmapMergerTask setMergeBitmap(Bitmap mMergeBitmap) {
105 | this.mMergeBitmap = mMergeBitmap;
106 | return this;
107 | }
108 |
109 | /**
110 | * Sets the merging offset points. Invoking this method will mark the merging mechanism to merge the mergeBitmap image to the base bitmap image
111 | * from the top left portion as specified by the params leftOffset and topOffset
112 | *
113 | * @param leftOffset pixel offsets from left
114 | * @param topOffset pixel offsets from top
115 | * @return the related BitmapMergerTask
116 | */
117 | public BitmapMergerTask setOffsets(int leftOffset, int topOffset) {
118 | this.mTopOffset = topOffset;
119 | this.mLeftOffset = leftOffset;
120 | this.mMergeOptions = BitmapMergeOptions.MERGE_FROM_TOP_LEFT;
121 | return this;
122 | }
123 |
124 | /**
125 | * Sets the merging offset angle. Invoking this method will mark the merging mechanism to merge the mergeBitmap image to the base bitmap image
126 | * at the angle off from the base line from center to mid point on the right edge of the baseBitmap.
127 | * @param angle - angle off from the base line.
128 | * @return the related BitmapMergerTask
129 | */
130 | public BitmapMergerTask setAngle(int angle) {
131 | this.mAngle = angle;
132 | this.mMergeOptions = BitmapMergeOptions.MERGE_AT_ANGLE_OFF;
133 | return this;
134 | }
135 |
136 | /**
137 | * Sets the listener for merge complete.
138 | * @param listener for merge completeness.
139 | * @return the related BitmapMergerTask
140 | */
141 | public BitmapMergerTask setMergeListener(OnMergeListener listener) {
142 | this.mMergeListener = listener;
143 | return this;
144 | }
145 |
146 | /**
147 | * Initiates the merging task in the background
148 | */
149 | public void merge() {
150 | super.execute((Void[])null);
151 | }
152 |
153 | private Bitmap mergeAtAngle() {
154 |
155 | if (mScale > 0) {
156 |
157 | int radius = mBaseBitmap.getWidth() / 4;
158 | int centerX = mBaseBitmap.getWidth() / 2;
159 | int centerY = mBaseBitmap.getHeight() / 2;
160 |
161 | double radians = Math.toRadians(mAngle);
162 |
163 | int x = (int) (radius * Math.cos(radians) + centerX);
164 | int y = (int) (radius * Math.sin(radians) + centerY);
165 |
166 |
167 | Bitmap overlayScaled = Bitmap.createScaledBitmap(mMergeBitmap, (int) (mBaseBitmap.getWidth() * mScale), (int) (mBaseBitmap.getHeight() * mScale), true);
168 |
169 | Bitmap workingBitmap = Bitmap.createBitmap(mBaseBitmap);
170 | Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
171 |
172 | Canvas canvas = new Canvas(mutableBitmap);
173 |
174 | x -= (overlayScaled.getWidth() / 2);
175 | y -= (overlayScaled.getHeight() / 2);
176 |
177 |
178 | canvas.drawBitmap(overlayScaled, x, y, new Paint());
179 |
180 | return mutableBitmap;
181 | } else {
182 | return mBaseBitmap;
183 | }
184 |
185 | }
186 |
187 | private Bitmap mergeFromTopLeft() {
188 |
189 | return mergeBitmaps(mBaseBitmap,mMergeBitmap,mScale, mLeftOffset, mTopOffset);
190 | }
191 |
192 |
193 | private Bitmap mergeAtCenter() {
194 |
195 | if (mScale > 0) {
196 | Bitmap overlayScaled = Bitmap.createScaledBitmap(mMergeBitmap, (int) (mBaseBitmap.getWidth() * mScale), (int) (mBaseBitmap.getHeight() * mScale), true);
197 |
198 | int lockWidth = overlayScaled.getWidth();
199 | int lockHeight = overlayScaled.getHeight();
200 |
201 | int totalWidth = mBaseBitmap.getWidth();
202 | int totalHeight = mBaseBitmap.getHeight();
203 |
204 | int startX = (totalWidth / 2) - (lockWidth / 2);
205 | int startY = (totalHeight / 2) - (lockHeight / 2);
206 |
207 | return mergeBitmaps(mBaseBitmap, mMergeBitmap, mScale, startX, startY);
208 | } else {
209 | return mBaseBitmap;
210 | }
211 |
212 | }
213 |
214 | private static Bitmap mergeBitmaps(Bitmap baseBitmap, Bitmap overlayBitmap, float scale, int leftOffset, int topOffset) {
215 |
216 | if (scale > 0) {
217 | Bitmap overlayScaled = Bitmap.createScaledBitmap(overlayBitmap, (int) (baseBitmap.getWidth() * scale), (int) (baseBitmap.getHeight() * scale), true);
218 |
219 | Bitmap workingBitmap = Bitmap.createBitmap(baseBitmap);
220 | Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
221 |
222 |
223 | Canvas canvas = new Canvas(mutableBitmap);
224 |
225 | canvas.drawBitmap(overlayScaled, leftOffset, topOffset, new Paint());
226 |
227 | return mutableBitmap;
228 | } else {
229 | return baseBitmap;
230 | }
231 |
232 | }
233 |
234 | }
235 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/ui/fragments/BitmapAngleFragment.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger.ui.fragments;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.graphics.Bitmap;
6 | import android.net.Uri;
7 | import android.os.Bundle;
8 | import android.support.v4.app.Fragment;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.ImageView;
13 | import android.widget.SeekBar;
14 | import android.widget.TextView;
15 |
16 | import com.cooltechworks.bitmapmerger.R;
17 | import com.cooltechworks.bitmapmerger.tasks.BitmapDecoderTask;
18 | import com.cooltechworks.bitmapmerger.tasks.BitmapMergerTask;
19 |
20 |
21 | public class BitmapAngleFragment extends Fragment {
22 |
23 |
24 | private static final int SELECT_PHOTO_1 = 1;
25 | private static final int SELECT_PHOTO_2 = 2;
26 |
27 | private Bitmap mMergeBitmap, mBaseBitmap;
28 | private int mAngle = 0;
29 | private float mScale = 0.5f;
30 |
31 | private BitmapMergerTask mBitmapMergerTask;
32 |
33 | private View mRootView;
34 |
35 |
36 | @Override
37 | public View onCreateView(LayoutInflater inflater, ViewGroup root,Bundle savedInstanceState) {
38 |
39 |
40 | mRootView = inflater.inflate(R.layout.frag_bmp_merge_angle,root,false);
41 |
42 | View.OnClickListener listener = new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 |
46 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
47 | photoPickerIntent.setType("image/*");
48 |
49 | switch (v.getId()) {
50 | case R.id.base_image_label:
51 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_1);
52 | break;
53 | case R.id.merge_image_label:
54 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_2);
55 | break;
56 |
57 | }
58 | }
59 | };
60 |
61 | View baseLabelView = mRootView.findViewById(R.id.base_image_label);
62 | View mergeLabelView = mRootView.findViewById(R.id.merge_image_label);
63 |
64 | baseLabelView.setOnClickListener(listener);
65 | mergeLabelView.setOnClickListener(listener);
66 |
67 | SeekBar angleSeekbar = (SeekBar) mRootView.findViewById(R.id.angle);
68 | angleSeekbar.setProgress(mAngle);
69 |
70 | SeekBar scaleSeekbar = (SeekBar) mRootView.findViewById(R.id.scale_size);
71 | scaleSeekbar.setProgress((int) (mScale * 100));
72 |
73 | ((TextView) mRootView.findViewById(R.id.angle_label)).setText(getString(R.string.angle_factor, mAngle));
74 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
75 |
76 |
77 | angleSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
78 | @Override
79 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
80 |
81 | mAngle = progress;
82 |
83 | ((TextView) mRootView.findViewById(R.id.angle_label)).setText(getString(R.string.angle_factor, mAngle));
84 | refresh();
85 |
86 | }
87 |
88 | @Override
89 | public void onStartTrackingTouch(SeekBar seekBar) {
90 |
91 | }
92 |
93 | @Override
94 | public void onStopTrackingTouch(SeekBar seekBar) {
95 |
96 | }
97 | });
98 |
99 | scaleSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
100 | @Override
101 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
102 |
103 | mScale = progress / 100f;
104 |
105 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
106 | refresh();
107 |
108 | }
109 |
110 | @Override
111 | public void onStartTrackingTouch(SeekBar seekBar) {
112 |
113 | }
114 |
115 | @Override
116 | public void onStopTrackingTouch(SeekBar seekBar) {
117 |
118 | }
119 | });
120 |
121 | refresh();
122 |
123 | return mRootView;
124 |
125 |
126 | }
127 |
128 | public void refresh() {
129 |
130 | final ImageView imgView = (ImageView) mRootView.findViewById(R.id.image_holder);
131 |
132 | TextView baseLabelTextView = (TextView) mRootView.findViewById(R.id.base_image_label);
133 | TextView mergeLabelTextView = (TextView) mRootView.findViewById(R.id.merge_image_label);
134 |
135 |
136 | if (mMergeBitmap != null && mBaseBitmap != null) {
137 |
138 | // If you're working with larger bitmaps and continuously changing the scale value or angle value, you might notice the lag between slider change
139 | // and the image position. To get rid of that, uncomment the following codes.
140 | // if (mBitmapMergerTask != null && mBitmapMergerTask.getStatus() == BitmapMergerTask.Status.RUNNING) {
141 | // mBitmapMergerTask.cancel(true);
142 | // }
143 |
144 |
145 | mBitmapMergerTask = new BitmapMergerTask();
146 | mBitmapMergerTask.setBaseBitmap(mBaseBitmap)
147 | .setMergeBitmap(mMergeBitmap)
148 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
149 | @Override
150 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
151 | imgView.setImageBitmap(mergedBitmap);
152 | }
153 | })
154 | .setScale(mScale)
155 | .setAngle(mAngle)
156 | .merge();
157 |
158 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
159 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
160 |
161 |
162 | }
163 | else {
164 |
165 |
166 | if(mBaseBitmap != null) {
167 | imgView.setImageBitmap(mBaseBitmap);
168 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
169 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
170 | }
171 | else {
172 | baseLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
173 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
174 | }
175 |
176 | }
177 |
178 | }
179 |
180 | @Override
181 | public void onActivityResult(final int requestCode, int resultCode, Intent imageReturnedIntent) {
182 | super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
183 |
184 | switch (requestCode) {
185 | case SELECT_PHOTO_1:
186 | case SELECT_PHOTO_2:
187 | if (resultCode == Activity.RESULT_OK) {
188 | final Uri imageUri = imageReturnedIntent.getData();
189 |
190 | int dimens[] = getDimens(R.id.image_holder);
191 |
192 | new BitmapDecoderTask()
193 | .setDecodingImageReference(imageUri, getActivity().getContentResolver())
194 | .setRequiredWidth(dimens[0])
195 | .setRequiredHeight(dimens[1])
196 | .setListener(new BitmapDecoderTask.OnDecodeListener() {
197 | @Override
198 | public void onDecode(BitmapDecoderTask task, Bitmap bitmap) {
199 |
200 | if (requestCode == SELECT_PHOTO_1) {
201 |
202 | mBaseBitmap = bitmap;
203 |
204 | } else {
205 |
206 | mMergeBitmap = bitmap;
207 |
208 | }
209 | refresh();
210 | }
211 | })
212 | .decode();
213 |
214 | }
215 | }
216 | }
217 |
218 | public int[] getDimens(int resId) {
219 | int width = mRootView.findViewById(resId).getMeasuredWidth();
220 | int height = mRootView.findViewById(resId).getMeasuredHeight();
221 |
222 | return new int[]{width, height};
223 |
224 | }
225 |
226 | }
227 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/tasks/BitmapDecoderTask.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger.tasks;
2 |
3 | import android.content.ContentResolver;
4 | import android.content.res.Resources;
5 | import android.graphics.Bitmap;
6 | import android.graphics.BitmapFactory;
7 | import android.net.Uri;
8 | import android.os.AsyncTask;
9 |
10 | import java.io.FileNotFoundException;
11 | import java.io.InputStream;
12 |
13 | /**
14 | * Created by Harish Sridharan on 29/06/15.
15 | */
16 |
17 | /**
18 | * BitmapDecoderTask is a background asynchronous task that decodes the image (particularly larger images) from Uri or from resources. The decoder decodes the image in background thread and samples it to prevent {@link OutOfMemoryError} when loading larger bitmaps.
19 | *
20 | * The decoding can be done for
21 | *
22 | * 1. Images present in resources
23 | * 2. Images present in disk which are referenced with Uri.
24 | */
25 | public class BitmapDecoderTask extends AsyncTask {
26 |
27 | class BitmapDecodeException extends RuntimeException {
28 | public BitmapDecodeException(String msg) { super(msg);
29 | }
30 | }
31 |
32 |
33 | enum DecodingOptions {
34 | DECODE_FROM_RESOURCE,
35 | DECODE_FROM_DISK,
36 | DECODING_NOT_SPECIFIED;
37 | }
38 |
39 | public interface OnDecodeListener {
40 |
41 | void onDecode(BitmapDecoderTask task, Bitmap output);
42 | }
43 |
44 | private int mReqHeight,mReqWidth;
45 | private OnDecodeListener mListener;
46 | private Uri mContentProviderUri; // if getting bitmap from external storage or from disk
47 | private ContentResolver mResolver;
48 | private int mDrawableId; // if getting bitmap from drawable.
49 | private Resources mResources;
50 | private DecodingOptions mDecodingOptions = DecodingOptions.DECODING_NOT_SPECIFIED;
51 |
52 |
53 | /**
54 | * Set the listener for listening to the background task of decoding and sampling the image.
55 | * @param listener - listener for decode complete.
56 | * @return the related BitmapDecoderTask
57 | */
58 | public BitmapDecoderTask setListener(OnDecodeListener listener) {
59 | this.mListener = listener;
60 | return this;
61 | }
62 |
63 | /**
64 | * Sets the reference image which has to be decoded.
65 | * @param drawableId - id of the drawable.
66 | * @param resources - resources reference for getting the drawable.
67 | * @return related BitmapDecoderTask.
68 | */
69 | public BitmapDecoderTask setDecodingImageReference(int drawableId, Resources resources) {
70 | this.mDrawableId = drawableId;
71 | this.mResources = resources;
72 | this.mDecodingOptions = DecodingOptions.DECODE_FROM_RESOURCE;
73 | return this;
74 | }
75 |
76 |
77 | /**
78 | * Sets the reference image which has to be decoded.
79 | * @param contentProviderUri - Uri which refers to a path containing the image.
80 | * @param mResolver - the resolver required to get the image from the Uri
81 | * @return the related BitmapDecoderTask
82 | */
83 | public BitmapDecoderTask setDecodingImageReference(Uri contentProviderUri, ContentResolver mResolver) {
84 | this.mResolver = mResolver;
85 | this.mContentProviderUri = contentProviderUri;
86 | this.mDecodingOptions = DecodingOptions.DECODE_FROM_DISK;
87 | return this;
88 | }
89 |
90 | /**
91 | * Sets the required width for decoding the image.
92 | *
93 | * This is highly necessary for sampling the image. As referred in https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
94 | * its not worth loading a full image as large image will always be scaled down before being displayed, we have this method for sampling for reducing
95 | * the size before being set for efficient processing and escape from java.lang.OutOfMemoryError.
96 | *
97 | *
98 | * @param width - width of the reference view for sampling. Calculate the width of the reference view where the image has to be displayed.
99 | * @return the related BitmapDecoderTask
100 | */
101 | public BitmapDecoderTask setRequiredWidth(int width) {
102 | this.mReqWidth = width;
103 | return this;
104 | }
105 |
106 | /**
107 | * Sets the required height for decoding the image.
108 | *
109 | * This is highly necessary for sampling the image. As referred in https://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
110 | * its not worth loading a full image as large image will always be scaled down before being displayed, we have this method for sampling for reducing
111 | * the size before being set for efficient processing and escape from java.lang.OutOfMemoryError.
112 | *
113 | *
114 | * @param height - height of the reference view for sampling. Calculate the height of the reference view where the image has to be displayed.
115 | * @return the related BitmapDecoderTask
116 | */
117 | public BitmapDecoderTask setRequiredHeight(int height) {
118 | this.mReqHeight = height;
119 | return this;
120 | }
121 |
122 | @Override
123 | protected Bitmap doInBackground(Void... params) {
124 | switch (mDecodingOptions) {
125 | case DECODE_FROM_DISK:
126 | return decodeSampledBitmapFromDisk();
127 | case DECODE_FROM_RESOURCE:
128 | return decodeSampledBitmapFromResource();
129 | default:
130 | throw new BitmapDecodeException("Did not specify the image reference with setDecodingImageReference()");
131 | }
132 | }
133 |
134 | @Override
135 | public void onPostExecute(Bitmap bitmap) {
136 | if(mListener != null) {
137 | mListener.onDecode(this, bitmap);
138 | }
139 | }
140 |
141 | /**
142 | * Initiates the background process to decoding and sampling the image.
143 | */
144 | public void decode() {
145 | super.execute((Void[]) null);
146 | }
147 |
148 | private Bitmap decodeSampledBitmapFromDisk() {
149 |
150 | try {
151 |
152 | if(mResolver == null || mContentProviderUri == null) {
153 | throw new BitmapDecodeException("Did not provide the uri reference or resolver");
154 | }
155 |
156 | if(mReqWidth <= 0 ) {
157 | throw new BitmapDecodeException("Did not provide a valid required width. Should be > 0");
158 | }
159 |
160 | if(mReqHeight <= 0 ) {
161 | throw new BitmapDecodeException("Did not provide a valid required height. Should be > 0");
162 | }
163 |
164 | InputStream sampleStream = mResolver.openInputStream(mContentProviderUri);
165 | InputStream samplingStream = mResolver.openInputStream(mContentProviderUri);
166 |
167 |
168 | // First decode with inJustDecodeBounds=true to check dimensions
169 | final BitmapFactory.Options options = new BitmapFactory.Options();
170 | options.inJustDecodeBounds = true;
171 | BitmapFactory.decodeStream(sampleStream, null, options);
172 |
173 | // Calculate inSampleSize
174 | options.inSampleSize = calculateInSampleSize(options, mReqWidth, mReqHeight);
175 |
176 | // Decode bitmap with inSampleSize set
177 | options.inJustDecodeBounds = false;
178 | return BitmapFactory.decodeStream(samplingStream, null, options);
179 | }catch (FileNotFoundException e) {
180 | e.printStackTrace();
181 | throw new BitmapDecodeException("Did not provide a valid Uri.");
182 |
183 | }
184 | }
185 |
186 |
187 | private Bitmap decodeSampledBitmapFromResource() {
188 |
189 | if(mResources == null ) {
190 | throw new BitmapDecodeException("Did not provide the resources");
191 | }
192 |
193 | if(mReqWidth <= 0 ) {
194 | throw new BitmapDecodeException("Did not provide a valid required width. Should be > 0");
195 | }
196 |
197 | if(mReqHeight <= 0 ) {
198 | throw new BitmapDecodeException("Did not provide a valid required height. Should be > 0");
199 | }
200 |
201 | // First decode with inJustDecodeBounds=true to check dimensions
202 | final BitmapFactory.Options options = new BitmapFactory.Options();
203 | options.inJustDecodeBounds = true;
204 | BitmapFactory.decodeResource(mResources, mDrawableId, options);
205 |
206 | // Calculate inSampleSize
207 | options.inSampleSize = calculateInSampleSize(options, mReqWidth, mReqHeight);
208 |
209 | // Decode bitmap with inSampleSize set
210 | options.inJustDecodeBounds = false;
211 | return BitmapFactory.decodeResource(mResources, mDrawableId, options);
212 | }
213 |
214 |
215 | private static int calculateInSampleSize(
216 | BitmapFactory.Options options, int reqWidth, int reqHeight) {
217 | // Raw height and width of image
218 | final int height = options.outHeight;
219 | final int width = options.outWidth;
220 | int inSampleSize = 1;
221 |
222 | if (height > reqHeight || width > reqWidth) {
223 |
224 | final int halfHeight = height / 2;
225 | final int halfWidth = width / 2;
226 |
227 | // Calculate the largest inSampleSize value that is a power of 2 and keeps both
228 | // height and width larger than the requested height and width.
229 | while ((halfHeight / inSampleSize) > reqHeight
230 | && (halfWidth / inSampleSize) > reqWidth) {
231 | inSampleSize *= 2;
232 | }
233 | }
234 |
235 | return inSampleSize;
236 | }
237 | }
238 |
--------------------------------------------------------------------------------
/app/src/main/java/com/cooltechworks/bitmapmerger/ui/fragments/BitmapOffsetFragment.java:
--------------------------------------------------------------------------------
1 | package com.cooltechworks.bitmapmerger.ui.fragments;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.graphics.Bitmap;
6 | import android.net.Uri;
7 | import android.os.Bundle;
8 | import android.support.v4.app.Fragment;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.ImageView;
13 | import android.widget.SeekBar;
14 | import android.widget.TextView;
15 |
16 | import com.cooltechworks.bitmapmerger.R;
17 | import com.cooltechworks.bitmapmerger.tasks.BitmapDecoderTask;
18 | import com.cooltechworks.bitmapmerger.tasks.BitmapMergerTask;
19 |
20 |
21 | public class BitmapOffsetFragment extends Fragment {
22 |
23 |
24 | private static final int SELECT_PHOTO_1 = 1;
25 | private static final int SELECT_PHOTO_2 = 2;
26 |
27 | private Bitmap mBaseBitmap, mMergeBitmap;
28 | private int mFromTop = 0, mFromLeft = 0;
29 | private float mScale = 0.5f;
30 |
31 | private BitmapMergerTask mBitmapMergerTask;
32 |
33 | private View mRootView;
34 |
35 |
36 | @Override
37 | public View onCreateView(LayoutInflater inflater, ViewGroup root,Bundle savedInstanceState) {
38 |
39 |
40 | mRootView = inflater.inflate(R.layout.frag_bmp_merge_offset,root,false);
41 |
42 | View.OnClickListener listener = new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 |
46 | Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
47 | photoPickerIntent.setType("image/*");
48 |
49 | switch (v.getId()) {
50 | case R.id.base_image_label:
51 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_1);
52 | break;
53 | case R.id.merge_image_label:
54 | startActivityForResult(photoPickerIntent, SELECT_PHOTO_2);
55 | break;
56 |
57 | }
58 | }
59 | };
60 |
61 | View baseLabelView = mRootView.findViewById(R.id.base_image_label);
62 | View mergeLabelView = mRootView.findViewById(R.id.merge_image_label);
63 |
64 | baseLabelView.setOnClickListener(listener);
65 | mergeLabelView.setOnClickListener(listener);
66 |
67 | int maxWidth = getDimens(R.id.image_holder)[0];
68 | int maxHeight = getDimens(R.id.image_holder)[1];
69 |
70 |
71 | SeekBar fromLeftSeek = (SeekBar) mRootView.findViewById(R.id.from_left_seek);
72 |
73 |
74 | fromLeftSeek.setMax(maxWidth);
75 | fromLeftSeek.setProgress(mFromLeft);
76 |
77 | SeekBar fromTopSeek = (SeekBar) mRootView.findViewById(R.id.from_top_seek);
78 | fromTopSeek.setMax(maxHeight);
79 | fromTopSeek.setProgress(mFromTop);
80 |
81 | SeekBar scaleSeekbar = (SeekBar) mRootView.findViewById(R.id.scale_size);
82 | scaleSeekbar.setProgress((int) (mScale * 100));
83 |
84 | ((TextView) mRootView.findViewById(R.id.from_left_label)).setText(getString(R.string.from_left, mFromLeft));
85 | ((TextView) mRootView.findViewById(R.id.from_top_label)).setText(getString(R.string.from_top, mFromTop));
86 |
87 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
88 |
89 |
90 | fromLeftSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
91 | @Override
92 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
93 |
94 | mFromLeft = progress;
95 |
96 | ((TextView) mRootView.findViewById(R.id.from_left_label)).setText(getString(R.string.from_left, mFromLeft));
97 | refresh();
98 |
99 | }
100 |
101 | @Override
102 | public void onStartTrackingTouch(SeekBar seekBar) {
103 | }
104 |
105 | @Override
106 | public void onStopTrackingTouch(SeekBar seekBar) {
107 | }
108 | });
109 |
110 | fromTopSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
111 | @Override
112 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
113 |
114 | mFromTop = progress;
115 |
116 | ((TextView) mRootView.findViewById(R.id.from_top_label)).setText(getString(R.string.from_top, mFromTop));
117 | refresh();
118 |
119 | }
120 |
121 | @Override
122 | public void onStartTrackingTouch(SeekBar seekBar) {
123 | }
124 |
125 | @Override
126 | public void onStopTrackingTouch(SeekBar seekBar) {
127 | }
128 | });
129 |
130 | scaleSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
131 | @Override
132 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
133 |
134 | mScale = progress / 100f;
135 |
136 | ((TextView) mRootView.findViewById(R.id.scale_label)).setText(getString(R.string.scale_factor, mScale));
137 | refresh();
138 |
139 | }
140 |
141 | @Override
142 | public void onStartTrackingTouch(SeekBar seekBar) {
143 |
144 | }
145 |
146 | @Override
147 | public void onStopTrackingTouch(SeekBar seekBar) {
148 |
149 | }
150 | });
151 |
152 | refresh();
153 |
154 | return mRootView;
155 |
156 |
157 | }
158 |
159 | public void refresh() {
160 |
161 | final ImageView imgView = (ImageView) mRootView.findViewById(R.id.image_holder);
162 |
163 | TextView baseLabelTextView = (TextView) mRootView.findViewById(R.id.base_image_label);
164 | TextView mergeLabelTextView = (TextView) mRootView.findViewById(R.id.merge_image_label);
165 |
166 |
167 | if (mMergeBitmap != null && mBaseBitmap != null) {
168 |
169 |
170 | int maxWidth = mBaseBitmap.getWidth();
171 | int maxHeight = mBaseBitmap.getHeight();
172 |
173 |
174 | SeekBar fromLeftSeek = (SeekBar) mRootView.findViewById(R.id.from_left_seek);
175 |
176 |
177 | fromLeftSeek.setMax(maxWidth);
178 | fromLeftSeek.setProgress(mFromLeft);
179 |
180 | SeekBar fromTopSeek = (SeekBar) mRootView.findViewById(R.id.from_top_seek);
181 | fromTopSeek.setMax(maxHeight);
182 | fromTopSeek.setProgress(mFromTop);
183 |
184 |
185 | // If you're working with larger bitmaps and continuously changing the scale value or angle value, you might notice the lag between slider change
186 | // and the image position. To get rid of that, uncomment the following codes.
187 | if (mBitmapMergerTask != null && mBitmapMergerTask.getStatus() == BitmapMergerTask.Status.RUNNING) {
188 | mBitmapMergerTask.cancel(true);
189 | }
190 |
191 |
192 | mBitmapMergerTask = new BitmapMergerTask();
193 | mBitmapMergerTask.setBaseBitmap(mBaseBitmap)
194 | .setMergeBitmap(mMergeBitmap)
195 | .setMergeListener(new BitmapMergerTask.OnMergeListener() {
196 | @Override
197 | public void onMerge(BitmapMergerTask task, Bitmap mergedBitmap) {
198 | imgView.setImageBitmap(mergedBitmap);
199 | }
200 | })
201 | .setScale(mScale)
202 | .setOffsets(mFromLeft,mFromTop)
203 | .merge();
204 |
205 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
206 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
207 |
208 |
209 | }
210 | else {
211 |
212 |
213 | if(mBaseBitmap != null) {
214 | imgView.setImageBitmap(mBaseBitmap);
215 | baseLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
216 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
217 | }
218 | else {
219 | baseLabelTextView.setTextColor(getResources().getColor(R.color.green_fg));
220 | mergeLabelTextView.setTextColor(getResources().getColor(R.color.dark_blue_fg));
221 | }
222 |
223 | }
224 |
225 | }
226 |
227 | @Override
228 | public void onActivityResult(final int requestCode, int resultCode, Intent imageReturnedIntent) {
229 | super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
230 |
231 | switch (requestCode) {
232 | case SELECT_PHOTO_1:
233 | case SELECT_PHOTO_2:
234 | if (resultCode == Activity.RESULT_OK) {
235 | final Uri imageUri = imageReturnedIntent.getData();
236 |
237 | int dimens[] = getDimens(R.id.image_holder);
238 |
239 | new BitmapDecoderTask()
240 | .setDecodingImageReference(imageUri, getActivity().getContentResolver())
241 | .setRequiredWidth(dimens[0])
242 | .setRequiredHeight(dimens[1])
243 | .setListener(new BitmapDecoderTask.OnDecodeListener() {
244 | @Override
245 | public void onDecode(BitmapDecoderTask task, Bitmap bitmap) {
246 |
247 | if (requestCode == SELECT_PHOTO_1) {
248 |
249 | mBaseBitmap = bitmap;
250 |
251 | } else {
252 |
253 | mMergeBitmap = bitmap;
254 |
255 | }
256 | refresh();
257 | }
258 | })
259 | .decode();
260 |
261 | }
262 | }
263 | }
264 |
265 | public int[] getDimens(int resId) {
266 | int width = mRootView.findViewById(resId).getMeasuredWidth();
267 | int height = mRootView.findViewById(resId).getMeasuredHeight();
268 |
269 | return new int[]{width, height};
270 |
271 | }
272 |
273 | }
274 |
--------------------------------------------------------------------------------