public void handleDecode(Result result, Bitmap barcode) { inactivityTimer.onActivity(); playBeepSoundAndVibrate(); String resultString = result.getText(); //FIXME if (resultString.equals("")) { Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show(); }else { // System.out.println("Result:"+resultString); Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("result", resultString); resultIntent.putExtras(bundle); this.setResult(RESULT_OK, resultIntent); } CaptureActivity.this.finish(); }
时间: 2023-08-02 21:09:03 浏览: 52
这段代码是一个二维码/条形码扫描器的回调函数,当扫描完成后会调用该函数并传入扫描结果和扫描出的二维码/条形码图片。函数中会先停止计时器和播放提示音,然后判断扫描结果是否为空。如果为空,则显示扫描失败的提示;否则,将扫描结果封装到一个 Intent 中,并设置返回值为 RESULT_OK,最后关闭当前 Activity。需要注意的是,FIXME 注释表示该部分代码可能存在问题或需要进一步完善。
相关问题
请帮我优化这段代码使其可以解Data Matrix ECC 140二维码 Bitmap bmp = new Bitmap(imgPath); var barcodeReader = new ZXing.BarcodeReader(); barcodeReader.Options.PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.DATA_MATRIX }; var result = barcodeReader.Decode(bmp);
你可以尝试以下代码:
```csharp
using System.Drawing;
using ZXing;
using ZXing.Common;
Bitmap bmp = new Bitmap(imgPath);
var barcodeReader = new BarcodeReader
{
Options = new DecodingOptions
{
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.DATA_MATRIX },
TryHarder = true,
PureBarcode = true,
PossibleEAN13CountryCodes = new List<string> { "CN" } // 如果需要指定国家码的话,可以加上这一行
}
};
var result = barcodeReader.Decode(bmp);
if (result == null)
{
// 处理解码失败的情况
}
else
{
// 处理解码成功的情况
}
```
其中,`PureBarcode` 为 `true` 表示只解码纯条码(不包含图像);`TryHarder` 为 `true` 表示使用更耗时的算法来提高解码准确率;`PossibleEAN13CountryCodes` 可以指定 EAN-13 条码的国家码列表,如果不需要可以忽略。
我希望在这段代码完成向数据库添加图片后,通过toast提示用户保存成功或失败 save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache()); imageView.setDrawingCacheEnabled(false); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] data = stream.toByteArray(); ContentValues values = new ContentValues(); values.put("data", data); db.insert("images", null, values); } });
可以在插入数据的语句后面加上以下代码来实现:
```
long result = db.insert("images", null, values);
if (result == -1) {
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
```
这段代码会将插入数据的结果返回给result变量,如果返回值为-1,说明插入失败,弹出“保存失败”的Toast提示;否则弹出“保存成功”的Toast提示。注意,这段代码需要在Activity中执行,所以需要将MainActivity替换为你的Activity名称。