ImagePrintable
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.printer.sdk.usb
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.printer.sdk.usb.Printable
data class ImagePrintable private constructor(val image: Bitmap,
val alignment: Byte,
val newLinesAfter: Int) : Printable {
override fun getPrintableByteArray(printer: Printer): List<ByteArray> {
val operations = mutableListOf(
printer.justificationCommand.plus(alignment),
printer.printingImagesHelper.getBitmapAsByteArray(image)
)
if (newLinesAfter > 0) {
operations.add(printer.feedLineCommand.plus(newLinesAfter.toByte()))
}
return operations
}
class Builder() {
private var alignment: Byte = DefaultPrinter.ALIGNMENT_LEFT
private var newLinesAfter = 0
private lateinit var image: Bitmap
constructor(src: Int, resources: Resources) : this() {
image = BitmapFactory.decodeResource(resources, src)
}
constructor(image: Bitmap) : this() {
this.image = image
}
fun setAlignment(alignment: Byte): Builder {
this.alignment = alignment
return this
}
fun setNewLinesAfter(lines: Int): Builder {
this.newLinesAfter = lines
return this
}
fun build(): ImagePrintable {
return ImagePrintable(image, alignment, newLinesAfter)
}
}
}