# QRCode-Kotlin
Creating QRCodes in Kotlin (and Java) is harder than it should be. **QRCode-Kotlin aims to bring a simple,
straightforward and customizable way to create QRCodes**, especially in the backend.
It is with this mission in mind that we keep doing our best to learn how developers use this library and their goals so
that we can provide a better library/API for them. Please, feel free to share if and how you're using this project ^^
* **Pure Kotlin:** Rewritten on pure Kotlin from a reference implementation of the QRCode spec
by [Kazuhiko Arase](https://github.com/kazuhikoarase/qrcode-generator)
* **Lightweight:** No dependencies, `~115KB` and it does what it says on the tin.
* **Easy to use:** Quickly and easily implement QRCodes with few lines of code.
* **Good-looking:** Many developers don't have time and sometimes knowledge to implement the perfect QRCode,
so this library tries to generate good-looking QRCodes by default.
* **Server friendly:** The JVM version is mainly focused on a personal use-case where I needed to generate QRCodes on
the backend, but all libraries I found were either complex or huge, usually both.
* **Multiplatform:** This is a KMP library with support to Java, JavaScript, Android, iOS and tvOS.
## 1. Installation
The library is available
from [Maven Central](https://search.maven.org/artifact/io.github.g0dkar/qrcode-kotlin/4.1.0/qrcode-kotlin)
and [NPM JS](https://www.npmjs.com/package/qrcode-kotlin), so you can add it to your project as a dependency like any
other:
**Gradle:**
```groovy
// Use this for both Android and JVM
implementation("io.github.g0dkar:qrcode-kotlin:4.1.1")
```
**Maven - JVM:**
```xml
<dependency>
<groupId>io.github.g0dkar</groupId>
<artifactId>qrcode-kotlin-jvm</artifactId>
<version>4.1.1</version>
</dependency>
```
**Maven - Android:**
```xml
<dependency>
<groupId>io.github.g0dkar</groupId>
<artifactId>qrcode-kotlin-android</artifactId>
<version>4.1.1</version>
</dependency>
```
**NodeJS:**
```shell
npm install [email protected]
```
**Browser:**
```html
<script src="https://cdn.jsdelivr.net/gh/g0dkar/qrcode-kotlin@main/release/qrcode-kotlin.min.js" type="application/javascript"></script>
```
## Usage
To create QRCodes, the main class that should be used is the `qrcode.render.QRCode` class. It has static methods to help
you create a QRCode the way you want:
```kotlin
// Use one of these:
val squares = QRCode.ofSquares()
val circles = QRCode.ofCircles()
val roundedSquares = QRCode.ofRoundedSquares()
```
With that, you'll have a [QRCodeBuilder](src/commonMain/kotlin/qrcode/QRCodeBuilder.kt) instance. It has methods to
adjust colors, size, spacing, add a logo and more! Also, make sure to check
the [Colors](src/commonMain/kotlin/qrcode/color/Colors.kt) class as well.
Here's a code to get you started:
```kotlin
val helloWorld = QRCode.ofSquares()
.withColor(Colors.DEEP_SKY_BLUE) // Default is Colors.BLACK
.withSize(10) // Default is 25
.build("Hello world!")
// By default, QRCodes are rendered as PNGs.
val pngBytes = helloWorld.render()
FileOutputStream("hello-world.png").use { it.write(pngBytes) }
```
We highly recommend that you check out some examples:
* [All sorts of shapes](examples/kotlin/src/main/kotlin/Example01-Shapes.kt): Squares, Circles, Rounded Squares and
Custom shapes
* [All about colors](examples/kotlin/src/main/kotlin/Example02-Colors.kt): Foreground, Background, Transparent
backgrounds, Linear Gradient colors
* [Adding a Logo](examples/kotlin/src/main/kotlin/Example03-Logo.kt): Add a logo and remove the cells behind it, or
don't :)
* [SVG QRCodes](examples/kotlin/src/main/kotlin/Example04-SVG.kt): How to extend the renderer to render SVG (uses [JFree SVG](https://github.com/jfree/jfreesvg))
* [The banner on the top of this README](examples/kotlin/src/main/kotlin/ProjectLogo.kt): Yup, all done with the
library ^^
The examples show pretty much all that can be done with the library! Even how to extend it so that it can create SVG
QRCodes ;)
You can mix and match all those together. Try generating the library logo and banner with gradients and all in SVG ;)
### Spring Framework and/or Spring Boot
As said earlier, one of the main reasons I developed this library was to use it on a backend application. So it is only
natural to show how to do that :)
This Spring Framework/Boot controller method can generate QRCodes of a given content:
```kotlin
import org.springframework.core.io.ByteArrayResource
import org.springframework.http.HttpHeaders.CONTENT_DISPOSITION
import org.springframework.http.MediaType.IMAGE_PNG_VALUE
@GetMapping("/qrcode")
fun generateQrCode(content: String): ResponseEntity<ByteArrayResource> {
val pngData = QRCode().ofSquares()
.build(content)
.render()
val resource = ByteArrayResource(pngData, IMAGE_PNG_VALUE)
return ResponseEntity.ok()
.header(CONTENT_DISPOSITION, "attachment; filename=\"qrcode.png\"")
.body(resource)
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
kotlin-使用kotlin开发的二维码生成器.zip (177个子文件)
gradlew.bat 3KB
.editorconfig 935B
.gitignore 229B
gradlew 8KB
qrcode-example.html 7KB
qrcode-kotlin-jvm-4.1.1.jar 116KB
gradle-wrapper.jar 42KB
QRCode.java 16KB
QRUtil.java 9KB
RSBlock.java 6KB
Example01_Shapes.java 2KB
Polynomial.java 2KB
Example02_Colors.java 2KB
QRData.java 2KB
Example03_SVG.java 1KB
QRKanji.java 1KB
BitBuffer.java 1KB
QRNumber.java 1KB
QRAlphaNum.java 1KB
JVMTriangleShapeFunction.java 1KB
TriangleShapeFunction.java 1021B
SVGQRCodeGraphics.java 993B
QRMath.java 887B
QR8BitByte.java 774B
MaskPattern.java 637B
SVGGraphicsFactory.java 478B
Util.java 403B
Mode.java 374B
ErrorCorrectionLevel.java 328B
qrcode-kotlin.js 293KB
package.json 618B
Contents.json 177B
Contents.json 123B
Contents.json 63B
Contents.json 63B
QRCodeProcessor.kt 16KB
QRUtil.kt 12KB
QRCodeSetup.kt 11KB
QRCodeTest.kt 10KB
QRCodeBuilder.kt 9KB
Colors.kt 9KB
QRCodeGraphics.tvos.kt 9KB
QRCodeGraphics.ios.kt 9KB
RSBlock.kt 9KB
QRCodeGraphics.jvm.kt 8KB
QRCodeGraphics.android.kt 8KB
QRCodeGraphics.js.kt 8KB
QRCode.kt 7KB
QRNumberTest.kt 6KB
QRCodeSquare.kt 5KB
DefaultShapeFunction.kt 5KB
QRData.kt 4KB
Example01-Shapes.kt 4KB
PolynomialTest.kt 4KB
TestUtils.kt 4KB
QRCodeGraphics.kt 3KB
ColorsTest.kt 3KB
QRCodeEnums.kt 3KB
QRCodeListActivity.kt 3KB
NewQRCodeActivity.kt 3KB
ProjectLogo.kt 2KB
QRCodeListDatasource.kt 2KB
QRCodeListAdapter.kt 2KB
Polynomial.kt 2KB
Example02-Colors.kt 2KB
Example04-SVG.kt 2KB
QRCodeDetailActivity.kt 2KB
QRCodeColorFunction.kt 2KB
LinearGradientColorFunction.kt 2KB
QRMath.kt 1KB
BitBuffer.kt 1KB
Example03-Logo.kt 1KB
RoundSquaresShapeFunction.kt 1KB
CircleShapeFunction.kt 1KB
QRCodeShapeFunction.kt 1KB
DefaultColorFunction.kt 917B
QRCodeGraphicsFactory.kt 812B
SVGQRCodeGraphics.kt 707B
QRCodeProcessorTest.kt 682B
QRCodeData.kt 681B
TvOSNativeParser.kt 644B
IOSNativeParser.kt 644B
ErrorMessage.kt 592B
Example05-BackwardsCompat.kt 562B
SVGGraphicsFactory.kt 362B
AboutActivity.kt 307B
QRCodeRawData.kt 159B
ColorType.kt 48B
build.gradle.kts 8KB
build.gradle.kts 1KB
settings.gradle.kts 633B
build.gradle.kts 213B
build.gradle.kts 204B
qrcode-kotlin.js.map 154KB
README.md 5KB
project.pbxproj 24KB
project-banner.png 116KB
project-logo.png 103KB
example03-logo.png 69KB
example03-logo-with-cells.png 68KB
共 177 条
- 1
- 2
资源评论
Mopes__
- 粉丝: 1042
- 资源: 319
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功