月下载量上千次Android实现二维码生成器app源码分享

在360上面上线了一个月,下载量上千余次。这里把代码都分享出来,供大家学习哈!还包括教大家如何接入广告,赚点小钱花花,喜欢的帮忙顶一个,大神见了勿喷,小学僧刚学Android没多久。首先介绍这款应用:APP是一款二维码生成器,虽然如何制作二维码教程网上有很多,我这里再唠叨一下并把我的所有功能模块代码都分享出来。

在这里我们需要一个辅助类RGBLuminanceSource,这个类Google也提供了,我们直接粘贴过去就可以使用了

package com.njupt.liyao;

import com.google.zxing.LuminanceSource; 

import android.graphics.Bitmap;
import android.graphics.BitmapFactory; 

import java.io.FileNotFoundException; 

public final class RGBLuminanceSource extends LuminanceSource { 

 private final byte[] luminances; 

 public RGBLuminanceSource(String path) throws FileNotFoundException {
this(loadBitmap(path));
}

 public RGBLuminanceSource(Bitmap bitmap) {
 super(bitmap.getWidth(), bitmap.getHeight()); 

 int width = bitmap.getWidth();
 int height = bitmap.getHeight();
 int[] pixels = new int[width * height];
 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 

 // In order to measure pure decoding speed, we convert the entire image
 // to a greyscale array
 // up front, which is the same as the Y channel of the
 // YUVLuminanceSource in the real app.
 luminances = new byte[width * height];
 for (int y = 0; y < height; y++) {
 int offset = y * width;
 for (int x = 0; x < width; x++) {
 int pixel = pixels[offset + x];
 int r = (pixel >> 16) & 0xff;
 int g = (pixel >> 8) & 0xff;
 int b = pixel & 0xff;
 if (r == g && g == b) {
 // Image is already greyscale, so pick any channel.
 luminances[offset + x] = (byte) r;
 } else {
 // Calculate luminance cheaply, favoring green.
 luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
}
}
}
}

@Override
 public byte[] getRow(int y, byte[] row) {
 if (y < 0 || y >= getHeight()) {
 throw new IllegalArgumentException(
"Requested row is outside the image:"+ y);
}
 int width = getWidth();
 if (row == null || row.length < width) {
 row = new byte[width];
}

 System.arraycopy(luminances, y * width, row, 0, width);
 return row;
}

 // Since this class does not support cropping, the underlying byte array
 // already contains
 // exactly what the caller is asking for, so give it to them without a copy.
@Override
 public byte[] getMatrix() {
 return luminances;
}

 private static Bitmap loadBitmap(String path) throws FileNotFoundException {
 Bitmap bitmap = BitmapFactory.decodeFile(path);
 if (bitmap == null) {
 throw new FileNotFoundException("Couldn't open"+ path);
}
 return bitmap;
}

}
public Bitmap getTwoDimensionPicture(String text,int width,int height) throws WriterException{
if(text.equals(""))
{
 text="";
}
 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
 hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
 BitMatrix bitMatrix = new QRCodeWriter().encode(text,
 BarcodeFormat.QR_CODE, width, height, hints);
 int []pixels = new int[width*height];
 for(int y=0;y<height;y++){
 for(int x=0;x<width;x++){
 if (bitMatrix.get(x, y))
{
 pixels[y * width + x] = BLACK;
}
else
{
 pixels[y * width + x] = WHITE;
}
}
}
 Bitmap bitmap=Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
 bitmap.setPixels(pixels, 0,width, 0, 0, width, height);

 return bitmap;
}
public void createDirctoryToSaveImage(){
 String dirPath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode";
 File dirFile=new File(dirPath);
if(!dirFile.exists()){
dirFile.mkdir();
}
}
public void writeBitMapToSDCard(Bitmap bitmap) throws IOException{
 String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";
 String filePath=Environment.getExternalStorageDirectory()+File.separator+"TowDimensionCode"
+File.separator+fname;
 File file=new File(filePath);
 FileOutputStream fileOutputStream=new FileOutputStream(file);
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
//把图片加入到系统图库里面
MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
 file.getAbsolutePath(), fname, null);
//uri得到的是文件的绝对路径
 getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"+file.getAbsolutePath())));
edtText.setText(file.getAbsolutePath());
 Toast.makeText(this,"生成成功", Toast.LENGTH_LONG).show();
}
//打开相册
 private void setImage() {
//使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片
 Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType(IMAGE_TYPE);
 startActivityForResult(getAlbum, IMAGE_CODE);
}

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data){
 if (resultCode != RESULT_OK) { //此处的 RESULT_OK 是系统自定义得一个常量
 Log.e("TAG->onresult","ActivityResult resultCode error");
return;
}
 Bitmap bm = null;
 //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
 ContentResolver resolver = getContentResolver();
//此处的用于判断接收的Activity是不是你想要的那个
 if (requestCode == IMAGE_CODE) {
 try {
 Uri originalUri = data.getData(); //获得图片的uri
 bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
//显得到bitmap图片
imgView.setImageBitmap(bm);
//这里开始的第二部分,获取图片的路径:
 String[] proj = {MediaColumns.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
 Cursor cursor = managedQuery(originalUri, proj, null, null, null);
 //按我个人理解 这个是获得用户选择的图片的索引值
 int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
 //将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
 String path = cursor.getString(column_index);
edtText.setText(path);
btnOpen.setText(R.string.recognitionTwoCode);
 }catch (IOException e) {
Log.e("TAG-->Error",e.toString());
}
}
}
/**
 * 解析二维码图片里的内容
 * @param filePath 二维码图片的位置
 * @throws IOException
 * @throws NotFoundException
*/
 private String readImage(ImageView imageView) {
 String content = null;
 Map<DecodeHintType, String> hints = new HashMap<DecodeHintType, String>();
 hints.put(DecodeHintType.CHARACTER_SET,"utf-8");
 // 获得待解析的图片
 Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
 RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
 BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
 QRCodeReader reader = new QRCodeReader();
 try {
 Result result = reader.decode(bitmap1, hints);
 // 得到解析后的文字
 content = result.getText();
 } catch (Exception e) {
e.printStackTrace();
}
 return content;
}
//ad布局部分
 private RelativeLayout adContainer = null;
 private IMvBannerAd bannerad = null;
final String adSpaceid ="这是你申请的广告ID号";
adContainer=(RelativeLayout)findViewById(R.id.adcontent);
 bannerad = Mvad.showBanner(adContainer, this, adSpaceid, false);
bannerad.showAds(this);

月下载量上千次Android实现二维码生成器app源码大家不要错过呀!

(0)

相关推荐

  • 使用Zxing实现二维码生成器内嵌图片

    使用Zxing实现二维码生成器内嵌图片,具有一定的参考价值,具体如下: 基本思路是先使用zxing生成的二维码图片,然后读取图片,在其中插入图标,然后整个输出图片. 最近的项目中需要生成二维码,找了几个例子综合下,做出了最后的效果,二维码可以生成图片格式(jpg等)或者在web页面上显示,此片文章仅作记录,雷同之处多多,包涵.... 注:需要Zxing包装的工具类,大概的流程是读取内嵌的图片,将内容转化成二维码,将图片内嵌到二维码中,出图. 下面是完整代码: import Java.awt.Ba

  • iOS 二维码生成及扫码详解及实例代码

    iOS二维码生成及扫码 现在越来越多的应用加入二维码相关的业务,在iOS开发市场上很多开发人员都在使用第三方的扫码与生成二维码的控件,个人认为此类的第三方控件识别度不高.最近正好整理新框架的事情,研究了一下.具体代码如下 生成二维码代码 /** * @author 半 饱, 15-12-18 * * @brief 生成二维码图片 * * @param code 生成二维码图片内容 * @param width 二维码图片宽度 * @param height 二维码图片高度 * * @return

  • google提供二维码生成器

    google提供二维码生成器 其实就是参考下面的传参方法 复制代码 代码如下: http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=http://www.jb51.net&choe=UTF-8 具体看下面二维码的图片路径.

  • PHP微信开发之二维码生成类

    <?php /** * Created by PhpStorm. * User: bin * Date: 15-1-16 * Time: 上午9:48 */ namespace Home\Common; // 微信处理类 set_time_limit(30); class Weixin{ //构造方法 static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?"; static $token_url

  • 详解二维码生成工厂

    本次主要分享的是3个免费的二维码接口的对接代码和测试得出的注意点及区别,有更好处理方式多多交流,相互促进进步:最近在学习JavsScript的扩展TypeScript,感觉语法糖很甜,大部分与C#更为类似,可能都是微软项目的原因吧,有兴趣的朋友可以多多相互交流下: 以上是个人的看法,下面来正式分享今天的文章吧: Google的Api二维码生成接口 2d-code的Api二维码生成接口 topscan的Api二维码生成接口 使用面向对象+加载程序集创建对象合并以上接口封装成二维码生成工厂 下面一步

  • 批处理制作二维码生成器

    这个程序不能直接支持 Unicode, 同样不能直接支持任何双字节或多字节字符(包括汉字), 但可以用十六进制转码的方式生成包含 Unicode (或其他任何编码)字符的二维码图形. 如果数据含有UTF-8 Unicode 字符时, 在数据头部加上 BOM (\xEF\xBB\xBF) 即可. 例如: \xEF\xBB\xBF\xE6\xB1\x89\xE5\xAD\x97 上面的代码表示中文字符 "汉字" 任何 ASCII 字符(\x00 到 \xFF)都可以用十六进制转码方式输入,

  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法. (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成[QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式] * @param string $data 二维码包含的信息,可以是数字.字符.二进制信息.汉字.不能混合数据类型,数据必须经过UTF-8 URL-encoded.如果需要传递的信息超过2K个字节,请使用POST方式 * @param int $widhtHeig

  • Python实现的二维码生成小软件

    前几天,我估摸着做一个能生成QR Code小程序,并能用wxPython在屏幕上显示出来.当然,我想用纯Python实现,观望了一会后,我找到了三个候选: github 上的 python-qrcode sourceforge上的 pyqrcode Goolge code 上的 pyqrnative 我尝试了python-qrcode以及pyqrnative,因为它们能够运行在Windows/Mac/Linux.也不需要依赖额外的其他库除了Python图像库.pyqrcode项目需要其他一些先决

  • c#二维码生成的代码分享

    复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 调用外网API 生成二维码  周祥 2013年11月12日10:54:38/// </summary>public class Qr{    public Qr()    {        //        //TODO: 在此处添加构造函数逻辑        /

  • Python二维码生成库qrcode安装和使用示例

    二维码简称 QR Code(Quick Response Code),学名为快速响应矩阵码,是二维条码的一种,由日本的 Denso Wave公司于 1994 年发明.现随着智能手机的普及,已广泛应用于平常生活中,例如商品信息查询.社交好友互动.网络地址访问等等. 安装 Python 的二维码库 -- qrcode 由于生成 qrcode 图片需要依赖 Python 的图像库,所以需要先安装 Python 图像库 PIL(Python Imaging Library),不然会遇到 "ImportE

  • java实现二维码生成的几个方法(推荐)

    java实现二维码生成的几个方法,具体如下: 1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode/downloads/28391/qrcode.zip 这个是日本人写的,生成的是我们常见的方形的二维码 可以用中文 如:5677777ghjjjjj 2: 使用BarCode4j生成条形码和二维码 BarCode4j网址:http://sourcefor

随机推荐