在360上面上線了一個月,下載量上千余次。這里把代碼都分享出來,供大家學習哈!還包括教大家如何接入廣告,賺點小錢花花,喜歡的幫忙頂一個,大神見了勿噴,小學僧剛學Android沒多久。
首先介紹這款應用:APP是一款二維碼生成器,雖然如何制作二維碼教程網上有很多,我這里再嘮叨一下並把我的所有功能模塊代碼都分享出來。
既然是二維碼生成器那么我們如何制作二維碼呢?
這里主要用到了Google的Zxing開源包,可以到網上下載,也可以到我的csdn上面去下載http://download.csdn.net/detail/u014132820/8223437
在這里我們需要一個輔助類RGBLuminanceSource,這個類Google也提供了,我們直接粘貼過去就可以使用了
下面是我的代碼組織結構:
package com.njupt.liyao; import com.google.zxing.LuminanceSource; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.FileNotFoundException; /** * This class is used to help decode images from files which arrive as RGB data * from Android bitmaps. It does not support cropping or rotation. * * @author dswitkin@google.com (Daniel Switkin) */ 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; } }
下面這段代碼可以利用Zxing包將文本信息比如網址,文章生成一張照片,我這里返回一個Bitmap對象,因為后面還要將其寫入SD卡里面和顯示在ImageView上面。
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; }
通過上面這段代碼其實我們已經得到了存儲文本信息的二維碼圖片了,下面我們在二維碼寫到SD卡里面並且添加到系統相冊里面方便用戶調用(因為如果你只是寫入到SD卡里面是不能再系統相冊里面及時看到的)
在App啟動之前我們應該在SD卡里面創建一個文件夾保存這些二維碼:
public void createDirctoryToSaveImage(){ String dirPath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode"; File dirFile=new File(dirPath); if(!dirFile.exists()){ dirFile.mkdir(); } }
接下來是把圖片寫入到SD卡已及如何加入到系統圖庫里面(如何把圖片添加到系統相冊的幾種方法可以看我的這篇博客:http://www.cnblogs.com/BasilLee/p/4082450.html ):
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(); }
到了這一步我們已經完成了二維碼的生成已經保存,下面就是我們怎么點擊Button打開系統圖庫要用戶來選擇他所需解析的二維碼了:
點擊按鈕打開系統相冊我主要是通過以下兩個方法來實現的:
//打開相冊 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()); } } }
到了現在我們要做的就是把二維碼里面的內容解析出來了,要我們來看看如何解析的:
/** * 解析二維碼圖片里的內容 * @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; }
這個函數返回一個String類型也就是二維碼里面所含的文本信息了。
現在功能基本上已經完成了,現在可能大家會想着如何接入廣告賺點小錢了,畢竟程序猿沒妹子喜歡只能努力賺錢了,嘿嘿!下面我來介紹360的廣告平台的接入,其實代碼很簡單就幾行,但是呢,首先你得下載360的SDK還有創建廣告位,這里就以我的代碼里面為例了!
首先是360的SDK,就是MV_adsdk_v0.1.6.jar包
導入進去以后呢加入這幾行代碼就OK了:
//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);
這里面RelativeLayout容器是你想要廣告顯示的位置,記得上線時候把Mvad.showBanner最后置為false這樣才能顯示商業廣告這樣才能有收入啊,嘿嘿!
說了這么多,那到底是個什么效果呢?下面拿出你的手機掃一掃下面的二維吧下載試試吧~~~~~~大家下載后幫忙給個好評哈~~~程序猿何苦難為程序猿呢是吧~~嘿嘿
http://zhushou.360.cn/detail/index/soft_id/2212352 (微信掃描不能下載直接到360官網鏈接去下吧)
如果你和我一樣是學生黨還在考四六級那么我還有一款小app大家也可以下載使用,相信對大家四六級考試還是有幫助的:
http://zhushou.360.cn/detail/index/soft_id/2300203 (微信掃描不能下載直接到360官網鏈接去下吧)