joecks / clipboard-actions-community

A issue tracker for the Clipboard Actions Project
http://joecks.github.io/clipboard-actions/
Other
85 stars 21 forks source link

Use native QR code app xzing if installed #3

Open joecks opened 8 years ago

joecks commented 8 years ago

use teh xzing app if available for QR code actions.

dgmltn commented 8 years ago

Or, embed the zxing library in the clipboard app:

in build.gradle:

    compile 'com.google.zxing:core:3.2.1'

In your [display qr code] activity (this code fragment is from Kotlin, sorry!):

        private val WHITE = 0xffffffff.toInt()
        private val BLACK = 0xff000000.toInt()

        fun generateBitmapDrawable(qrcode: String?, res: Resources): BitmapDrawable? {
            if (qrcode == null) {
                return null
            }
            val bitmap = encodeAsBitmap(qrcode, BarcodeFormat.AZTEC) ?: return null
            val drawable = BitmapDrawable(res, bitmap)
            drawable.isFilterBitmap = false
            return drawable
        }

        fun encodeAsBitmap(contents: String, format: BarcodeFormat): Bitmap? {
            if (TextUtils.isEmpty(contents)) {
                return null
            }
            var hints: MutableMap<EncodeHintType, Any>? = null
            val encoding = guessAppropriateEncoding(contents)
            if (encoding != null) {
                hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java)
                hints.put(EncodeHintType.CHARACTER_SET, encoding)
            }
            val writer = MultiFormatWriter()
            val result: BitMatrix
            try {
                result = writer.encode(contents, format, 0, 0, hints)
            }
            catch (e: WriterException) {
                e.printStackTrace()
                return null
            }
            catch (iae: IllegalArgumentException) {
                Timber.e("Unsupported format")
                return null
            }

            val width = result.width
            val height = result.height

            val pixels = IntArray(width * height)
            for (y in 0..height - 1) {
                val offset = y * width
                for (x in 0..width - 1) {
                    pixels[offset + x] = if (result.get(x, y)) BLACK else WHITE
                }
            }

            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
            return bitmap
        }
joecks commented 8 years ago

Cool. That is a nice example. When I have time I will consider to write an own UI but in the first iteration, I will simply use app if existing :)