ZXingの使用

13199 ワード

クリックしてリンクを開けて原文のブロガーの無私な分かち合いに感謝します!
             ,         “  ”    !      Android                   ?
            zxing。 github      :https://github.com/zxing/zxing,               ,     
       demo,         。

QRコードの識別
 /**
 *        
 */
public class ScanCodeActivity extends Activity implements View.OnClickListener {
    private Button start_scan;
    private TextView result_tv;
    private final static int REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        this.start_scan = (Button) findViewById(R.id.start_scan);
        this.result_tv = (TextView) findViewById(R.id.result_tv);
        this.start_scan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.start_scan) {
            //zxing                ,    startActivityForResult  
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        }
    }

    /**
     *  onActivityResult     
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            this.result_tv.setText(scanResult);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
QRコードの生成:
/**
 *        
 */
public class MadeCodeActivity extends Activity implements View.OnClickListener {
    private EditText code_edt;
    private Button made_code;
    private ImageView result_iv;
    private CheckBox logo_cb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_made);
        this.code_edt = (EditText) findViewById(R.id.code_edt);
        this.made_code = (Button) findViewById(R.id.made_code);
        this.result_iv = (ImageView) findViewById(R.id.result_iv);
        logo_cb = (CheckBox) findViewById(R.id.logo_cb);
        this.made_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.made_code) {
            String content = code_edt.getText().toString().trim();
            boolean isAddLogo = logo_cb.isChecked();
            if (TextUtils.isEmpty(content)) {
                Toast.makeText(this, "        ", Toast.LENGTH_LONG).show();
                return;
            }
            madeCode(content, isAddLogo);
        }
    }

    /**
     *      
     *
     * @param content             
     * @param isAddLogo          LOGO  
     */
    private void madeCode(String content, boolean isAddLogo) {
        String bitmapPath;
        if (!isAddLogo) {//  LOGO
            bitmapPath = EncodeUtil.createQRImage(this, content, 300, 300, null);
        } else {// Logo,R.mipmap.ic_launcher  LOGO    ,    
            bitmapPath = EncodeUtil.createQRImage(this, content, 300, 300, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        }
        if (!TextUtils.isEmpty(bitmapPath)) {
            result_iv.setImageBitmap(BitmapFactory.decodeFile(bitmapPath));
        }
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
完了したプロジェクト(Android Studioプロジェクト)のダウンロード先:http://download.csdn.net/detail/true100/9487162