Permalink
Browse files

Merge pull request #17 from atomfrede/master

Modules and fixing #16
  • Loading branch information...
kenglxn committed May 23, 2014
2 parents 6177c1b + 73a6094 commit 827ec9901fba8e28aba5fec06a365595b3a20f0f
View
@@ -9,3 +9,22 @@ android
core
javase
.classpath
+
+*.*~
+.project
+.settings
+core/.classpath
+.classpath
+# built application files
+*.apk
+*.ap_
+
+# files for the dex VM
+*.dex
+
+# Java class files
+*.class
+
+# generated files
+bin/
+gen/
View
@@ -1,4 +1,4 @@
-language: java
+language: android
before_install: "git clone -b travis `git config --get remote.origin.url` target/travis"
branches:
except:
@@ -10,3 +10,6 @@ env:
global:
- secure: "nzDY1aT+P3NFCsbYS3He3C60auLKDYuGOYJHPmT4eeZDxGgxuqErMjNZ439L\nJxD8UUaJz19r3IP6543sNCmtJWbQjBB231XKo3Ecdp8aWxs8kBOhKWeLkbGv\nqi5jbPTaB/K+gYSiE3AEiWJV3wL88VQ+5V4/uUpKDId97TZhAVg="
- secure: "Y728e7nyo+vG+1yuMkohnzOCRMo+z2tkaILW0AP9DBiPe8Pr1Uau2aBTW9WX\nW3CJj59cfv2QR4y9gtgcxUK3C92aaqbMQdNtX+qPciuBjMxKR+WdBFsh0bcc\nPdThBsji4ldzKS5v5O32g7it7RwA7978sK4UXOcNFEAyHY7fowQ="
+android:
+ components:
+ - build-tools-19.0.2
View
@@ -13,21 +13,46 @@ ZXING: http://code.google.com/p/zxing/
#### Get it:
-QRGen is available from [Maven Central Repository](http://search.maven.org/#browse%7C-852965118) using the following dependency declaration:
+QRGen consists of three modules: ```core```, ```javase``` and ```android```. Everything is available from [Maven Central Repository](http://search.maven.org/#browse%7C-852965118).
+
+##### Java Application
+
+When developing a Java application you need to add ```javase``` module to your list of dependencies. The required ```core``` module will be added automatically by your build system:
+
+
+Gradle:
+
+ dependencies {
+ compile ("net.glxn.qrgen:javase:2.0-SNAPSHPOT")
+ }
+
+Maven:
+
+ <dependencies>
+ <dependency>
+ <groupId>net.glxn.qrgen</groupId>
+ <artifactId>javase</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+
+##### Android
+
+When you want to use QRGen inside your android application you need to add the ```android``` module to your list of dependencies. The required ```core``` module will be added automatically by your build system:
Gradle:
dependencies {
- compile ("net.glxn:qrgen:1.4")
+ compile ("net.glxn.qrgen:android:2.0-SNAPSHPOT")
}
Maven:
<dependencies>
<dependency>
- <groupId>net.glxn</groupId>
- <artifactId>qrgen</artifactId>
- <version>1.4</version>
+ <groupId>net.glxn.qrgen</groupId>
+ <artifactId>android</artifactId>
+ <version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
View
@@ -0,0 +1,16 @@
+# built application files
+*.apk
+*.ap_
+
+# files for the dex VM
+*.dex
+
+# Java class files
+*.class
+
+# generated files
+bin/
+gen/
+
+/project.properties
+
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="net.glxn.qrgen.android" android:versionCode="1" android:versionName="2.0-SNAPSHOT">
+<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+
+</manifest>
+
View
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<lint>
+</lint>
View
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>qrgen-parent</artifactId>
+ <groupId>net.glxn.qrgen</groupId>
+ <version>2.0-SNAPSHOT</version>
+ </parent>
+ <groupId>net.glxn.qrgen</groupId>
+ <artifactId>android</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <packaging>apklib</packaging>
+ <name>android</name>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <platform.version>4.1.1.4</platform.version>
+ <android.plugin.version>3.8.2</android.plugin.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.google.android</groupId>
+ <artifactId>android</artifactId>
+ <version>${platform.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>net.glxn.qrgen</groupId>
+ <artifactId>core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.robolectric</groupId>
+ <artifactId>robolectric</artifactId>
+ <version>2.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.squareup</groupId>
+ <artifactId>fest-android</artifactId>
+ <version>1.0.7</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>com.jayway.maven.plugins.android.generation2</groupId>
+ <artifactId>android-maven-plugin</artifactId>
+ <version>${android.plugin.version}</version>
+ <extensions>true</extensions>
+ <configuration>
+ <sdk>
+ <platform>16</platform>
+ </sdk>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="hello">Hello android!</string>
+ <string name="lib_name">android</string>
+</resources>
@@ -0,0 +1,42 @@
+package net.glxn.qrgen.android;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import net.glxn.qrgen.core.image.ImageType;
+import android.graphics.Bitmap;
+import android.graphics.Bitmap.CompressFormat;
+
+public class BitmapIO {
+
+ public static boolean write(Bitmap image, String type, OutputStream stream) {
+ if (type.equals(ImageType.PNG)) {
+ return image.compress(CompressFormat.PNG, 80, stream);
+ } else if (type.equals(ImageType.JPG)) {
+ return image.compress(CompressFormat.JPEG, 80, stream);
+ }
+
+ // Default is PNG and for GIFs also
+ return image.compress(CompressFormat.PNG, 80, stream);
+ }
+
+ public static boolean write(Bitmap image, String type, File file) throws IOException {
+ OutputStream stream = null;
+ try{
+ stream = new FileOutputStream(file);
+ return write(image, type, stream);
+
+ } catch (IOException ioe) {
+
+ } finally {
+ if(stream != null) {
+ stream.flush();
+ stream.close();
+ }
+ }
+
+ return false;
+ }
+}
@@ -0,0 +1,48 @@
+package net.glxn.qrgen.android;
+
+import android.graphics.Bitmap;
+
+public class MatrixToImageConfig {
+
+ public static final int BLACK = 0xFF000000;
+ public static final int WHITE = 0xFFFFFFFF;
+
+ private final int onColor;
+ private final int offColor;
+
+ /**
+ * Creates a default config with on color {@link #BLACK} and off color
+ * {@link #WHITE}, generating normal black-on-white barcodes.
+ */
+ public MatrixToImageConfig() {
+ this(BLACK, WHITE);
+ }
+
+ /**
+ * @param onColor
+ * pixel on color, specified as an ARGB value as an int
+ * @param offColor
+ * pixel off color, specified as an ARGB value as an int
+ */
+ public MatrixToImageConfig(int onColor, int offColor) {
+ this.onColor = onColor;
+ this.offColor = offColor;
+ }
+
+ public int getPixelOnColor() {
+ return onColor;
+ }
+
+ public int getPixelOffColor() {
+ return offColor;
+ }
+
+ Bitmap.Config getBufferedImageColorModel() {
+// return 0;
+ // Use faster BINARY if colors match default
+ //Represents an image with 8-bit RGBA color components packed into integer pixels.
+ //Represents an opaque byte-packed 1, 2, or 4 bit image.
+ return onColor == BLACK && offColor == WHITE ? Bitmap.Config.ALPHA_8: Bitmap.Config.ARGB_8888;
+ //return onColor == BLACK && offColor == WHITE ? Bitmap.TYPE_BYTE_BINARY: BufferedImage.TYPE_INT_RGB;
+ }
+}
@@ -0,0 +1,114 @@
+package net.glxn.qrgen.android;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+
+import com.google.zxing.common.BitMatrix;
+
+public class MatrixToImageWriter {
+
+ private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
+
+ private MatrixToImageWriter() {
+ }
+
+ /**
+ * Renders a {@link BitMatrix} as an image, where "false" bits are rendered
+ * as white, and "true" bits are rendered as black. Uses default
+ * configuration.
+ *
+ * @param matrix
+ * {@link BitMatrix} to write
+ * @return {@link Bitmap} representation of the input
+ */
+ public static Bitmap toBitmap(BitMatrix matrix) {
+ return toBitmap(matrix, DEFAULT_CONFIG);
+ }
+
+ /**
+ * As {@link #toBitmap(BitMatrix)}, but allows customization of the output.
+ *
+ * @param matrix
+ * {@link BitMatrix} to write
+ * @param config
+ * output configuration
+ * @return {@link Bitmap} representation of the input
+ */
+ public static Bitmap toBitmap(BitMatrix matrix, MatrixToImageConfig config) {
+ int width = matrix.getWidth();
+ int height = matrix.getHeight();
+
+ Bitmap image = Bitmap.createBitmap(width, height,
+ Bitmap.Config.ARGB_8888);
+ Canvas canvas = new Canvas();
+ canvas.setBitmap(image);
+
+ for (int x = 0; x < width; x++) {
+ for (int y = 0; y < height; y++) {
+ // image.setPixel(x, y, matrix.get(x, y) ? onColor : offColor);
+ Paint paint = new Paint();
+ if (matrix.get(x, y)) {
+ paint.setColor(Color.BLACK);
+ } else {
+ paint.setColor(Color.WHITE);
+ }
+
+ canvas.drawPoint(x, y, paint);
+
+ }
+ }
+
+ canvas.drawBitmap(image, 0, 0, null);
+ canvas.save(Canvas.ALL_SAVE_FLAG);
+ return image;
+ }
+
+ /**
+ * Writes a {@link BitMatrix} to a file.
+ *
+ * @see #toBufferedImage(BitMatrix)
+ */
+ public static void writeToFile(BitMatrix matrix, String format, File file)
+ throws IOException {
+ writeToFile(matrix, format, file, DEFAULT_CONFIG);
+ }
+
+ /**
+ * As {@link #writeToFile(BitMatrix, String, File)}, but allows
+ * customization of the output.
+ */
+ public static void writeToFile(BitMatrix matrix, String format, File file, MatrixToImageConfig config) throws IOException {
+ Bitmap image = toBitmap(matrix, config);
+ if (!BitmapIO.write(image, format, file)) {
+ throw new IOException("Could not write an image of format " + format + " to " + file);
+ }
+ }
+
+ /**
+ * Writes a {@link BitMatrix} to a stream.
+ *
+ * @see #toBufferedImage(BitMatrix)
+ */
+ public static void writeToStream(BitMatrix matrix, String format,
+ OutputStream stream) throws IOException {
+ writeToStream(matrix, format, stream, DEFAULT_CONFIG);
+ }
+
+ /**
+ * As {@link #writeToStream(BitMatrix, String, OutputStream)}, but allows
+ * customization of the output.
+ */
+ public static void writeToStream(BitMatrix matrix, String format,
+ OutputStream stream, MatrixToImageConfig config) throws IOException {
+ Bitmap image = toBitmap(matrix, config);
+ if (!BitmapIO.write(image, format, stream)) {
+ throw new IOException("Could not write an image of format " + format);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 827ec99

Please sign in to comment.