欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新

安卓中生成二维码和扫描二维码

来源: 开发者 投稿于  被查看 10026 次 评论:155

安卓中生成二维码和扫描二维码


  这是一个通用的方法,只需要调用一下这个链接就能将你传过去的内容生成二维码返回给你,代码如下:

<?$content = '你要生成的内容';
 generateQRfromGoogle(, ='300',='L',='0'
 =300 '<img src="http://chart.apis.google.com/chart?chs='..'x'..'&cht=qr&chld='..'|'..'&chl='..'" alt="QR code" widhtHeight="'..'" widhtHeight="'..'"/>'?>

   这个代码关键就在那个链接上,不管你使用的什么代码(我用的是php),只要将参数填写好然后获取这个链接的内容就能生成二维码,示例(http://chart.apis.google.com/chart?chs=85x85&cht=qr&chld=L|0&chl=http://jijie.cc/store/461):

  在安卓中只需要内容和大小尺寸设置好,然后把它当作一个图片进行获取就能生成二维码(不会获取网络图片的同学请看我的上一篇博客)。

生成二维码:

  Input  ImageView result= Button save= Button share= Button back== =====.getIntent().getStringExtra("content"=

= 100

        save.setOnClickListener( 
                 ByteArrayOutputStream baos = 100"DomobSDKDemo", "Interstitial Ad is not ready"

        share.setOnClickListener( 
="image/png"=  File("/mnt/sdcard/zibuyu/temp/temp.jpg"="share""二维码说说:一切尽在图片中!""sms_body","二维码说说:一切尽在图片中!""分享图片到:" 

     Bitmap Create2DCode(String str) 
=  MultiFormatWriter().encode( String(str.getBytes("GBK"),"ISO-8859-1"),BarcodeFormat.QR_CODE, 300, 300 width = height =
        [] pixels =  [width * ( y = 0; y < height; y++ ( x = 0; x < width; x++* width + x] = 0xff000000[] colors==
        bitmap.setPixels(pixels, 0, width, 0, 0
       Write( []b) == time==time+".png"= +"/zibuyu/" (!= ="图片保存在了内存卡下zibuyu文件夹下,请查看!"0, 0== 0= =  BufferedOutputStream(0

   Writetemp( []b) ===       Date(System.currentTimeMillis());
              String fileName ="temp.jpg"= +"/zibuyu/temp" (!= = =  BufferedOutputStream(0

( src ==  = w = 300 h = 300== 0, 0, 
    

  CaptureActivity  Activity   Vector<BarcodeFormat>     BEEP_VOLUME = 0.10f  Button back= 
        back=== =  InactivityTimer( 
 === = = = (audioService.getRingerMode() !== =   (handler != =    (handler == =  CaptureActivityHandler(, decodeFormats,"GBK"  surfaceChanged(SurfaceHolder holder,  format,   (!=  =               
         Intent intent="info",result.  (playBeep && mediaPlayer == 
= ==     VIBRATE_DURATION = 200L  (playBeep && mediaPlayer != =
      OnCompletionListener beepListener =  0

 

  上图:

 

工程文件:https://github.com/yimengqingqiu/makeqr (github)

邮箱:standopen@foxmail.com (有什么疑问或者好的修改建议可以可以联系我)

 

 

 

 

 

package zxing.standopen;
 
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
import zxing.moyan.index;
import cn.domob.android.ads.DomobAdEventListener;
import cn.domob.android.ads.DomobAdView;
import cn.domob.android.ads.DomobInterstitialAd;
import cn.domob.android.ads.DomobInterstitialAdListener;
import cn.domob.android.ads.DomobAdManager.ErrorCode;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
 
import ad.standopen.contact;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
public class Input extends Activity {
    private ImageView result=null;
    private Button save=null;
    private Button share=null;
    private Button back=null;
    Bitmap bmp=null;
    DomobInterstitialAd mInterstitialAd;
    RelativeLayout mAdContainer;
    DomobAdView mAdview320x50;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.input);
        save=(Button)findViewById(R.id.save);
        share=(Button)findViewById(R.id.share);
        result=(ImageView)findViewById(R.id.img);
        back=(Button)findViewById(R.id.back);
        String content=this.getIntent().getStringExtra("content");
        try {
            bmp=createBitmap(Create2DCode(content));
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        result.setImageBitmap(bmp);
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
 
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos1);
 
        try {
            Writetemp(baos1.toByteArray());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        //保存二维码
        save.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
 
                    if (mInterstitialAd.isInterstitialAdReady()){
                        mInterstitialAd.showInterstitialAd(Input.this);
                    } else {
                        Log.i("DomobSDKDemo", "Interstitial Ad is not ready");
                        mInterstitialAd.loadInterstitialAd();
                    }
                    
                    
                    
                    try {
                        Write(baos.toByteArray());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
        });
         
        //分享二维码
        share.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                  Intent intent=new Intent(Intent.ACTION_SEND);   
                   intent.setType("image/png");   
                   File f = new File("/mnt/sdcard/zibuyu/temp/temp.jpg");
                    Uri u = Uri.fromFile(f);
                   intent.putExtra(Intent.EXTRA_SUBJECT, "share");   
                   intent.putExtra(Intent.EXTRA_TEXT,"二维码说说:一切尽在图片中!");
                   intent.putExtra(Intent.EXTRA_STREAM, u);
                   intent.putExtra("sms_body","二维码说说:一切尽在图片中!");
                   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                   startActivity(Intent.createChooser(intent, "分享图片到:"));
            }
        });
       back.setOnClickListener(new View.OnClickListener() {
        
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
        
        
    }
     
    //生成二维码,生成二维码返回BITMAP(重点)
    public Bitmap Create2DCode(String str) throws WriterException, UnsupportedEncodingException {
        //生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        
        BitMatrix matrix = new MultiFormatWriter().encode(new String(str.getBytes("GBK"),"ISO-8859-1"),BarcodeFormat.QR_CODE, 300, 300);
        
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        //二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if(matrix.get(x, y)){
                    pixels[y * width + x] = 0xff000000;
                }
                
            }
        }    
        int[] colors={R.color.white};
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }
     
    //保存二维码
    public  void Write(byte []b) throws IOException
    {
        File cacheFile =null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File sdCardDir = Environment.getExternalStorageDirectory();
            
            long time=Calendar.getInstance().getTimeInMillis();
            String fileName =time+".png";
            File dir = new File(sdCardDir.getCanonicalPath()
                    +"/zibuyu/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            cacheFile = new File(dir, fileName);
        
        }   
        Toast toast = Toast.makeText(getApplicationContext(),
                "图片保存在了内存卡下zibuyu文件夹下,请查看!", Toast.LENGTH_LONG);
                 toast.setGravity(Gravity.CENTER, 0, 0);
                 LinearLayout toastView = (LinearLayout) toast.getView();
                 ImageView imageCodeProject = new ImageView(getApplicationContext());
                 imageCodeProject.setImageResource(R.drawable.fun);
                 toastView.addView(imageCodeProject, 0);
                 toast.show();
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
            
                bos.write(b,0,b.length);
                bos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     
    public  void Writetemp(byte []b) throws IOException
      {
          File cacheFile =null;
          if (Environment.getExternalStorageState().equals(
                  Environment.MEDIA_MOUNTED)) {
              File sdCardDir = Environment.getExternalStorageDirectory();
              Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间
              String fileName ="temp.jpg";
              File dir = new File(sdCardDir.getCanonicalPath()
                      +"/zibuyu/temp");
              if (!dir.exists()) {
                  dir.mkdirs();
              }
              cacheFile = new File(dir, fileName);
          
          }   
          BufferedOutputStream bos = null;
          try {
              bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
              
                  bos.write(b,0,b.length);
                  bos.close();
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
     
    private Bitmap createBitmap( Bitmap src)
    {
    if( src == null )
    {
    return null;
    }
    Paint paint=new Paint();
    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
    
    int w = 300;
    int h = 300;
    Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
    Canvas cv = new Canvas( newb );
 
    cv.drawColor(Color.WHITE);
 
    cv.drawBitmap(src, 0, 0, null );
    cv.save( Canvas.ALL_SAVE_FLAG );
    cv.restore();//存储
    return newb;
 
    }
 
     
   
}

用户评论