如何将扫描的QR码图像保存到Android的图像视图中?

如何将扫描的QR码图像保存到Android的图像视图中?,第1张

概述我使用Zxing代码进行QR码读取.它工作正常.我的问题是如何将扫描的QR码显示在图像视图中.请帮我.提前致谢.解决方法:Android清单:<uses-sdkandroid:minSdkVersion="15"/> <applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"

我使用Zxing代码进行QR码读取.它工作正常.

我的问题是如何将扫描的QR码显示在图像视图中.

请帮我.

提前致谢.

解决方法:

Android清单:

<uses-sdk androID:minSdkVersion="15" />

 

<application    androID:icon="@drawable/ic_launcher"    androID:label="@string/app_name"    androID:theme="@androID:style/theme.Holo.light">    <activity        androID:name=".GenerateQRCodeActivity"        androID:label="@string/app_name" >        <intent-filter>            <action androID:name="androID.intent.action.MAIN" />            <category androID:name="androID.intent.category.LAUNCHER" />        </intent-filter>    </activity></application>

AndroID布局 – main.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content" androID:text="input some text here ..."  androID:textAppearance="?androID:attr/textAppearanceMedium" /> <EditText androID:ID="@+ID/qrinput" androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content" androID:ems="10">  <requestFocus /> </EditText> <button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content" androID:text="Generate QR Code" /> <ImageVIEw androID:ID="@+ID/imageVIEw1" androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content" /></linearLayout>

AndroID Activity – GenerateQRCodeActivity.java

package com.as400samplecode;import com.Google.zxing.barcodeFormat;import com.Google.zxing.WriterException;import androID.app.Activity;import androID.graphics.Bitmap;import androID.graphics.Point;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.display;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.WindowManager;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.ImageVIEw;public class GenerateQRCodeActivity extends Activity implements OnClickListener{ private String LOG_TAG = "GenerateQRCode"; @OverrIDe public voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.main);  button button1 = (button) findVIEwByID(R.ID.button1);  button1.setonClickListener(this); } public voID onClick(VIEw v) {  switch (v.getID()) {  case R.ID.button1:   EditText qrinput = (EditText) findVIEwByID(R.ID.qrinput);   String qrinputText = qrinput.getText().toString();   Log.v(LOG_TAG, qrinputText);   //Find screen size   WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);   display display = manager.getDefaultdisplay();   Point point = new Point();   display.getSize(point);   int wIDth = point.x;   int height = point.y;   int smallerDimension = wIDth < height ? wIDth : height;   smallerDimension = smallerDimension * 3/4;   //Encode with a QR Code image   QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrinputText,             null,             Contents.Type.TEXT,              barcodeFormat.QR_CODE.toString(),             smallerDimension);   try {    Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();    ImageVIEw myImage = (ImageVIEw) findVIEwByID(R.ID.imageVIEw1);    myImage.setimageBitmap(bitmap);   } catch (WriterException e) {    e.printstacktrace();   }   break;   // More buttons go here (if any) ...  } }}

QRCodeEncoder.java的来源

package com.as400samplecode;import androID.provIDer.ContactsContract;import androID.graphics.Bitmap;import androID.os.Bundle;import androID.telephony.PhoneNumberUtils;import java.util.Collection;import java.util.EnumMap;import java.util.HashSet;import java.util.Map;import com.Google.zxing.barcodeFormat;import com.Google.zxing.EncodeHintType;import com.Google.zxing.MultiFormatWriter;import com.Google.zxing.WriterException;import com.Google.zxing.common.BitMatrix;public final class QRCodeEncoder {    private static final int WHITE = 0xFFFFFFFF;    private static final int BLACK = 0xFF000000;    private int dimension = Integer.MIN_VALUE;    private String contents = null;    private String displayContents = null;    private String Title = null;    private barcodeFormat format = null;    private boolean encoded = false;    public QRCodeEncoder(String data, Bundle bundle, String type, String format, int dimension) {        this.dimension = dimension;        encoded = encodeContents(data, bundle, type, format);    }    public String getContents() {        return contents;    }    public String getdisplayContents() {        return displayContents;    }    public String getTitle() {        return Title;    }    private boolean encodeContents(String data, Bundle bundle, String type, String formatString) {        // Default to QR_CODE if no format given.        format = null;        if (formatString != null) {            try {                format = barcodeFormat.valueOf(formatString);            } catch (IllegalArgumentException iae) {                // Ignore it then            }        }        if (format == null || format == barcodeFormat.QR_CODE) {            this.format = barcodeFormat.QR_CODE;            encodeQRCodeContents(data, bundle, type);        } else if (data != null && data.length() > 0) {            contents = data;            displayContents = data;            Title = "Text";        }        return contents != null && contents.length() > 0;    }    private voID encodeQRCodeContents(String data, Bundle bundle, String type) {        if (type.equals(Contents.Type.TEXT)) {            if (data != null && data.length() > 0) {                contents = data;                displayContents = data;                Title = "Text";            }        } else if (type.equals(Contents.Type.EMAIL)) {            data = trim(data);            if (data != null) {                contents = "mailto:" + data;                displayContents = data;                Title = "E-Mail";            }        } else if (type.equals(Contents.Type.PHONE)) {            data = trim(data);            if (data != null) {                contents = "tel:" + data;                displayContents = PhoneNumberUtils.formatNumber(data);                Title = "Phone";            }        } else if (type.equals(Contents.Type.SMS)) {            data = trim(data);            if (data != null) {                contents = "sms:" + data;                displayContents = PhoneNumberUtils.formatNumber(data);                Title = "SMS";            }        } else if (type.equals(Contents.Type.CONTACT)) {            if (bundle != null) {                StringBuilder newContents = new StringBuilder(100);                StringBuilder newdisplayContents = new StringBuilder(100);                newContents.append("MECARD:");                String name = trim(bundle.getString(ContactsContract.Intents.Insert.name));                if (name != null) {                    newContents.append("N:").append(escapeMECARD(name)).append(';');                    newdisplayContents.append(name);                }                String address = trim(bundle.getString(ContactsContract.Intents.Insert.POSTAL));                if (address != null) {                    newContents.append("ADR:").append(escapeMECARD(address)).append(';');                    newdisplayContents.append('\n').append(address);                }                Collection<String> uniquePhones = new HashSet<String>(Contents.PHONE_KEYS.length);                for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {                    String phone = trim(bundle.getString(Contents.PHONE_KEYS[x]));                    if (phone != null) {                        uniquePhones.add(phone);                    }                }                for (String phone : uniquePhones) {                    newContents.append("TEL:").append(escapeMECARD(phone)).append(';');                    newdisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));                }                Collection<String> uniqueEmails = new HashSet<String>(Contents.EMAIL_KEYS.length);                for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {                    String email = trim(bundle.getString(Contents.EMAIL_KEYS[x]));                    if (email != null) {                        uniqueEmails.add(email);                    }                }                for (String email : uniqueEmails) {                    newContents.append("EMAIL:").append(escapeMECARD(email)).append(';');                    newdisplayContents.append('\n').append(email);                }                String url = trim(bundle.getString(Contents.URL_KEY));                if (url != null) {                    // escapeMECARD(url) -> wrong escape e.g. http\://zxing.Google.com                    newContents.append("URL:").append(url).append(';');                    newdisplayContents.append('\n').append(url);                }                String note = trim(bundle.getString(Contents.NOTE_KEY));                if (note != null) {                    newContents.append("NOTE:").append(escapeMECARD(note)).append(';');                    newdisplayContents.append('\n').append(note);                }                // Make sure we've encoded at least one fIEld.                if (newdisplayContents.length() > 0) {                    newContents.append(';');                    contents = newContents.toString();                    displayContents = newdisplayContents.toString();                    Title = "Contact";                } else {                    contents = null;                    displayContents = null;                }            }        } else if (type.equals(Contents.Type.LOCATION)) {            if (bundle != null) {                // These must use Bundle.getfloat(), not getDouble(), it's part of the API.                float latitude = bundle.getfloat("LAT", float.MAX_VALUE);                float longitude = bundle.getfloat("LONG", float.MAX_VALUE);                if (latitude != float.MAX_VALUE && longitude != float.MAX_VALUE) {                    contents = "geo:" + latitude + ',' + longitude;                    displayContents = latitude + "," + longitude;                    Title = "Location";                }            }        }    }    public Bitmap encodeAsBitmap() throws WriterException {        if (!encoded) return null;        Map<EncodeHintType, Object> hints = null;        String enCoding = guessAppropriateEnCoding(contents);        if (enCoding != null) {            hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);            hints.put(EncodeHintType.CHaraCTER_SET, enCoding);        }        MultiFormatWriter writer = new MultiFormatWriter();        BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);        int wIDth = result.getWIDth();        int height = result.getHeight();        int[] pixels = new int[wIDth * height];        // All are 0, or black, by default        for (int y = 0; y < height; y++) {            int offset = y * wIDth;            for (int x = 0; x < wIDth; x++) {                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;            }        }        Bitmap bitmap = Bitmap.createBitmap(wIDth, height, Bitmap.Config.ARGB_8888);        bitmap.setPixels(pixels, 0, wIDth, 0, 0, wIDth, height);        return bitmap;    }    private static String guessAppropriateEnCoding(CharSequence contents) {        // Very crude at the moment        for (int i = 0; i < contents.length(); i++) {            if (contents.charat(i) > 0xFF) { return "UTF-8"; }        }        return null;    }    private static String trim(String s) {        if (s == null) { return null; }        String result = s.trim();        return result.length() == 0 ? null : result;    }    private static String escapeMECARD(String input) {        if (input == null || (input.indexOf(':') < 0 && input.indexOf(';') < 0)) { return input; }        int length = input.length();        StringBuilder result = new StringBuilder(length);        for (int i = 0; i < length; i++) {            char c = input.charat(i);            if (c == ':' || c == ';') {                result.append('\');            }            result.append(c);        }        return result.toString();    }}

Contents.java的来源

package com.as400samplecode;import androID.provIDer.ContactsContract;public final class Contents {    private Contents() {    }    public static final class Type {     // Plain text. Use Intent.putExtra(DATA, string). This can be used for URLs too, but string     // must include "http://" or "https://".        public static final String TEXT = "TEXT_TYPE";        // An email type. Use Intent.putExtra(DATA, string) where string is the email address.        public static final String EMAIL = "EMAIL_TYPE";        // Use Intent.putExtra(DATA, string) where string is the phone number to call.        public static final String PHONE = "PHONE_TYPE";        // An SMS type. Use Intent.putExtra(DATA, string) where string is the number to SMS.        public static final String SMS = "SMS_TYPE";  //  A contact. Send a request to encode it as follows:  //  <p/>  //  import androID.provIDer.Contacts;  //  <p/>  //  Intent intent = new Intent(Intents.Encode.ACTION); intent.putExtra(Intents.Encode.TYPE,  //  CONTACT); Bundle bundle = new Bundle(); bundle.putString(Contacts.Intents.Insert.name,  //  "Jenny"); bundle.putString(Contacts.Intents.Insert.PHONE, "8675309");  //  bundle.putString(Contacts.Intents.Insert.EMAIL, "[email protected]");  //  bundle.putString(Contacts.Intents.Insert.POSTAL, "123 Fake St. San Francisco, CA 94102");  //  intent.putExtra(Intents.Encode.DATA, bundle);        public static final String CONTACT = "CONTACT_TYPE";        public static final String LOCATION = "LOCATION_TYPE";        private Type() {        }    }    public static final String URL_KEY = "URL_KEY";    public static final String NOTE_KEY = "NOTE_KEY";    // When using Type.CONTACT, these arrays provIDe the keys for adding or retrIEving multiple    // phone numbers and addresses.    public static final String[] PHONE_KEYS = {            ContactsContract.Intents.Insert.PHONE, ContactsContract.Intents.Insert.SECONDARY_PHONE,            ContactsContract.Intents.Insert.TERTIARY_PHONE    };    public static final String[] PHONE_TYPE_KEYS = {            ContactsContract.Intents.Insert.PHONE_TYPE,            ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE,            ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE    };    public static final String[] EMAIL_KEYS = {            ContactsContract.Intents.Insert.EMAIL, ContactsContract.Intents.Insert.SECONDARY_EMAIL,            ContactsContract.Intents.Insert.TERTIARY_EMAIL    };    public static final String[] EMAIL_TYPE_KEYS = {            ContactsContract.Intents.Insert.EMAIL_TYPE,            ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE,            ContactsContract.Intents.Insert.TERTIARY_EMAIL_TYPE    };}
总结

以上是内存溢出为你收集整理的如何将扫描的QR码图保存到Android的图像视图中?全部内容,希望文章能够帮你解决如何将扫描的QR码图像保存到Android的图像视图中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1117800.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存