Tuesday, July 26, 2016

How to Compile ZXING Barcode Scanner App in Android Studio Part 2

This is a follow-up from the previous post. Here, I will cover how to compile only from source files, without using pre-built jar files.

We will resume from where we left off from the previous post. Select File -> Project Structure and click on + icon. Select Java Library and set the library name as core and edit the package to be com.google.zxing. Leave class name as MyClass, as we will delete this file in a moment.

When the module has been created, you will see core directory in the project's root directory. Copy all files and folders from zxing source directory, i.e., zxing/core/src/main/java/com/google/zxing/ to the project's core/src/main/java/com/google/zxing/ directory. Delete MyClass.java file, which has been automatically created by Android Studio when creating this module.

Next, you will need to add module dependency. Add the following lines to app module's build.gradle file:
...
dependencies {
    compile project(':core')
}

Lastly, you will also need to add zxing/android-core module into this project. Since this consists of only a single file, it is easiest to simply copy this file into the app module. Copy zxing/android-core/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java file from the zxing source directory into the project's app/src/main/java/com/google/zxing/client/android/camera/ directory. Note that this is the app module's directory, not core library's directory.

Now, you should be able to compile the project!


NOTE: Depending on your configuration, you may receive an error similar to:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.

To resolve this, simply do what it asks. That is, copy the above two lines of codes into core module's build.gradle file, which should look similar to:
apply plugin: 'java'

targetCompatibility = '1.7'
sourceCompatibility = '1.7'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Monday, July 25, 2016

How to Compile ZXING Barcode Scanner App in Android Studio Part 1

In this post, I will cover how to compile stand-alone zxing's Barcode Scanner app in Android Studio.

First, download the zxing's latest source files from here or run git clone:
$ git clone https://github.com/zxing/zxing

Next, launch Android Studio and choose Import project (Eclipse ADT, Gradle, etc) and choose zxing/android folder. Select all three check boxes when prompted, and finish importing.

In the app module's build.gradle file, edit compileSdkVersion, buildToolsVersion, minSdkVersion, and targetSdkVersion values if necessary.

Try to compile the project. It will complain with the following error:
Error:(19, 24) error: cannot find symbol class Result

That is, in the HistoryItem.java file, importing com.google.zxing.Result cannot be resolved. This is because we need to link zxing/core library. You could either import zxing/core source files and compile, or simply include pre-built jar file.

In this post, I will go through the method of linking pre-built jar file. If you are curious as to how to compile the project all from source files, go straight to my follow up post.

To compile using pre-built jar files, go to zxing's maven release repository and navigate to zxing/core/ and download the latest version of jar file. I downloaded core-3.2.1.jar file.

Next, create app/src/main/libs directory in the project and copy core-3.2.1.jar file here. Now we need to include this file as a library in Android Studio. You could do this by right-clicking this jar file in Android Studio Project pane and selecting Add to Library,which will add the following lines of code in app's build.gradle file:
dependencies {
    compile files('src/main/libs/core-3.2.1.jar')
}

You can try to compile the project again, but it will probably prompt another error:
Error:(124, 24) error: cannot find symbol variable CameraConfigurationUtils

Again, this is because we need zxing/android-core library in order to compile the app. Similar to zxing/core library, we go to the zxing's release repository and navigate to zxing/android-core/ and download the latest version of jar file. I downloaded android-core-3.2.1.jar file.

Similar to the core library file, we need to copy this file into app/src/main/libs directory in the project and select Add to Library. Your app's build.gradle should read:
...
dependencies {
    compile files('src/main/libs/core-3.2.1.jar')
    compile files('src/main/libs/android-core-3.2.1.jar')
}

Now, you should be able to compile the app!

Saturday, July 23, 2016

How to Compile Mixed Processing OpenCV Tutorial App on Android Studio

The second OpenCV for Android tutorial app, namely Mixed Processing, differs from Camera Preview app in that it also has a C++ source file, thus requiring Android NDK. To compile this project in Android Studio, the first several steps are identical to Camera Preview app, so follow instructions in my previous post. In summary, these steps that are required to integrate OpenCV for Android SDK statically are:

1. Download OpenCV for Android and unzip
2. Import the tutorial project to Android Studio
3. Adjust the Android API and SDK Tools version in the app and library module build.gradle files
4. Copy OpenCV Android native library folder into the app module folder and rename it as jniLibs
5. Add OpenCV static initializing code to the main activity java file
6. Add appcompat dependency in the app module build.gradle file (for API 23 or above only)
7. Add camera permission code in the java file (for API 23 or above only)
8. Change the theme in AndroidManifest.xml file (for API 23 or above only)

Even after all the above changes, the project will still not compile because it contains native C++ file, which we must let Android Studio know what to do with. So, here we go again! I will assume the project directory of ~/AndroidStudioProjects/tutorial-2-mixedprocessing. Before we do anything further, make sure to install Android NDK from SDK Manager.

When you try to compile the project, it will complain with the following error message:
Error:(12, 0) NDK integration is deprecated in the current plugin.
Consider trying the new experimental plugin
Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration

Let's do what it asks for. You probably do not have this gradle.properties file yet, so let's create it.
$ echo "android.useDeprecatedNdk=true" > ~/AndroidStudioProjects/tutorial-2-mixedprocessing/gradle.properties

When you try to build the project, it will now complain with two errors:
Error:(2, 33) opencv2/core/core.hpp: No such file or directory
...
Error:Execution failed for task ':openCVTutorial2MixedProcessing:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '~/Library/Android/Sdk/ndk-bundle/ndk-build'' finished with non-zero exit value 2

Let's tackle these errors one by one. Open up the app module's build.gradle file, and edit it similar to below:
import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial2"
        minSdkVersion 8
        targetSdkVersion 23

        ndk {
            moduleName "mixed_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = []
    }

    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine '~/Library/Android/Sdk/ndk-bundle/ndk-build', '-C', file('src/main').absolutePath // replace with your path
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

dependencies {
    compile project(':openCVLibrary310')
    compile 'com.android.support:appcompat-v7:23.0.0'
}

Note that you must replace ~/Library/Android/Sdk/ndk-bundle/ndk-build with the NDK path in your system. You can check this in local.settings file, which will have a line similar to
ndk.dir=~/Library/Android/Sdk/ndk-bundle
You can simply use this path followed by /ndk-build, as shown above.

Now try to build the project again, and you will see the first error message is gone. To solve the last error, edit the app module's jni/Android.mk file as shown below:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
include ~/Downloads/opencv3.1/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk // Replace this with  your own path where you have extracted OpenCV for Android

LOCAL_MODULE    := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

Again, make sure to replace ~/Downloads/opencv3.1/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk with the appropriate path.

Finally, if you plan to run the app on x86 emulator or devices, you must also edit app module's jni/Application.mk file and replace APP_ABI := armeabi-v7a with APP_ABI := all.

 At last, you should be able to compile and run this Mixed Processing tutorial app on your device or emulator!

By the way, if you think you are missing the menu (Such as Samsung Galaxy phones), press-and-hold the back button; it will show the menu items in the full-screen mode.

How to Compile CameraPreview OpenCV Tutorial App on Android Studio

First, download the latest OpenCV for Android from here. At the time of writing this post, the latest version is OpenCV 3.1.0. I will assume that you have downloaded the file in ~/Downloads folder.

Unzip the contents by running
$ unzip OpenCV-3.1.0-android-sdk.zip

Next, open up Android Studio, and select Import project (Eclipse ADT, Gradle, etc.) and choose ~/Downloads/OpenCV-android-sdk/samples/tutorial-1-camerapreview

Choose the import destination directory as desired. I will assume it to be ~/AndroidStudioProjects/tutorial-1-camerapreview

Check all three boxes and click on finish.

Now, you should be prompted with an auto-generated Eclipse Android Project Import Summary. In the messages box below, you may probably see
Error:Cause: failed to find target with hash string 'android-14' in: /data/Android/Sdk
Install missing platform(s) and sync project

To fix this, open up build.gradle files for both the openCVLibrary module and openCVTutorial module. Make sure to change compile and target SDK versions appropriately. I will use API 23 and Build Tools 23.0.3 throughout this post, but your versions may differ from me. For example, gradle.build file for the openCVLibrary should read

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

and openCVTutorial module's build.gradle should read

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial1"
        minSdkVersion 8
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':openCVLibrary310')
}

Now, you should be able to successfully compile the project. Try running the app on your device or emulator. You will probably be encountered with Package Not Found message, unless you already have OpenCV Manager installed on your device. You should be able to successfully run the app after installing OpenCV Manager!

In case you do not want to require OpenCV Manager on the device, it is possible to link the OpenCV library as a static library. To do this, you will first need to copy the native library folder to the project directory and name it as jniLibs:
$ cp -r ~/Downloads/OpenCV-android-sdk/sdk/native/libs ~/AndroidStudioProjects/tutorial-1-camerapreview/openCVTutorial1CameraPreview/src/main/jniLibs

Next, you will need to insert static initialization code into the main activity java file at ~/AndroidStudioProjects/tutorial-1-camerapreview/openCVTutorial1CameraPreview/src/main/java/org/opencv/samples/tutorial1/Tutorial1Activity.java:

...
public class Tutorial1Activity extends Activity implements CvCameraViewListener2 {
    static {
        if (!OpenCVLoader.initDebug()) {
            Log.v("OpenCV", "ERROR: OpenCV Library Load Failed");
        } else {
            Log.v("OpenCV", "OpenCV Library Load Successful");
        }
    }
...

where 7 lines starting from static have been inserted.

You should now be able to compile and run the app without OpenCV Manager app on the device. Sometimes, you need to clean build and rebuild the project, or select File -> Invalidate Caches / Restart option to get it take effect. If it still says it requires OpenCV Manager, make sure to delete the original app from the device, rebuild the app, and re-run the app.

NOTE - if you are compiling for Android API 23 or above, you will need to set permission for the camera. Follow the instructions below:

1. Add a dependency in openCVTutorial module's build.gradle file to read
...
dependencies {
    compile project(':openCVLibrary310')
    compile 'com.android.support:appcompat-v7:23.0.0'
}
...

Make sure to insert the appropriate repository version in place of v7:23.0.0; Else, you could always install one.

2. Let the main activity extend AppCompatActivity and add permission request code in the main java file:
...
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
...
public class Tutorial1Activity extends AppCompatActivity implements CvCameraViewListener2 {
...
    public void onCreate(Bundle savedInstanceState) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA},
                0);
...

This is the bare minimum code to access camera for API 23 and above. For more details and practical coding, please refer to Android's official document.

3. In openCVTutorial module's AndroidManifest.xml file, replace android:theme="@android:style/Theme.NoTitleBar.Fullscreen" with android:theme="@style/Theme.AppCompat.Light".

Now, you should be able to run the app without OpenCV Manager installed! If you are running on the emulator, make sure to go to the advanced option to emulate back camera.

Sunday, July 17, 2016

How to List Installed Packages in Ubuntu using Dpkg

In Ubuntu, we use apt-get to install and remove packages. To list all the packages installed on the system, one needs to run
$ dpkg --get-selections

To search for a certain package name, say python, among the list, one needs to pipe the result to grep,
$ dpkg -get-selections | grep python

Lastly, to check the details of the installed package, say python3, one needs to run
$ dpkg -s python3

That's it.

Saturday, July 16, 2016

How to Import Existing Git Repository and Upload into Git Server

Say you have a local git repository called my_project in your home folder ~ of your local machine, and you'd like upload this into your personal git server. Here is how you can do this. For more info, please refer to here.

First, make sure that your git repository folder exists in the working directory.
$ pwd
/home/your_username/
$ ls
my_project/
...

Then, create a bare git repository from the existing repository
$ git clone --bare my_project my_project.git

You should now have the cloned bare repository
$ ls
my project/
my project.git/
...

Noe that .git ending is a conventional indication of server repository.

Next, copy the cloned repository to your server, usually through scp
$ scp -r my_project.git git_server_user@your.git.server:/git/

Here, you will need to replace git_server_user, your.git.server, and /git/ with the appropriate server username, server address, and destination path in the server, respectively.

That should be it. Now, to clone from your server, simply run the following from a client system
$ git clone ssh://git_server_user@your.git.server:/git/my_project.git

Of course, you may want to delete the bare repository from the local machine (not the server).
$ rm -rf my_project.git

That should do it!

Sunday, July 10, 2016

Tab Completion with Linked Directory (Bash)

I found it very annoying that bash won't fully tab-complete a linked directory; it omits the very important character / at the end.

For example, if I type in
$ cd ~/Doc
and press [tab], it will auto-complete as below
$ cd ~/Documents/
with / character at the end, since this is a directory.

Unfortunately, however, if I create a linked directory
$ ln -s ~/Documents ~/linked_Documents
and try
$ cd ~/linked_Doc
and again press [tab], it will auto-complete to
$ cd ~/linked_Documents
without / character.

The solution for this is found here, which is reproduced below.

Simply add the following lines into ~/.inputrc and restart your terminal
$include /etc/inputrc
set mark-symlinked-directories on

That should do it!

Monday, July 4, 2016

Use Git to Version Control DOCX Files for Windows / Cygwin

Git is a wonderful tool for version control of various texts and documents, such as source code. Unfortunately however, git by default will not be able to produce diff result for Microsoft's word document format (.docx) files. In this post, I will go over how to use git diff docx files on Cygwin. This post is based on this.

Let's see what happens when we try to use git for docx files. Download Cygwin x86 for 32-bit system or Cygwin x64 for 64-bit system from the Cygwin download page.

Run the installer file to install it on the system. Just go through installation with default settings until Select Packages section. Here, search for and install unzip (Archive), git (Devel), and vim (Editors). Note that I will assume the default Cygwin installation directory to be c:\cygwin. Note that if you download 64-bit version, the default directory will be c:\cygwin64.

Let's run Cygwin. You should see bash terminal. Say you have your docx files saved in c:\Users\unixnme\Documents\docx folder. To change directory into this folder, run
$ cd /cygdrive/c/Users/unixnme/Documents/docx

Let's assume that there is a docx file called test.docx in the folder.
$ ls
test.docx

Let's initiate a git repository and commit test.docx file.
$ git init
Initialized empty Git repository in /cygdrive/c/Users/unixnme/Documents/docx.git
$ git add test.docx
$ git config --global user.name your_name
$ git config --global user.email your_email@address.com
$ git commit -m "initial commit"
[master (root-commit) 90cf9f2] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 test.docx

Next, make any changes to test.docx file, save, and run git diff command:
$ git diff
diff --git a/test.docx b/test.docx
index 41c6200..ce81ceb 100755
Binary files a/test.docx and b/test.docx differ

As expected, git diff will complain that test.docx is a binary file. Now, let us enable git diff for docx files.


First, we need to install docx2txt utility. Download docx2txt-1.4.tgz file from here to your download folder, say c:\Users\unixnme\Downloads. Extract the files by running tar command
$ cd /cygdrive/c/Users/unixnme/Downloads
$ tar vxzf docx2txt-1.4.tgz

Go into the extracted directory and enable execution flag for windows installer batch file.
$ cd docx2txt-1.4
$ chmod u+x WInstall.bat

We are ready to install it. Enter the installation folder path and Perl path as follows:
$ ./WInstall.bat
Welcome to command line installer for docx2txt.

Where should the docx2txt tool be installed? Specify the location
without surrounding quotes.

Installation Folder :c:\cygwin\bin

Please specify fully qualified paths to utilities when requested.
Perl.exe is required for docx2txt tool as well as for this installation.

Path to Perl.exe : c:\cygwin\bin\perl.exe

Continuing with simple installation ....

Copying script files to "c:\cygwin\bin" ....

Please adjust perl, unzip and cakecmd paths (as needed) in
"c:\cygwin\bin\docx2txt.bat" and "c:\cygwin\bin\docx2txt.config"

Note that I simply installed docx2txt in c:\cygwin\bin, but you may want to change it as desired. The default perl executable is installed in the cygwin/bin directory, which in this case is c:\cygwin\bin\perl.exe. Note that if you have installed the 64-bit version of cygwin, then you will need to replace cygwin with cygwin64 for paths. If it asks for unzip and cakecmd paths, simply leave them blank as they are not necessary.

Next, we need to create /usr/bin/docx2txt file by
$ vim /usr/bin/docx2txt

with the following content.
#!/bin/bash
docx2txt.pl $1 -

Make sure to set the executable flag
$ chmod u+x /usr/bin/docx2txt

Finally, go back to the git repository folder and create .gitattributes file to apply filter for docx files
$ cd /cygdrive/c/Users/unixnme/Documents/docx
$ echo "*.docx diff=word" > .gitattributes

Finally, edit git config file to run /usr/bin/docx2txt script file for word filter
$ git config diff.word.textconv docx2txt

When everything is successful, you should see diff result for the modified docx file like a regular plain text file
$ git diff
diff --git a/test.docx b/test.docx
index 41c6200..ce81ceb 100755
--- a/test.docx
+++ b/test.docx
@@ -1 +1 @@
-This is a test file
+This is a test file, which I have modified after commit.

Enjoy git on your Microsoft Word documents!

Saturday, July 2, 2016

How to Setup Git Server on Mac OS X

In this tutorial, I will go over the instructions to setup a git server on Mac OS X. Here, I will assume that the server IP address is 12.34.56.78.

First, you will need to add a user named git, into which client machines will ssh into. To do so,
1. Open up System Preferences -> Users & Groups
2. You may need to click the Lock image to make any changes. 
3. Click on the + button to add a user.
4. Add a standard user whose full name and account name is git. Enter password.
5. Click on Create User button.

Next, you will need to allow ssh login for user git from client machines. To do so,
1. Open up System Preferences -> Sharing
2. Check Remote Login box.
3. Add the user git in the Allow access for field by clicking on the + button.

From your client system, make sure that you can remote log into the server's git account.
$ ssh git@12.34.56.78 -p 22

Note that here I am assuming that you are using the port 22 to access the server. Depending on your router or firewall configuration, the port number may differ.

If you can successfully log into the account, then you are pretty much done. Let's assume that you will want to create a repository named Project. From the git account from the server or through ssh, run
$ pwd
/Users/git
$ git init --bare Project.git

This will create a bare Project repository on the server, which clients can clone, push to, and pull from. The location of this repository in this case is /Users/git/Project.git, as you can see from pwd command above. To clone this repository from a client, you will need to run the following from the client
$ git clone ssh://git@12.34.56.78:22/Users/git/Project.git

You will need to enter the password for the server's git account, as if you are remote logging into it through ssh. You should now have a personal git server.

Note: If you are concerned with safety, you may want to disable password login and instead only allow public key authentication. I will cover how to do so in the future post.