利用Android怎么實現(xiàn)一個二維碼生成功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

1.在project的build.gradle添加如下代碼:
allprojects {
repositories {
maven {
url 'https://jitpack.io'
}
}
}2.在build.gradle添加依賴:
dependencies {
compile 'com.github.goodboy321:Scan-Zxing:1.0'
}布局:
<EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入內(nèi)容,生成二維碼" android:text="http://www.baidu.com" /> <Button android:background="@color/colorAccent" android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二維碼" /> <Button android:layout_marginTop="10dp" android:background="@color/colorPrimaryDark" android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="掃碼(識別相冊中二維碼)" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/image_callback" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" />
主方法:
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
Intent intent = new Intent(mContext, CaptureActivity.class);
startActivityForResult(intent, REQUEST);
break;
case R.id.btn2:
image.setVisibility(View.VISIBLE);
//隱藏掃碼結(jié)果view
imageCallback.setVisibility(View.GONE);
String content = et.getText().toString().trim();
Bitmap bitmap = null;
try {
bitmap = BitmapUtils.create2DCode(content);//根據(jù)內(nèi)容生成二維碼
tvResult.setVisibility(View.GONE);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST) {
image.setVisibility(View.GONE);
imageCallback.setVisibility(View.VISIBLE);
String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);
if(bitmap != null){
imageCallback.setImageBitmap(bitmap);//現(xiàn)實掃碼圖片
}
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
文章標題:利用Android怎么實現(xiàn)一個二維碼生成功能-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article6/cedeog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、外貿(mào)建站、移動網(wǎng)站建設、面包屑導航、服務器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容