月下载量上千次Android实现二维码生成器app源码分享
在360上面上线了一个月,下载量上千余次。这里把代码都分享出来,供大家学习哈!还包括教大家如何接入广告,赚点小钱花花,喜欢的帮忙顶一个,大神见了勿喷,小学僧刚学Android没多久。首先介绍这款应用:APP是一款二维码生成器,虽然如何制作二维码教程网上有很多,我这里再唠叨一下并把我的所有功能模块代码都分享出来。
在这里我们需要一个辅助类RGBLuminanceSource,这个类Google也提供了,我们直接粘贴过去就可以使用了
packagecom.njupt.liyao;
importcom.google.zxing.LuminanceSource;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importjava.io.FileNotFoundException;
publicfinalclassRGBLuminanceSourceextendsLuminanceSource{
privatefinalbyte[]luminances;
publicRGBLuminanceSource(Stringpath)throwsFileNotFoundException{
this(loadBitmap(path));
}
publicRGBLuminanceSource(Bitmapbitmap){
super(bitmap.getWidth(),bitmap.getHeight());
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
int[]pixels=newint[width*height];
bitmap.getPixels(pixels,0,width,0,0,width,height);
//Inordertomeasurepuredecodingspeed,weconverttheentireimage
//toagreyscalearray
//upfront,whichisthesameastheYchannelofthe
//YUVLuminanceSourceintherealapp.
luminances=newbyte[width*height];
for(inty=0;y<height;y++){
intoffset=y*width;
for(intx=0;x<width;x++){
intpixel=pixels[offset+x];
intr=(pixel>>16)&0xff;
intg=(pixel>>8)&0xff;
intb=pixel&0xff;
if(r==g&&g==b){
//Imageisalreadygreyscale,sopickanychannel.
luminances[offset+x]=(byte)r;
}else{
//Calculateluminancecheaply,favoringgreen.
luminances[offset+x]=(byte)((r+g+g+b)>>2);
}
}
}
}
@Override
publicbyte[]getRow(inty,byte[]row){
if(y<0||y>=getHeight()){
thrownewIllegalArgumentException(
"Requestedrowisoutsidetheimage:"+y);
}
intwidth=getWidth();
if(row==null||row.length<width){
row=newbyte[width];
}
System.arraycopy(luminances,y*width,row,0,width);
returnrow;
}
//Sincethisclassdoesnotsupportcropping,theunderlyingbytearray
//alreadycontains
//exactlywhatthecallerisaskingfor,sogiveittothemwithoutacopy.
@Override
publicbyte[]getMatrix(){
returnluminances;
}
privatestaticBitmaploadBitmap(Stringpath)throwsFileNotFoundException{
Bitmapbitmap=BitmapFactory.decodeFile(path);
if(bitmap==null){
thrownewFileNotFoundException("Couldn'topen"+path);
}
returnbitmap;
}
}
publicBitmapgetTwoDimensionPicture(Stringtext,intwidth,intheight)throwsWriterException{
if(text.equals(""))
{
text="";
}
Hashtable<EncodeHintType,String>hints=newHashtable<EncodeHintType,String>();
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
BitMatrixbitMatrix=newQRCodeWriter().encode(text,
BarcodeFormat.QR_CODE,width,height,hints);
int[]pixels=newint[width*height];
for(inty=0;y<height;y++){
for(intx=0;x<width;x++){
if(bitMatrix.get(x,y))
{
pixels[y*width+x]=BLACK;
}
else
{
pixels[y*width+x]=WHITE;
}
}
}
Bitmapbitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels,0,width,0,0,width,height);
returnbitmap;
}
publicvoidcreateDirctoryToSaveImage(){
StringdirPath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode";
FiledirFile=newFile(dirPath);
if(!dirFile.exists()){
dirFile.mkdir();
}
}
publicvoidwriteBitMapToSDCard(Bitmapbitmap)throwsIOException{
Stringfname=DateFormat.format("yyyyMMddhhmmss",newDate()).toString()+".jpg";
StringfilePath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode"
+File.separator+fname;
Filefile=newFile(filePath);
FileOutputStreamfileOutputStream=newFileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
//把图片加入到系统图库里面
MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
file.getAbsolutePath(),fname,null);
//uri得到的是文件的绝对路径
getApplicationContext().sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"+file.getAbsolutePath())));
edtText.setText(file.getAbsolutePath());
Toast.makeText(this,"生成成功",Toast.LENGTH_LONG).show();
}
//打开相册
privatevoidsetImage(){
//使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片
IntentgetAlbum=newIntent(Intent.ACTION_GET_CONTENT);
getAlbum.setType(IMAGE_TYPE);
startActivityForResult(getAlbum,IMAGE_CODE);
}
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(resultCode!=RESULT_OK){//此处的RESULT_OK是系统自定义得一个常量
Log.e("TAG->onresult","ActivityResultresultCodeerror");
return;
}
Bitmapbm=null;
//外界的程序访问ContentProvider所提供数据可以通过ContentResolver接口
ContentResolverresolver=getContentResolver();
//此处的用于判断接收的Activity是不是你想要的那个
if(requestCode==IMAGE_CODE){
try{
UrioriginalUri=data.getData();//获得图片的uri
bm=MediaStore.Images.Media.getBitmap(resolver,originalUri);
//显得到bitmap图片
imgView.setImageBitmap(bm);
//这里开始的第二部分,获取图片的路径:
String[]proj={MediaColumns.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
Cursorcursor=managedQuery(originalUri,proj,null,null,null);
//按我个人理解这个是获得用户选择的图片的索引值
intcolumn_index=cursor.getColumnIndexOrThrow(MediaColumns.DATA);
//将光标移至开头,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
Stringpath=cursor.getString(column_index);
edtText.setText(path);
btnOpen.setText(R.string.recognitionTwoCode);
}catch(IOExceptione){
Log.e("TAG-->Error",e.toString());
}
}
}
/**
*解析二维码图片里的内容
*@paramfilePath二维码图片的位置
*@throwsIOException
*@throwsNotFoundException
*/
privateStringreadImage(ImageViewimageView){
Stringcontent=null;
Map<DecodeHintType,String>hints=newHashMap<DecodeHintType,String>();
hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
//获得待解析的图片
Bitmapbitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
RGBLuminanceSourcesource=newRGBLuminanceSource(bitmap);
BinaryBitmapbitmap1=newBinaryBitmap(newHybridBinarizer(source));
QRCodeReaderreader=newQRCodeReader();
try{
Resultresult=reader.decode(bitmap1,hints);
//得到解析后的文字
content=result.getText();
}catch(Exceptione){
e.printStackTrace();
}
returncontent;
}
//ad布局部分 privateRelativeLayoutadContainer=null; privateIMvBannerAdbannerad=null; finalStringadSpaceid="这是你申请的广告ID号"; adContainer=(RelativeLayout)findViewById(R.id.adcontent); bannerad=Mvad.showBanner(adContainer,this,adSpaceid,false); bannerad.showAds(this);
月下载量上千次Android实现二维码生成器app源码大家不要错过呀!