mrtungdev / flutter_star_micronics

GNU General Public License v3.0
1 stars 1 forks source link

This is a request to print image with a sample code #1

Open Eldhose-Islet opened 3 years ago

Eldhose-Islet commented 3 years ago

This seems to be the best library to manage star printer in a flutter in my search of few months. The following is the code that I use in a different plugin.. I am eagerly waiting to implement it in this library. So that it can be used in the POS app we are developing.

Kotlin

it === command map object

if (it.containsKey("appendBitmapImg")) {
        val diffusion: kotlin.Boolean = if (it.containsKey("diffusion")) (it["diffusion"].toString()).toBoolean() else true
        val width: kotlin.Int = if (it.containsKey("width")) (it["width"].toString()).toInt() else 576
        val bothScale: kotlin.Boolean = if (it.containsKey("bothScale")) (it["bothScale"].toString()).toBoolean() else true
        val rotation: com.starmicronics.starioextension.ICommandBuilder.BitmapConverterRotation = if (it.containsKey("rotation")) getConverterRotation(it.get("rotation").toString()) else getConverterRotation("Normal")
        val decodedString: ByteArray = if (it.containsKey("appendBitmapImg")) (it["appendBitmapImg"] as ByteArray) else ByteArray(0);//Base64.decode(bitmapString, Base64.DEFAULT)
        val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
        if (decodedByte != null) {
          if (it.containsKey("absolutePosition")) {
            builder.appendBitmapWithAbsolutePosition(decodedByte, diffusion, width, bothScale, rotation, (it["absolutePosition"].toString()).toInt())
          } else if (it.containsKey("alignment")) {
            builder.appendBitmapWithAlignment(decodedByte, diffusion, width, bothScale, rotation, getAlignment(it["alignment"].toString()))
          } else builder.appendBitmap(decodedByte, diffusion, width, bothScale, rotation)
        }
      }

Swift

command === Dictonary object passed

if (command["appendBitmap"] != nil) {
                let urlString = command["appendBitmap"] as? String
                let width = command["width"] != nil ? (command["width"] as? NSNumber)?.intValue ?? 0 : 576
                let diffusion = ((command["diffusion"] as? NSNumber)?.boolValue ?? false == false) ? false : true
                let bothScale = ((command["bothScale"] as? NSNumber)?.boolValue ?? false == false) ? false : true
                let rotation = getBitmapConverterRotation(command["rotation"] as? String)
                let error: Error? = nil
                let imageURL = URL(string: urlString ?? "")
                var imageData: Data? = nil
                do {
                    if let imageURL = imageURL {
                        imageData = try Data(contentsOf: imageURL, options: .uncached)
                    }
                } catch {
                }

                if error != nil {
                    let fileImageURL = URL(fileURLWithPath: urlString ?? "")
                    do {
                        imageData = try Data(contentsOf: fileImageURL)
                    } catch {
                    }
                }
                if imageData != nil {
                    let image = UIImage(data: imageData!)
                    if command["absolutePosition"] != nil {
                        let position = ((command["absolutePosition"] as? NSNumber)?.intValue ?? 0) != 0 ? (command["absolutePosition"] as? NSNumber)?.intValue ?? 0 : 40
                        builder.appendBitmap(withAbsolutePosition: image, diffusion: diffusion, width: width, bothScale: bothScale, rotation: rotation, position: position)
                    } else if command["alignment"] != nil {
                        let alignment = getAlignment(command["alignment"] as?  String)
                        builder.appendBitmap(withAlignment: image, diffusion: diffusion, width: width, bothScale: bothScale, rotation: rotation, position: alignment)
                    } else {
                        builder.appendBitmap(image, diffusion: diffusion, width: width, bothScale: bothScale, rotation: rotation)
                    }
                }
            } 

Flutter

_capturePng() returns image as Uint8List


final img = await _capturePng();
commands.push({'appendBitmapImg': img});
mrtungdev commented 3 years ago

Did you resolve the prob? Yeah, I've created a package to use ours in our pos too.