Last active
March 21, 2017 02:13
-
-
Save cozzin/7b062f86f82bb7a953469b84bff9f0bb to your computer and use it in GitHub Desktop.
Using ZXing library in Android Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dotincorp.android.util; | |
import android.graphics.Bitmap; | |
import android.graphics.Color; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.google.zxing.BarcodeFormat; | |
import com.google.zxing.MultiFormatWriter; | |
import com.google.zxing.common.BitMatrix; | |
/** | |
* Created by seongho on 2017. 3. 20.. | |
*/ | |
public class BarcodeGenerator extends AsyncTask<String, String, Bitmap>{ | |
public BarcodeResponse delegate = null; | |
private BarcodeFormat mBarcodeType = BarcodeFormat.CODE_128; | |
private int mImageWidth = 0; | |
private int mImageHeigth = 0; | |
@Override | |
protected Bitmap doInBackground(String... params) { | |
return getImage(params[0]); | |
} | |
@Override | |
protected void onPostExecute(Bitmap bitmap) { | |
if(delegate!=null){ | |
delegate.postResult(mBarcodeType, bitmap); | |
}else{ | |
Log.e("BarcodeGenerator", "You have not assigned BarcodeResponse delegate"); | |
} | |
} | |
public Bitmap getImage(String barcodeNumber){ | |
MultiFormatWriter gen = new MultiFormatWriter(); | |
try { | |
final int WIDTH = mImageWidth; | |
final int HEIGHT = mImageHeigth; | |
BitMatrix bytemap = gen.encode(barcodeNumber, mBarcodeType, WIDTH, HEIGHT); | |
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888); | |
for (int i = 0 ; i < WIDTH ; ++i) | |
for (int j = 0 ; j < HEIGHT ; ++j) { | |
bitmap.setPixel(i, j, bytemap.get(i,j) ? Color.BLACK : Color.WHITE); | |
} | |
return bitmap; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public int getImageHeigth() { | |
return mImageHeigth; | |
} | |
public void setImageHeigth(int mImageHeigth) { | |
this.mImageHeigth = mImageHeigth; | |
} | |
public BarcodeFormat getBarcodeType() { | |
return mBarcodeType; | |
} | |
public void setBarcodeType(BarcodeFormat mBarcodeType) { | |
this.mBarcodeType = mBarcodeType; | |
} | |
public int getImageWidth() { | |
return mImageWidth; | |
} | |
public void setImageWidth(int mImageWidth) { | |
this.mImageWidth = mImageWidth; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dotincorp.android.util; | |
import android.graphics.Bitmap; | |
import com.google.zxing.BarcodeFormat; | |
/** | |
* Created by seongho on 2017. 3. 20.. | |
*/ | |
public interface BarcodeResponse { | |
void postResult(BarcodeFormat barcodeType , Bitmap asyncResult); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar' | |
compile 'com.google.zxing:core:3.2.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.os.Bundle; | |
import android.widget.ImageView; | |
import com.dotincorp.android.util.BarcodeGenerator; | |
import com.dotincorp.android.util.BarcodeResponse; | |
import com.google.zxing.BarcodeFormat; | |
/** | |
* Created by seongho on 2017. 3. 20.. | |
*/ | |
public class MainActivity extends Activity implements BarcodeResponse { | |
ImageView mBarcodeImage; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setLayout(); | |
} | |
private void setLayout(){ | |
setContentView(R.layout.activity_barcode); | |
mBarcodeImage = (ImageView) findViewById(R.id.barcode_image); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
// EAM_13 | |
BarcodeGenerator barcodeGenerator = new BarcodeGenerator(); | |
barcodeGenerator.delegate = this; | |
barcodeGenerator.setBarcodeType(BarcodeFormat.EAN_13); | |
barcodeGenerator.setImageWidth(320); | |
barcodeGenerator.setImageHeigth(180); | |
barcodeGenerator.execute("5901234123457"); | |
} | |
@Override | |
public void postResult(BarcodeFormat barcodeType, Bitmap asyncResult) { | |
if(barcodeType == BarcodeFormat.EAN_13){ | |
mBarcodeImage.setImageBitmap(asyncResult); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment