標籤:orm tty state try 問題 .com nts and 旋轉
我們都知道一般的二微碼都是以不同形式顯示有的是登入,有的是網頁,其實二維碼只是一個字串。到這裡我們就有一個疑問那二維碼是這樣判斷是網站或者是登入功能的呢?
其實這些判斷部分,二維碼產生器早就給你完成了,我們只需匯入一個core-3.2.1.jar包就行。那接下來我們來做一個簡單的小案例,進一步瞭解二維碼。
一、建立項目
1、我們建一個名為"weima"的項目。
2、在該項目libs下匯入一個core-3.2.1.jar包(註::http://pan.baidu.com/s/1i5FiYXZ),你也可以在百度搜尋。
3、我們都發現常見的二維碼中間有個小圖片,所以我們先下載一張圖片放入res下的drawable—hdpi處。
二、布局檔案
1、首先我們添加一個EditText輸入框和一個Button按鈕,一個ImageView控制項用來顯示二維碼的表徵圖。
2、當我們在輸入框輸入完數字或網址時,點擊Button按鈕後,就會在ImageView控制項產生一個二維碼。
3、當我們添加EditText,可能由於版本問題會出現無法點擊介面,以下問題。
4、EditText由於版本問題所以要將版本調到19,調版本的在右上方安卓機器人表徵圖處。
activity_main.xml檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.weima.MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="38dp" android:layout_marginTop="33dp" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginRight="20dp" android:layout_marginTop="22dp" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/button1" android:layout_marginLeft="14dp" android:layout_marginTop="47dp" /> </RelativeLayout>
三、MainActivity的代碼
MainActivity.java檔案:
public class MainActivity extends Activity { private EditText ent; private Button btn; private ImageView img; private static final int IMAGE_HALFWIDTH = 35;//圖片寬度值大小 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ent=(EditText)findViewById(R.id.editText1); img=(ImageView)findViewById(R.id.imageView1); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //取出字串 String entText=ent.getText().toString(); Bitmap log=BitmapFactory.decodeResource(getResources(), R.drawable.weixintubiao); try { Bitmap bm=createCode(entText,log,BarcodeFormat.QR_CODE); img.setImageBitmap(bm); } catch (WriterException e) { e.printStackTrace(); } }}); } public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)throws WriterException { //Matrix,中文裡叫矩陣,在影像處理方面,主要是用於平面的縮放、平移、旋轉等操作。 Matrix mat=new Matrix(); float imgX=(float)2*IMAGE_HALFWIDTH/mBitmap.getWidth(); float imgY=(float)2*IMAGE_HALFWIDTH/mBitmap.getHeight(); mat.setScale(imgX,imgY);//設定縮放資訊 //將logo圖片按martix設定的資訊縮放 mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,mBitmap.getWidth(), mBitmap.getHeight(), mat, false); MultiFormatWriter writer=new MultiFormatWriter(); Hashtable htable=new Hashtable(); htable.put(EncodeHintType.CHARACTER_SET, "UTF-8");//設定字元編碼 //產生二維碼矩陣資訊 BitMatrix matrix = writer.encode(string, format, 800, 800, htable); int width=matrix.getWidth();//矩陣寬度 int height=matrix.getHeight();//矩陣高度 int matW=width/2; int matH=height/2; int[] pixels = new int[width * height];//定義數組長度為矩陣高度*矩陣寬度,用於記錄矩陣中像素資訊 for(int y=0;y<height;y++){//從行開始迭代矩陣 for(int x=0;x<width;x++)//迭代列 { if (x > matW - IMAGE_HALFWIDTH && x < matW + IMAGE_HALFWIDTH && y > matH - IMAGE_HALFWIDTH && y < matH + IMAGE_HALFWIDTH) { //該位置用於存放圖片資訊 //記錄圖片每個像素資訊 pixels[y * width + x] = mBitmap.getPixel(x - matW+ IMAGE_HALFWIDTH, y - matH + IMAGE_HALFWIDTH); } else { if (matrix.get(x, y)) { //如果有黑塊點,記錄資訊 pixels[y * width + x] = 0xff000000;//記錄黑塊資訊 } } } } Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888); // 通過像素數組產生bitmap bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
1、首先我們定義三個控制項變數,通過R檔案找到控制項ID進行相關的操作,為Button控制項添加的相關的onClick點擊事件。
2、通過調用系統的Bitmap類對來對圖片進行相關的操作。
3、通過createCode產生二維碼並嵌入表徵圖。
最終:
二維碼(android)