Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample updated: added a save button; #6

Merged
merged 1 commit into from
May 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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) {
@@ -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);
@@ -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) {
@@ -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() {
@@ -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
@@ -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"
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"