1. 急!!求一個android 平台的java做的二維碼解碼小程序!!
【一】、編碼:
QRCodeEncoderHandler.java
Java代碼
package michael.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
* 二維碼生成器
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class QRCodeEncoderHandler {
/**
* 生成二維碼(QRCode)圖片
* @param content
* @param imgPath
*/
public void encoderQRCode(String content, String imgPath) {
try {
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
System.out.println(content);
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(140, 140,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
// 設定圖像顏色> BLACK
gs.setColor(Color.BLACK);
// 設置偏移量 不設置可能導致解析出錯
int pixoff = 2;
// 輸出內容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 120) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = "
+ contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, "png", imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String imgPath = "D:/test/twocode/Michael_QRCode.png";
String content = "Hello 大大、小小,welcome to QRCode!"
+ " Myblog [ http://sjsky.iteye.com ]"
+ " EMail [ [email protected] ]" + " Twitter [ @suncto ]";
QRCodeEncoderHandler handler = new QRCodeEncoderHandler();
handler.encoderQRCode(content, imgPath);
System.out.println("encoder QRcode success");
}
}
運行後生成的二維碼圖片如下:
【二】、解碼:
QRCodeDecoderHandler.java
Java代碼
package michael.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class QRCodeDecoderHandler {
/**
* 解碼二維碼
* @param imgPath
* @return String
*/
public String decoderQRCode(String imgPath) {
// QRCode 二維碼圖片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String decodedData = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
decodedData = new String(decoder.decode(new J2SEImage(bufImg)));
// try {
// System.out.println(new String(decodedData.getBytes("gb2312"),
// "gb2312"));
// } catch (Exception e) {
// // TODO: handle exception
// }
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
QRCodeDecoderHandler handler = new QRCodeDecoderHandler();
String imgPath = "d:/test/twocode/Michael_QRCode.png";
String decoderContent = handler.decoderQRCode(imgPath);
System.out.println("解析結果如下:");
System.out.println(decoderContent);
System.out.println("========decoder success!!!");
}
class J2SEImage implements QRCodeImage {
BufferedImage bufImg;
public J2SEImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
public int getWidth() {
return bufImg.getWidth();
}
public int getHeight() {
return bufImg.getHeight();
}
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
}
}
運行結果如下(解碼出的內容和之前輸入的內容一致 ):
解析結果如下:
Hello 大大、小小,welcome to QRCode!
Myblog [ http://sjsky.iteye.com ]
EMail [ [email protected] ]
Twitter [ @suncto ]
========decoder success!!!
不明之處請參閱:http://www.2cto.com/kf/201108/98471.html
2. android開發二維碼掃一掃需要什麼許可權
你好,
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" /> <!-- 使用照相機許可權 -->
<uses-feature android:name="android.hardware.camera.autofocus" /> <!-- 自動聚焦許可權 -->
在基於6.0以上系統開發的時候 ,需要申請用代碼申請許可權
3. 如何製作一個屬於自己的二維碼並且掃描這個二維碼能顯示自己的名字
不知道你們老師有沒有給你們指定通過什麼方式或者說平台來做這個二維碼呢?如果沒有的話,推薦一款目前市面上比較好用的二維碼生成器,你就能自己來做這樣的一個二維碼了。
第一步:注冊賬戶登錄到操作後台。
根據你老師作業要求,你直接在編輯框內輸入名字即可,出來編輯文字以外,你還能上傳圖片,文檔,視頻,音頻等內容。保存後即可生成一個二維碼。掃碼後就能看到你之前放在裡面的所有內容,非常簡單便捷。
希望可以幫助到你,望採納!
4. android開發 如何實現掃描本地二維碼圖片
開源的二維碼掃描庫主要有zxing和zbar,zbar在iPos平台上應用比較成熟,而在Android平台上主流還是用zxing庫,因此這里主要講述如何利用zxing進行二維碼開發。
如何將zxing的Android源碼導入工程。
在導入zxing的android源碼之前,先去官方下載zxing的源碼http://code.google.com/p/zxing/downloads/list。
這里以1.6版本為例,zxing 1.6源碼結構如下:
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.qrcode"
android:versionCode="1"
android:versionName="1.0">
<uses-sdkandroid:minSdkVersion="7"/>
<uses-permissionandroid:name="android.permission.VIBRATE"/><!--震動許可權-->
<uses-permissionandroid:name="android.permission.CAMERA"/>
<uses-featureandroid:name="android.hardware.camera"/><!--使用照相機許可權-->
<uses-featureandroid:name="android.hardware.camera.autofocus"/><!--自動聚焦許可權-->
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
<activityandroid:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--隱藏鍵盤--><!--全屏-->
<activity
android:configChanges="orientation|keyboardHidden"
android:name="com.zxing.activity.CaptureActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
</application>
</manifest>
這種情況大致就可以實現二維碼掃描了,想細化的話,還可以多看看安卓二維碼掃描開發相關的教程