实例介绍
【实例截图】
【核心代码】
/* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.zxing.client.androidtest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.Charset; import java.util.Arrays; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.hardware.Camera; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; public final class ZXingTestActivity extends Activity { private static final String TAG = ZXingTestActivity.class.getSimpleName(); private static final int ABOUT_ID = Menu.FIRST; private static final String PACKAGE_NAME = ZXingTestActivity.class.getPackage().getName(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.test); findViewById(R.id.get_camera_parameters).setOnClickListener(getCameraParameters); findViewById(R.id.scan_product).setOnClickListener(scanProduct); findViewById(R.id.scan_qr_code).setOnClickListener(scanQRCode); findViewById(R.id.scan_anything).setOnClickListener(scanAnything); findViewById(R.id.search_book_contents).setOnClickListener(searchBookContents); findViewById(R.id.encode_url).setOnClickListener(encodeURL); findViewById(R.id.encode_email).setOnClickListener(encodeEmail); findViewById(R.id.encode_phone).setOnClickListener(encodePhone); findViewById(R.id.encode_sms).setOnClickListener(encodeSMS); findViewById(R.id.encode_contact).setOnClickListener(encodeContact); findViewById(R.id.encode_location).setOnClickListener(encodeLocation); findViewById(R.id.encode_hidden_data).setOnClickListener(encodeHiddenData); findViewById(R.id.encode_bad_data).setOnClickListener(encodeBadData); findViewById(R.id.share_via_barcode).setOnClickListener(shareViaBarcode); findViewById(R.id.run_benchmark).setOnClickListener(runBenchmark); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, ABOUT_ID, 0, R.string.about_menu).setIcon(android.R.drawable.ic_menu_info_details); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == ABOUT_ID) { int versionCode; String versionName; try { PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0); versionCode = info.versionCode; versionName = info.versionName; } catch (PackageManager.NameNotFoundException e) { versionCode = 0; versionName = "unknown"; } AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle( getString(R.string.app_name) ' ' versionName " (" versionCode ')'); builder.setMessage(getString(R.string.about_message)); builder.setPositiveButton(R.string.ok_button, null); builder.show(); } return super.onOptionsItemSelected(item); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if (result != null) { String contents = result.getContents(); if (contents != null) { showDialog(R.string.result_succeeded, result.toString()); } else { showDialog(R.string.result_failed, getString(R.string.result_failed_why)); } } } private final Button.OnClickListener getCameraParameters = new Button.OnClickListener() { @Override public void onClick(View v) { String stats = collectStats(); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_EMAIL, "zxing-external@google.com"); intent.putExtra(Intent.EXTRA_SUBJECT, "Camera parameters report"); intent.putExtra(Intent.EXTRA_TEXT, stats); intent.setType("text/plain"); startActivity(intent); } }; private final Button.OnClickListener runBenchmark = new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setClassName(ZXingTestActivity.this, BenchmarkActivity.class.getName()); startActivity(intent); } }; private final Button.OnClickListener scanProduct = new Button.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(ZXingTestActivity.this); integrator.addExtra("SCAN_WIDTH", 800); integrator.addExtra("SCAN_HEIGHT", 200); integrator.addExtra("RESULT_DISPLAY_DURATION_MS", 3000L); integrator.addExtra("PROMPT_MESSAGE", "Custom prompt to scan a product"); integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES); } }; private final Button.OnClickListener scanQRCode = new Button.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(ZXingTestActivity.this); integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES); } }; private final Button.OnClickListener scanAnything = new Button.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(ZXingTestActivity.this); integrator.initiateScan(); } }; private final Button.OnClickListener searchBookContents = new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.google.zxing.client.android.SEARCH_BOOK_CONTENTS"); intent.putExtra("ISBN", "9780441014989"); intent.putExtra("QUERY", "future"); startActivity(intent); } }; private final Button.OnClickListener encodeURL = new Button.OnClickListener() { @Override public void onClick(View v) { encodeBarcode("TEXT_TYPE", "http://www.nytimes.com"); } }; private final Button.OnClickListener encodeEmail = new Button.OnClickListener() { @Override public void onClick(View v) { encodeBarcode("EMAIL_TYPE", "foo@example.com"); } }; private final Button.OnClickListener encodePhone = new Button.OnClickListener() { @Override public void onClick(View v) { encodeBarcode("PHONE_TYPE", "2125551212"); } }; private final Button.OnClickListener encodeSMS = new Button.OnClickListener() { @Override public void onClick(View v) { encodeBarcode("SMS_TYPE", "2125551212"); } }; private final Button.OnClickListener encodeContact = new Button.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString(ContactsContract.Intents.Insert.NAME, "Jenny"); bundle.putString(ContactsContract.Intents.Insert.PHONE, "8675309"); bundle.putString(ContactsContract.Intents.Insert.EMAIL, "jenny@the80s.com"); bundle.putString(ContactsContract.Intents.Insert.POSTAL, "123 Fake St. San Francisco, CA 94102"); encodeBarcode("CONTACT_TYPE", bundle); } }; private final Button.OnClickListener encodeLocation = new Button.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putFloat("LAT", 40.829208f); bundle.putFloat("LONG", -74.191279f); encodeBarcode("LOCATION_TYPE", bundle); } }; private final Button.OnClickListener encodeHiddenData = new Button.OnClickListener() { @Override public void onClick(View v) { IntentIntegrator integrator = new IntentIntegrator(ZXingTestActivity.this); integrator.addExtra("ENCODE_SHOW_CONTENTS", false); integrator.shareText("SURPRISE!"); } }; private final Button.OnClickListener encodeBadData = new Button.OnClickListener() { @Override public void onClick(View v) { encodeBarcode(null, "bar"); } }; private final Button.OnClickListener shareViaBarcode = new Button.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent("com.google.zxing.client.android.SHARE")); } }; private void showDialog(int title, CharSequence message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(title); builder.setMessage(message); builder.setPositiveButton("OK", null); builder.show(); } private void encodeBarcode(CharSequence type, CharSequence data) { IntentIntegrator integrator = new IntentIntegrator(this); integrator.shareText(data, type); } private void encodeBarcode(CharSequence type, Bundle data) { IntentIntegrator integrator = new IntentIntegrator(this); integrator.addExtra("ENCODE_DATA", data); integrator.shareText(data.toString(), type); // data.toString() isn't used } private static String getFlattenedParams() { Camera camera = Camera.open(); if (camera == null) { return null; } try { Camera.Parameters parameters = camera.getParameters(); if (parameters == null) { return null; } return parameters.flatten(); } finally { camera.release(); } } private static String collectStats() { StringBuilder result = new StringBuilder(1000); result.append("BOARD=").append(Build.BOARD).append('\n'); result.append("BRAND=").append(Build.BRAND).append('\n'); result.append("CPU_ABI=").append(Build.CPU_ABI).append('\n'); result.append("DEVICE=").append(Build.DEVICE).append('\n'); result.append("DISPLAY=").append(Build.DISPLAY).append('\n'); result.append("FINGERPRINT=").append(Build.FINGERPRINT).append('\n'); result.append("HOST=").append(Build.HOST).append('\n'); result.append("ID=").append(Build.ID).append('\n'); result.append("MANUFACTURER=").append(Build.MANUFACTURER).append('\n'); result.append("MODEL=").append(Build.MODEL).append('\n'); result.append("PRODUCT=").append(Build.PRODUCT).append('\n'); result.append("TAGS=").append(Build.TAGS).append('\n'); result.append("TIME=").append(Build.TIME).append('\n'); result.append("TYPE=").append(Build.TYPE).append('\n'); result.append("USER=").append(Build.USER).append('\n'); result.append("VERSION.CODENAME=").append(Build.VERSION.CODENAME).append('\n'); result.append("VERSION.INCREMENTAL=").append(Build.VERSION.INCREMENTAL).append('\n'); result.append("VERSION.RELEASE=").append(Build.VERSION.RELEASE).append('\n'); result.append("VERSION.SDK_INT=").append(Build.VERSION.SDK_INT).append('\n'); String flattened = getFlattenedParams(); String[] params = flattened.split(";"); Arrays.sort(params); for (String param : params) { result.append(param).append('\n'); } String resultString = result.toString(); writeStats(resultString); return resultString; } private static void writeStats(String resultString) { Writer out = null; try { out = new OutputStreamWriter(new FileOutputStream(new File("/sdcard/CameraParameters.txt")), Charset.forName("UTF-8")); out.write(resultString); } catch (IOException e) { Log.e(TAG, "Cannot write parameters file ", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { Log.w(TAG, e); } } } } }
标签: 二维码
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论