康耐视条码 SDK 不扫描代码
Cognex Barcode SDK not scanning codes
我已经在我的应用程序中完全实现了 Cognex cmbSDK。我正在使用试用版。关键和一切都正确实施,当我点击按钮时,它会打开相机并显示功能,但它不会扫描任何类型的代码。你能帮我看看为什么它不起作用吗?
这是我的代码:
open class MainActivity : AppCompatActivity(),
ReaderDevice.OnConnectionCompletedListener, ReaderDevice.ReaderDeviceListener,ActivityCompat.OnRequestPermissionsResultCallback{
private val permissionCode = 123
private val sdkKey: String = "My KEY"
private lateinit var readerDevice: ReaderDevice
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
readerDevice = ReaderDevice.getPhoneCameraDevice(this, CameraMode.NO_AIMER,
PreviewOption.NO_ZOOM_BUTTON, null, sdkKey)
readerDevice.setReaderDeviceListener(this)
readerDevice.enableImage(true)
readerDevice.enableImageGraphics(false)
readerDevice.connect(this)
enableSymbologies()
btnScanBarcode.setOnClickListener{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), permissionCode)
}
readerDevice.startScanning()
}
}
override fun onConnectionCompleted(readerDevice: ReaderDevice?, error: Throwable?) {
Toast.makeText(this, "Connection Created", Toast.LENGTH_LONG).show()
if (error != null) {
// ask for Camera Permission if necessary
if (error is CameraPermissionException) ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
permissionCode
)
}
}
override fun onAvailabilityChanged(readerDevice: ReaderDevice?) {
}
override fun onConnectionStateChanged(readerDevice: ReaderDevice?) {
}
override fun onReadResultReceived(readerDevice: ReaderDevice, results: ReadResults) {
readerDevice.stopScanning()
Toast.makeText(this, "Result Received. ${results.getResultAt(0).symbology}", Toast.LENGTH_LONG).show()
Log.d("barcode" ,"MY BARCODE RESULT: ")
if (results.subResults != null && results.subResults.size > 0) {
for (subResult in results.subResults) {
createResultItem(subResult)
}
} else if (results.count > 0) {
createResultItem(results.getResultAt(0))
}
}
private fun createResultItem(result: ReadResult) {
val item =
HashMap<String, String>()
if (result.isGoodRead) {
item["resultText"] = result.readString
val sym = result.symbology
if (sym != null) item["resultType"] =
result.symbology.getName() else item["resultType"] =
"UNKNOWN SYMBOLOGY"
} else {
item["resultText"] = "NO READ"
item["resultType"] = ""
}
Toast.makeText(this, item["resultText"], Toast.LENGTH_SHORT).show()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if(requestCode == permissionCode){
readerDevice.startScanning()
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
private fun enableSymbologies(){
readerDevice.setSymbologyEnabled(Symbology.DATAMATRIX, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.DATAMATRIX"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.C128, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.C128"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.UPC_EAN, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.UPC_EAN"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.CODABAR, false, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to disable Symbology.CODABAR"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.C93, false, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to disable Symbology.C93"
)
}
})
readerDevice.dataManSystem
.sendCommand(
"GET DEVICE.TYPE"
) { _, response ->
if (response.error == null) {
Log.d("Device type", response.payLoad)
}
}
readerDevice.dataManSystem
.sendCommand(
"GET DEVICE.FIRMWARE-VER"
) { _, response ->
if (response.error == null) {
Log.d("Firmware version", response.payLoad)
}
}
}}
清单文件代码:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="MX_MOBILE_LICENSE"
android:value="HERE I PUT MY KEY"/>
</application>
- 能否更具体地说明您想扫描的代码,因为从代码中我可以看出您只选择了 UPC_EAN、DATAMATRIX 和 C128(另请查看您的代码中是否启用了此符号系统许可证密钥)
- 此方法中用于符号系统参数的所有符号系统均可在ReaderDevice.java中找到。
例子
readerDevice.setSymbologyEnabled(Symbology.QR, 真, 空);
这里是我们的知识库以获取更多信息 - https://cmbdn.cognex.com/v2.3.x/knowledge/cognex-mobile-barcode-sdk-for-android/using-cmbsdk
您随时可以在 Cognex 网页上开票。
此致,
康耐视移动解决方案团队
此外,如果您在我们的 SDK 中看到我们将其放入 configureReaderDevice(此处放置您的符号系统),然后您从 onConnectionStateChanged
中调用它
但是请打开此 link 上的支持票,我们的技术支持将解决您的问题 - https://cmbdn.cognex.com/tickets
连接到阅读器设备后必须添加启用符号系统
此致,
我已经在我的应用程序中完全实现了 Cognex cmbSDK。我正在使用试用版。关键和一切都正确实施,当我点击按钮时,它会打开相机并显示功能,但它不会扫描任何类型的代码。你能帮我看看为什么它不起作用吗?
这是我的代码:
open class MainActivity : AppCompatActivity(),
ReaderDevice.OnConnectionCompletedListener, ReaderDevice.ReaderDeviceListener,ActivityCompat.OnRequestPermissionsResultCallback{
private val permissionCode = 123
private val sdkKey: String = "My KEY"
private lateinit var readerDevice: ReaderDevice
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
readerDevice = ReaderDevice.getPhoneCameraDevice(this, CameraMode.NO_AIMER,
PreviewOption.NO_ZOOM_BUTTON, null, sdkKey)
readerDevice.setReaderDeviceListener(this)
readerDevice.enableImage(true)
readerDevice.enableImageGraphics(false)
readerDevice.connect(this)
enableSymbologies()
btnScanBarcode.setOnClickListener{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), permissionCode)
}
readerDevice.startScanning()
}
}
override fun onConnectionCompleted(readerDevice: ReaderDevice?, error: Throwable?) {
Toast.makeText(this, "Connection Created", Toast.LENGTH_LONG).show()
if (error != null) {
// ask for Camera Permission if necessary
if (error is CameraPermissionException) ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
permissionCode
)
}
}
override fun onAvailabilityChanged(readerDevice: ReaderDevice?) {
}
override fun onConnectionStateChanged(readerDevice: ReaderDevice?) {
}
override fun onReadResultReceived(readerDevice: ReaderDevice, results: ReadResults) {
readerDevice.stopScanning()
Toast.makeText(this, "Result Received. ${results.getResultAt(0).symbology}", Toast.LENGTH_LONG).show()
Log.d("barcode" ,"MY BARCODE RESULT: ")
if (results.subResults != null && results.subResults.size > 0) {
for (subResult in results.subResults) {
createResultItem(subResult)
}
} else if (results.count > 0) {
createResultItem(results.getResultAt(0))
}
}
private fun createResultItem(result: ReadResult) {
val item =
HashMap<String, String>()
if (result.isGoodRead) {
item["resultText"] = result.readString
val sym = result.symbology
if (sym != null) item["resultType"] =
result.symbology.getName() else item["resultType"] =
"UNKNOWN SYMBOLOGY"
} else {
item["resultText"] = "NO READ"
item["resultType"] = ""
}
Toast.makeText(this, item["resultText"], Toast.LENGTH_SHORT).show()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if(requestCode == permissionCode){
readerDevice.startScanning()
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
private fun enableSymbologies(){
readerDevice.setSymbologyEnabled(Symbology.DATAMATRIX, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.DATAMATRIX"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.C128, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.C128"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.UPC_EAN, true, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to enable Symbology.UPC_EAN"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.CODABAR, false, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to disable Symbology.CODABAR"
)
}
})
readerDevice.setSymbologyEnabled(Symbology.C93, false, object : OnSymbologyListener {
override fun onSymbologyEnabled(
reader: ReaderDevice,
symbology: Symbology,
enabled: Boolean,
error: Throwable
) {
Log.e(
this.javaClass.simpleName,
"Failed to disable Symbology.C93"
)
}
})
readerDevice.dataManSystem
.sendCommand(
"GET DEVICE.TYPE"
) { _, response ->
if (response.error == null) {
Log.d("Device type", response.payLoad)
}
}
readerDevice.dataManSystem
.sendCommand(
"GET DEVICE.FIRMWARE-VER"
) { _, response ->
if (response.error == null) {
Log.d("Firmware version", response.payLoad)
}
}
}}
清单文件代码:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="MX_MOBILE_LICENSE"
android:value="HERE I PUT MY KEY"/>
</application>
- 能否更具体地说明您想扫描的代码,因为从代码中我可以看出您只选择了 UPC_EAN、DATAMATRIX 和 C128(另请查看您的代码中是否启用了此符号系统许可证密钥)
- 此方法中用于符号系统参数的所有符号系统均可在ReaderDevice.java中找到。
例子 readerDevice.setSymbologyEnabled(Symbology.QR, 真, 空);
这里是我们的知识库以获取更多信息 - https://cmbdn.cognex.com/v2.3.x/knowledge/cognex-mobile-barcode-sdk-for-android/using-cmbsdk
您随时可以在 Cognex 网页上开票。
此致, 康耐视移动解决方案团队
此外,如果您在我们的 SDK 中看到我们将其放入 configureReaderDevice(此处放置您的符号系统),然后您从 onConnectionStateChanged
中调用它但是请打开此 link 上的支持票,我们的技术支持将解决您的问题 - https://cmbdn.cognex.com/tickets
连接到阅读器设备后必须添加启用符号系统
此致,