如何生成一些文本的二維QR碼,並且在android中有一個圖像在中心?我瀏覽了很多,但是我只找到了如何使用這個link使用ZXing庫來生成簡單的2-D QR碼。使用ZXing庫可以生成具有中心圖像的二維QR碼嗎?在android中生成設計器2d QR碼
2
A
回答
7
爲了中心對準的圖像使用代碼像在我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" >
<ImageView
android:id="@+id/myImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
要生成並在顯示一個類似的QR編碼圖像使用代碼我MainActivity.java:
public class MainActivity extends AppCompatActivity {
public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 400;
public final static int HEIGHT = 400;
public final static String STR = "A string to be encoded as QR code";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.myImage);
try {
Bitmap bitmap = encodeAsBitmap(STR);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;
}
}
在Android Studio將以下行添加到build.gradle
文件:
dependencies {
....
compile 'com.google.zxing:core:3.2.1'
}
或者 - 如果仍然使用使用的Eclipse ADT插件通過斑馬線到庫子目錄(這裏fullscreen)添加core.jar添加:
-1
對於在相同的情況下,他的興趣所在,探索this project
這不是我的代碼,但我已檢查它,它工作正常。
主要思路是在QRCodeUtil。這只是簡單的覆蓋。不幸的是,沒有提供理論限制。
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}
if (logo == null) {
return src;
}
//獲取圖片的寬高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
if (srcWidth == 0 || srcHeight == 0) {
return null;
}
if (logoWidth == 0 || logoHeight == 0) {
return src;
}
//logo大小爲二維碼整體大小的1/5
float scaleFactor = srcWidth * 1.0f/5/logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth/2, srcHeight/2);
canvas.drawBitmap(logo, (srcWidth - logoWidth)/2, (srcHeight - logoHeight)/2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
相關問題
- 1. 使用Java在Android中生成QR碼
- 2. 生成QR碼
- 3. 使用PHP QR碼生成器在PHP中創建QR碼與中心徽標
- 4. 如何在iPhone中生成QR碼?
- 5. 使用API構建QR碼生成器
- 6. 使用Excel VBA生成2D(PDF417或QR)條形碼
- 7. 使用codenameone生成QR碼
- 8. 如何使用碎片爲Android創建QR碼生成器
- 9. 如何設計生成Python代碼的代碼生成器
- 10. 組件設計器在後面的代碼中生成代碼
- 11. Qt設計器代碼生成問題
- 12. 在頁面加載生成QR碼
- 13. 如何在Android中創建自己的QR生成器?
- 14. 生成器設計模式
- 15. QR碼的Android
- 16. Android生成QR掃描圖像
- 17. 以編程方式生成QR代碼
- 18. 用Xamarin.Forms和Zxing生成QR碼
- 19. 用於生成QR碼的JavaScript API
- 20. 使用TAB鍵生成QR碼
- 21. QR代碼,可以用R生成?
- 22. 高速緩存生成的QR碼
- 23. 從Linux終端生成QR碼?
- 24. 生成阿拉伯文QR碼
- 25. Android:使用QRGen lib生成QR碼時出錯
- 26. 在iPhone和Android上生成二維條碼(例如QR碼,Data Matrix,PDF417)
- 27. 從iPhone到Mac的Xcode QR生成器
- 28. QR的QR碼在
- 29. 檢入設計器生成的代碼到TFS中,問題
- 30. 如何生成QR碼並在網頁中顯示頁面
亞歷克斯,TS被問及-QR圖像,而不是如何中心的圖像 – djdance 2017-03-09 18:00:18
不是我,而是TS - 談到疊加圖像,這裏是一個正確的觀念https://github.com/wshychbydh/Qrcode – djdance 2017-03-10 07:15:22