在360上面上线了一个月,下载量上千余次。这里把代码都分享出来,供大家学习哈!还包括教大家如何接入广告,赚点小钱花花,喜欢的帮忙顶一个,大神见了勿喷,小学僧刚学android没多久。首先介绍这款应用:app是一款二维码生成器,虽然如何制作二维码教程网上有很多,我这里再唠叨一下并把我的所有功能模块代码都分享出来。
在这里我们需要一个辅助类rgbluminancesource,这个类google也提供了,我们直接粘贴过去就可以使用了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package com.njupt.liyao; import com.google.zxing.luminancesource; import android.graphics.bitmap; import android.graphics.bitmapfactory; import java.io.filenotfoundexception; public final class rgbluminancesource extends luminancesource { private final byte [] luminances; public rgbluminancesource(string path) throws filenotfoundexception { this (loadbitmap(path)); } public rgbluminancesource(bitmap bitmap) { super (bitmap.getwidth(), bitmap.getheight()); int width = bitmap.getwidth(); int height = bitmap.getheight(); int [] pixels = new int [width * height]; bitmap.getpixels(pixels, 0 , width, 0 , 0 , width, height); // in order to measure pure decoding speed, we convert the entire image // to a greyscale array // up front, which is the same as the y channel of the // yuvluminancesource in the real app. luminances = new byte [width * height]; for ( int y = 0 ; y < height; y++) { int offset = y * width; for ( int x = 0 ; x < width; x++) { int pixel = pixels[offset + x]; int r = (pixel >> 16 ) & 0xff ; int g = (pixel >> 8 ) & 0xff ; int b = pixel & 0xff ; if (r == g && g == b) { // image is already greyscale, so pick any channel. luminances[offset + x] = ( byte ) r; } else { // calculate luminance cheaply, favoring green. luminances[offset + x] = ( byte ) ((r + g + g + b) >> 2 ); } } } } @override public byte [] getrow( int y, byte [] row) { if (y < 0 || y >= getheight()) { throw new illegalargumentexception( "requested row is outside the image:" + y); } int width = getwidth(); if (row == null || row.length < width) { row = new byte [width]; } system.arraycopy(luminances, y * width, row, 0 , width); return row; } // since this class does not support cropping, the underlying byte array // already contains // exactly what the caller is asking for, so give it to them without a copy. @override public byte [] getmatrix() { return luminances; } private static bitmap loadbitmap(string path) throws filenotfoundexception { bitmap bitmap = bitmapfactory.decodefile(path); if (bitmap == null ) { throw new filenotfoundexception( "couldn't open" + path); } return bitmap; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public bitmap gettwodimensionpicture(string text, int width, int height) throws writerexception{ if (text.equals( "" )) { text= "" ; } hashtable<encodehinttype, string> hints = new hashtable<encodehinttype, string>(); hints.put(encodehinttype.character_set, "utf-8" ); bitmatrix bitmatrix = new qrcodewriter().encode(text, barcodeformat.qr_code, width, height, hints); int []pixels = new int [width*height]; for ( int y= 0 ;y<height;y++){ for ( int x= 0 ;x<width;x++){ if (bitmatrix.get(x, y)) { pixels[y * width + x] = black; } else { pixels[y * width + x] = white; } } } bitmap bitmap=bitmap.createbitmap(width, height,bitmap.config.argb_8888); bitmap.setpixels(pixels, 0 ,width, 0 , 0 , width, height); return bitmap; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public void createdirctorytosaveimage(){ string dirpath=environment.getexternalstoragedirectory()+file.separator+ "towdimensioncode" ; file dirfile= new file(dirpath); if (!dirfile.exists()){ dirfile.mkdir(); } } public void writebitmaptosdcard(bitmap bitmap) throws ioexception{ string fname = dateformat.format( "yyyymmddhhmmss" , new date()).tostring()+ ".jpg" ; string filepath=environment.getexternalstoragedirectory()+file.separator+ "towdimensioncode" +file.separator+fname; file file= new file(filepath); fileoutputstream fileoutputstream= new fileoutputstream(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( new intent(intent.action_media_scanner_scan_file, uri.parse( "file://" +file.getabsolutepath()))); edttext.settext(file.getabsolutepath()); toast.maketext( this , "生成成功" , toast.length_long).show(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//打开相册 private void setimage() { //使用intent调用系统提供的相册功能,使用startactivityforresult是为了获取用户选择的图片 intent getalbum = new intent(intent.action_get_content); getalbum.settype(image_type); startactivityforresult(getalbum, image_code); } @override protected void onactivityresult( int requestcode, int resultcode, intent data){ if (resultcode != result_ok) { //此处的 result_ok 是系统自定义得一个常量 log.e( "tag->onresult" , "activityresult resultcode error" ); return ; } bitmap bm = null ; //外界的程序访问contentprovider所提供数据 可以通过contentresolver接口 contentresolver resolver = getcontentresolver(); //此处的用于判断接收的activity是不是你想要的那个 if (requestcode == image_code) { try { uri originaluri = data.getdata(); //获得图片的uri bm = mediastore.images.media.getbitmap(resolver, originaluri); //显得到bitmap图片 imgview.setimagebitmap(bm); //这里开始的第二部分,获取图片的路径: string[] proj = {mediacolumns.data}; //好像是android多媒体数据库的封装接口,具体的看android文档 cursor cursor = managedquery(originaluri, proj, null , null , null ); //按我个人理解 这个是获得用户选择的图片的索引值 int column_index = cursor.getcolumnindexorthrow(mediacolumns.data); //将光标移至开头 ,这个很重要,不小心很容易引起越界 cursor.movetofirst(); //最后根据索引值获取图片路径 string path = cursor.getstring(column_index); edttext.settext(path); btnopen.settext(r.string.recognitiontwocode); } catch (ioexception e) { log.e( "tag-->error" ,e.tostring()); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * 解析二维码图片里的内容 * @param filepath 二维码图片的位置 * @throws ioexception * @throws notfoundexception */ private string readimage(imageview imageview) { string content = null ; map<decodehinttype, string> hints = new hashmap<decodehinttype, string>(); hints.put(decodehinttype.character_set, "utf-8" ); // 获得待解析的图片 bitmap bitmap = ((bitmapdrawable) imageview.getdrawable()).getbitmap(); rgbluminancesource source = new rgbluminancesource(bitmap); binarybitmap bitmap1 = new binarybitmap( new hybridbinarizer(source)); qrcodereader reader = new qrcodereader(); try { result result = reader.decode(bitmap1, hints); // 得到解析后的文字 content = result.gettext(); } catch (exception e) { e.printstacktrace(); } return content; } |
1
2
3
4
5
6
7
|
//ad布局部分 private relativelayout adcontainer = null ; private imvbannerad bannerad = null ; final string adspaceid = "这是你申请的广告id号" ; adcontainer=(relativelayout)findviewbyid(r.id.adcontent); bannerad = mvad.showbanner(adcontainer, this , adspaceid, false ); bannerad.showads( this ); |
月下载量上千次android实现二维码生成器app源码大家不要错过呀!