Skip to content

Commit

Permalink
Merge pull request #6 from SumiMakito/develop
Browse files Browse the repository at this point in the history
Sample updated: added a save button;
  • Loading branch information
sumimakito authored May 2, 2017
2 parents 4788ebe + b75b6ae commit 26e48f7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.github.sumimakito.awesomeqrsample"
minSdkVersion 19
targetSdkVersion 25
versionCode 5
versionName "1.4"
versionCode 6
versionName "1.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
Expand All @@ -24,6 +26,12 @@

import com.github.sumimakito.awesomeqr.AwesomeQRCode;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private final int SELECT_FILE_REQUEST_CODE = 822;
Expand All @@ -43,6 +51,8 @@ public class MainActivity extends AppCompatActivity {
private CheckBox ckbBinarize;
private CheckBox ckbRoundedDataDots;
private EditText etBinarizeThreshold;
private Bitmap qrBitmap;
private Button btOpen;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -62,6 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
btSelectBG = (Button) findViewById(R.id.backgroundImage);
btRemoveBackgroundImage = (Button) findViewById(R.id.removeBackgroundImage);
btGenerate = (Button) findViewById(R.id.generate);
btOpen = (Button) findViewById(R.id.open);
ckbWhiteMargin = (CheckBox) findViewById(R.id.whiteMargin);
ckbAutoColor = (CheckBox) findViewById(R.id.autoColor);
ckbBinarize = (CheckBox) findViewById(R.id.binarize);
Expand Down Expand Up @@ -93,6 +104,13 @@ public void onClick(View v) {
}
});

btOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (qrBitmap != null) saveBitmap(qrBitmap);
}
});

btRemoveBackgroundImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -196,13 +214,13 @@ private void generate(final String contents, final int size, final int margin, f
@Override
public void run() {
try {
final Bitmap b = AwesomeQRCode.create(contents, size, margin, dotScale, colorDark,
qrBitmap = AwesomeQRCode.create(contents, size, margin, dotScale, colorDark,
colorLight, background, whiteMargin, autoColor, binarize, binarizeThreshold,
roundedDD);
runOnUiThread(new Runnable() {
@Override
public void run() {
qrCodeImageView.setImageBitmap(b);
qrCodeImageView.setImageBitmap(qrBitmap);
scrollView.post(new Runnable() {
@Override
public void run() {
Expand All @@ -226,4 +244,31 @@ public void run() {
}
}).start();
}

private void saveBitmap(Bitmap bitmap) {
FileOutputStream fos = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
File outputFile = new File(getPublicContainer(), System.currentTimeMillis() + ".png");
fos = new FileOutputStream(outputFile);
fos.write(byteArray);
fos.close();
Toast.makeText(this, "Image saved to " + outputFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Failed to save the image.", Toast.LENGTH_LONG).show();
}
}

public static File getPublicContainer() {
File musicContainer = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File aqr = new File(musicContainer, "AwesomeQR");
if (aqr.exists() && !aqr.isDirectory()) {
aqr.delete();
}
aqr.mkdirs();
return aqr;
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@
android:layout_centerHorizontal="true"
android:text="Generate" />


<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Save image" />

<ImageView
android:id="@+id/qrcode"
android:layout_width="300dp"
Expand Down
1 change: 0 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
Expand Down

0 comments on commit 26e48f7

Please sign in to comment.