【问题标题】:Generate list of QR codes using RecyclerView, ZXing, RxJava in Android在 Android 中使用 RecyclerView、ZXing、RxJava 生成二维码列表
【发布时间】:2019-08-23 09:46:23
【问题描述】:

我想生成一个二维码列表(本例中为 10 个)。

我正在使用 RecyclerView 适配器和 ZXing 库,其中 generateQrCode 方法是我从这里获取的:https://stackoverflow.com/a/25283174/9311961,它返回 256 x 256 的位图(512 会花费太多时间) :

我的第一种方法是在后台线程上生成 QR,如下所示:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val context = holder.itemView.context
    val qrCode = qrCodeList!![position]

    println("TIME AT $position: ${getCurrentDateTime()}")
    holder.imageIv.setImageBitmap(generateQrCode(context, qrCode.qr))

    // ...
}

我得到了这些时间作为回应:

I/System.out: TIME AT 0: 2019-08-23 11:25:14
I/System.out: TIME AT 1: 2019-08-23 11:25:15
I/System.out: TIME AT 2: 2019-08-23 11:25:16
I/System.out: TIME AT 3: 2019-08-23 11:25:16
I/System.out: TIME AT 4: 2019-08-23 11:25:17
I/System.out: TIME AT 5: 2019-08-23 11:25:17
I/System.out: TIME AT 6: 2019-08-23 11:25:18
I/System.out: TIME AT 7: 2019-08-23 11:25:18
I/System.out: TIME AT 8: 2019-08-23 11:25:19
I/System.out: TIME AT 9: 2019-08-23 11:25:20

=> 6 秒

但问题是,这种方法会在生成 QR 之前阻止我的屏幕。

我的第二种方法是使用 RxJava 生成 QR,避免使用后台线程:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val context = holder.itemView.context
    val qrCode = qrCodeList!![position]

    val array = arrayListOf(qrCode.qr, holder.imageIv, position)
    println("TIME START: ${getCurrentDateTime()}")

    Single.just(array)
        .subscribeOn(Schedulers.computation()).map {
            val bitmap = generateQrCode(context, it[0] as String)
            arrayListOf(bitmap, it[1], it[2])
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this::onGenerateQrCodeSuccess, this::onGenerateQrCodeError)

    // ...
}

private fun onGenerateQrCodeSuccess(array: ArrayList<Any>) {
    val qrCode = array[0] as Bitmap
    val imageView = array[1] as AppCompatImageView
    val position = array[2] as Int

    println("TIME AT $position: ${getCurrentDateTime()}")
    imageView.setImageBitmap(qrCode)
}

private fun onGenerateQrCodeError(throwable: Throwable) {
    println("ERROR WHILE GENERATING QR: $throwable")
}

我得到了这些时间作为回应:

I/System.out: TIME START: 2019-08-23 12:15:32
I/System.out: TIME AT 0: 2019-08-23 12:15:43
I/System.out: TIME AT 2: 2019-08-23 12:15:44
I/System.out: TIME AT 3: 2019-08-23 12:15:44
I/System.out: TIME AT 1: 2019-08-23 12:15:45
I/System.out: TIME AT 4: 2019-08-23 12:15:54
I/System.out: TIME AT 6: 2019-08-23 12:15:55
I/System.out: TIME AT 7: 2019-08-23 12:15:55
I/System.out: TIME AT 5: 2019-08-23 12:15:55
I/System.out: TIME AT 8: 2019-08-23 12:15:57
I/System.out: TIME AT 9: 2019-08-23 12:15:57

=> 25 秒

这比从后台线程生成二维码要多得多。

如果我在第一次进场时不会被屏幕挡住,那就太好了。

那么我怎样才能获得通过使用 RxJava(或以任何其他方式)生成的快速 QR,以便在显示之前不会阻止我的屏幕?

【问题讨论】:

    标签: android kotlin rx-java qr-code zxing


    【解决方案1】:

    很可能问题出在computation 调度程序中。在内部,它使用固定数量的线程(等于设备上的核心数量)。所以只需将Schedulers.computation() 替换为Schedulers.io(),一切都会更快。

    【讨论】:

    • 使用 io 而不是计算需要 24 秒,所以它仍然不是一个好的选择。
    猜你喜欢
    • 2014-04-17
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多