如何在不安装实际Zxing应用程序的情况下集成Zxing条形码扫描仪(无法解析符号:.android.CaptureActivity)?
我想将zxing扫描仪集成到我的应用程序中,而不需要外部应用程序(play store提供的zxing扫描仪)。这是我的密码如何在不安装实际Zxing应用程序的情况下集成Zxing条形码扫描仪(无法解析符号:.android.CaptureActivity)?,android,zxing,Android,Zxing,我想将zxing扫描仪集成到我的应用程序中,而不需要外部应用程序(play store提供的zxing扫描仪)。这是我的密码 Button scan = (Button) findViewById(R.id.scan_button); scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Int
Button scan = (Button) findViewById(R.id.scan_button);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.mypackage.app");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, SCANNER_REQUEST_CODE);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SCANNER_REQUEST_CODE) {
// Handle scan intent
if (resultCode == Activity.RESULT_OK) {
// Handle successful scan
String contents = intent.getStringExtra("SCAN_RESULT");
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
Integer orientation = (intentOrientation == Integer.MIN_VALUE) ? null : intentOrientation;
String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
}
} else {
// Handle other intents
}
}
AndroidManifest.xml
<activity android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
我的Android Studio说无法解析符号:.Android.CaptureActivity
,如果我运行这个,就会发生错误
java.lang.ClassNotFoundException:未找到类“com.google.zxing.client.android.CaptureActivity”
发生了什么事?我不知道为什么会发生,但最后我使用了另一个库。我使用和的教程。现在它可以工作了。虽然已经得到了回答,但对于那些希望使用ZXing作为库而不使用任何自动安装方法(如ANT)的人来说,我觉得这是一个深入的解决方案 ZXing是一个开源项目(如ZXing网站所述),您可以将其功能用作项目库,而不是强制在用户设备上安装第三方应用程序,我认为这是大多数项目的首选 需要注意的是,这种方式是创建者可以接受的,并且不会违反他们的任何使用条款,因为我们不会复制他们的代码,而只是将其用作我们项目的库 一般过程如下:
Intent intent = new Intent(getApplicationContext(),CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
在onActivityResult()中检索扫描结果:
请注意,正如android capture中的任何其他活动一样,活动也需要在项目清单中定义,还请注意,您必须向项目授予使用摄像头的权限,如下所示:
<uses-permission android:name="android.permission.CAMERA" />
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape" >
</activity>
如果你找不到像我一样的罐子,这里有URL:
详细阐述stackex的答案。。。。创建具有任意名称的活动
public class CaptureActivityAnyOrientation extends CaptureActivity {
}
在manifest中定义所需的方向,或者像下面这样在横向和横向中工作
<activity android:name=".CaptureActivityAnyOrientation"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"/>
希望这能有所帮助。因为我找到的所有答案都是基于Eclipse的,而且到目前为止都很陈旧,所以我添加了我的答案,将ZXing添加为Android Studio(2.1.2)上的一个独立库
我已经把编译好的项目放到Github上了,只要在你的项目中添加zxing_standalone
模块,你就可以开始了。
<activity android:name=".CaptureActivityAnyOrientation"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"/>
IntentIntegrator.forSupportFragment(fragment)
.setCaptureActivity(CaptureActivityAnyOrientation.class)
.setOrientationLocked(false)
.setBeepEnabled(true)
.addExtra("PROMPT_MESSAGE", "Scan QR Code")
.initiateScan(IntentIntegrator.QR_CODE_TYPES);
Intent intent = new Intent(getApplicationContext(),CaptureActivity.class);
intent.setAction("com.google.zxing.client.android.SCAN");
intent.putExtra("SAVE_HISTORY", false);
startActivityForResult(intent, 0);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Log.d(TAG, "contents: " + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
Log.d(TAG, "RESULT_CANCELED");
}
}
Camera.PreviewCallback myCallBack = new Camera.PreviewCallback() {
@Override public void onPreviewFrame(byte[] data, Camera camera)
{
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
int format = parameters.getPreviewFormat();
LuminanceSource source = null;
if(format == ImageFormat.NV21) //YCbCr_420_SP)
{
source = new PlanarYUVLuminanceSource(imgData, bitmapWidth, bitmapHeight, 0,0, bitmapWidth,bitmapHeight, false);
} else
{
throw new UnsupportedOperationException("Unsupported image format ");
}
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
try
{
Result result = reader.decode(bBitmap);
txt.setText(result.getText())
} catch (NotFoundException e)
{
//txt.setText("");
}
}
};
import android.hardware.Camera;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
Camera mCamera;
MultiFormatReader reader;
TextView txt;
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
IntentIntegrator integrator = new IntentIntegrator(Home.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Code");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(intentResult != null) {
if(intentResult.getContents() == null) {
Log.d("MainActivity", "Cancelled");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + intentResult.getContents(), Toast.LENGTH_LONG).show();
}
}
implementation 'com.google.zxing:core:x.x.x'