生成QR code在Android的屏幕图像一 [英] Generate a image of QRCode in Android Screen
本文介绍了生成QR code在Android的屏幕图像一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在屏幕上显示的图像。
扫描QR code后,我需要显示在我的应用程序图像如何去了解它。
我已经试过这一点,但没有得到输出
酒吧codeFormat酒吧codeFormat =酒吧codeFormat.QR_ code;
INT width0 = 500;
INT height0 = 500; INT colorBack = 0xFF000000;
诠释colorFront = 0xFFFFFFFF的; QR codeWriter作家=新的QR codeWriter();
尝试
{
哈希表<恩codeHintType,对象>提示=新的Hashtable<恩codeHintType,对象>(2);
hint.put(恩codeHintType.CHARACTER_SET,UTF-8);
ByteMatrix bitMatrix = writer.en code(FNAME,酒吧codeFormat,width0,height0,暗示);
INT宽度= bitMatrix.getWidth();
INT高度= bitMatrix.getHeight();
INT [] =像素新INT [宽*高]。
对于(INT Y = 0; Y<高度; Y ++)
{
诠释偏移量= Y *宽度;
对于(INT X = 0; X<宽度; X ++)
{ //如果(bitMatrix.get(X,Y))
像素[偏移+ X] = colorBack;
//其他
//像素[偏移+ X] = colorFront;
}
} 位图的位图= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
bitmap.setPixels(像素,0,宽度,0,0,宽度,高度);
ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView1);
imageview.setImageBitmap(位图);
}赶上(WriterException E){
// TODO自动生成catch块
e.printStackTrace();
}
解决方案
德code QR code:
意向意图=新意图(com.google.zxing.client.android.SCAN);
intent.putExtra(SCAN_MODE,QR_ code_MODE);
startActivityForResult(意向,REQUEST_BAR code);
吐司面包= Toast.makeText(这一点,开始扫描QR code,Toast.LENGTH_SHORT);
toast.show();
结果
然后连接code它来获取OutputStream的图像:
公共无效generateQR code(INT宽度,高度INT,字符串内容的OutputStream OutputStream的)抛出异常{
字符串的imageformat = IMAGE_FORMAT;
BitMatrix bitMatrix =新的QR codeWriter()带code(内容,酒吧codeFormat.QR_ code,宽,高)。
MatrixToImageWriter.writeToStream(bitMatrix,的imageformat,OutputStream的);
}
OR
字节[] B = {0x48,×45,0x4C,0x4C,0x4F};
//字节数组转换成ISO-8859-1字符串
字符串数据;
尝试{
数据=新的String(二,ISO-8859-1);
}
赶上(UnsupportedEncodingException五){
//程序不应该能够到这里
返回;
}//获取的数据的字节矩阵
ByteMatrix矩阵;
com.google.zxing.Writer作家=新的QR codeWriter();
尝试{
矩阵= writer.en code(数据,com.google.zxing.Bar codeFormat.QR_ code,宽,高);
}
赶上(com.google.zxing.WriterException E){
//退出方法
返回;
}//生成从字节矩阵的图象
INT宽度= matrix.getWidth();
INT高度= matrix.getHeight();字节[] []数组= matrix.getArray();//创建缓冲的图像绘制到
BufferedImage的图像=新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);//通过矩阵迭代并绘制像素的图像
对于(INT Y = 0; Y<高度; Y ++){
为(中间体X = 0; X&下;宽度; X ++){
INT grayValue =阵列[Y] [X]&安培; 0xFF的;
image.setRGB(X,Y,(grayValue == 0?0:0XFFFFFF));
}
}//写入图像输出流
ImageIO.write(形象,PNG,OutputStream的);
编辑:参考帖子为从照相机和显示器在活动捕获图像
"I want to display the image on screen." After scanning the QR code I need to show that as image in my application how to go about it.
I have tried this but not getting output
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
int width0 = 500;
int height0 = 500;
int colorBack = 0xFF000000;
int colorFront = 0xFFFFFFFF;
QRCodeWriter writer = new QRCodeWriter();
try
{
Hashtable<EncodeHintType, Object> hint = new Hashtable<EncodeHintType,Object>(2);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
ByteMatrix bitMatrix = writer.encode(fName, barcodeFormat, width0, height0, hint);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++)
{
int offset = y * width;
for (int x = 0; x < width; x++)
{
// if (bitMatrix.get(x, y))
pixels[offset + x] = colorBack;
//else
//pixels[offset + x] = colorFront;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
解决方案
Decode qrcode:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, REQUEST_BARCODE);
Toast toast = Toast.makeText(this, "Start scanning QR code", Toast.LENGTH_SHORT);
toast.show();
and then encode it to get the image in outputstream:
public void generateQRCode(int width, int height, String content, OutputStream outputStream) throws Exception {
String imageFormat = IMAGE_FORMAT;
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE,width, height);
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, outputStream);
}
OR
byte[] b = {0x48, 0x45, 0x4C, 0x4C, 0x4F};
//convert the byte array into a ISO-8859-1 string
String data;
try {
data = new String(b, "ISO-8859-1");
}
catch (UnsupportedEncodingException e) {
//the program shouldn't be able to get here
return;
}
//get a byte matrix for the data
ByteMatrix matrix;
com.google.zxing.Writer writer = new QRCodeWriter();
try {
matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height);
}
catch (com.google.zxing.WriterException e) {
//exit the method
return;
}
//generate an image from the byte matrix
int width = matrix.getWidth();
int height = matrix.getHeight();
byte[][] array = matrix.getArray();
//create buffered image to draw to
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//iterate through the matrix and draw the pixels to the image
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int grayValue = array[y][x] & 0xff;
image.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
}
}
//write the image to the output stream
ImageIO.write(image, "png", outputStream);
Edit: refer post for "Capture Image from Camera and Display in Activity".
这篇关于生成QR code在Android的屏幕图像一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文