MyGit

kenglxn/QRGen

Fork: 300 Star: 1496 (更新于 2024-12-10 12:01:59)

license: 暂无

Language: Java .

a simple QRCode generation api for java built on top ZXING

最后发布版本: 3.0.1 ( 2022-11-29 02:50:35)

官方网址 GitHub网址

Build Status Release

QRGen: a simple QRCode generation api for java built on top ZXING

Please consider sponsoring the work on QRGen

Dependencies:

ZXING: http://code.google.com/p/zxing/

Get it:

QRGen consists of three modules: core, javase and android.

As of 2.1.0 QRGen is available from jitpack.io. QRGen is no longer deployed to maven central (ref: #61). Older releases are available from Maven Central Repository.

As of 3.0.1 java 11 is required. Latest java 8 version is 2.7.0.

Maven:

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

Gradle:

    allprojects {
        repositories {
            // ...
            maven { url "https://jitpack.io" }
        }
    }
Nexus proxy setup for jitpack

See https://github.com/jitpack/jitpack.io/issues/506 for solution.

(thanks to @LTheobald for the heads up)

Java Application

When developing a Java application you need to add javase module to your list of dependencies. The required core module will be added automatically by your build system:

Gradle:

    dependencies {
        implementation 'com.github.kenglxn.QRGen:javase:3.0.1'
    }

Maven:

    <dependencies>
        <dependency>
            <groupId>com.github.kenglxn.QRGen</groupId>
            <artifactId>javase</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
Android

When you want to use QRGen inside your android application you need to add the android module to your list of dependencies. The required core module will be added automatically by your build system:

Gradle:

    dependencies {
        implementation 'com.github.kenglxn.QRGen:android:3.0.1'
    }

Maven:

    <dependencies>
        <dependency>
            <groupId>com.github.kenglxn.QRGen</groupId>
            <artifactId>android</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

Or you can clone and build yourself:

    git clone git://github.com/kenglxn/QRGen.git
    cd QRGen/
    mvn clean install

Usage:

public class Demo {
    public static void main(String[] args) {
        // get QR file from text using defaults
        File file = QRCode.from("Hello World").file();

        // get QR stream from text using defaults
        ByteArrayOutputStream stream = QRCode.from("Hello World").stream();

        // override the image type to be JPG
        QRCode.from("Hello World").to(ImageType.JPG).file();
        QRCode.from("Hello World").to(ImageType.JPG).stream();

        // override image size to be 250x250
        QRCode.from("Hello World").withSize(250, 250).file();
        QRCode.from("Hello World").withSize(250, 250).stream();

        // override size and image type
        QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).file();
        QRCode.from("Hello World").to(ImageType.GIF).withSize(250, 250).stream();

        // override default colors (black on white)
        // notice that the color format is "0x(alpha: 1 byte)(RGB: 3 bytes)"
        // so in the example below it's red for foreground and yellowish for background, both 100% alpha (FF).
        QRCode.from("Hello World").withColor(0xFFFF0000, 0xFFFFFFAA).file();

        // supply own outputstream
        QRCode.from("Hello World").to(ImageType.PNG).writeTo(new ByteArrayOutputStream());

        // supply own file name
        QRCode.from("Hello World").file("QRCode");

        // supply charset hint to ZXING
        QRCode.from("Hello World").withCharset("UTF-8");

        // supply error correction level hint to ZXING
        QRCode.from("Hello World").withErrorCorrection(ErrorCorrectionLevel.L);

        // supply any hint to ZXING
        QRCode.from("Hello World").withHint(EncodeHintType.CHARACTER_SET, "UTF-8");

        // encode contact data as vcard using defaults
        VCard johnDoe = new VCard("John Doe")
                .setEmail("john.doe@example.org")
                .setAddress("John Doe Street 1, 5678 Doestown")
                .setTitle("Mister")
                .setCompany("John Doe Inc.")
                .setPhoneNumber("1234")
                .setWebsite("www.example.org");
        QRCode.from(johnDoe).file();

        // encode email data
        EMail email = new EMail("John.Doe@example.org");
        QRCode.from(email).file();

        // encode mms data
        MMS mms = new MMS("8675309", "Hello Jenny");
        QRCode.from(mms).file();

        // encode sms data
        SMS sms = new SMS("8675309", "Hello Jenny");
        QRCode.from(sms).file();

        // encode MeCard data
        MeCard janeDoe = new MeCard("Jane Doe");
        janeDoe.setEmail("john.doe@example.org");
        janeDoe.setAddress("John Doe Street 1, 5678 Doestown");
        janeDoe.setTelephone("1234");
        QRCode.from(janeDoe).file();

        // if using special characters don't forget to supply the encoding
        VCard johnSpecial = new VCard("Jöhn Dɵe")
                .setAddress("ëåäöƞ Sträät 1, 1234 Döestüwn");
        QRCode.from(johnSpecial).withCharset("UTF-8").file();
    }
}

Java SE only

When using java you can create svg files via .svg() terminal operator:

File file = QRCode.from("www.example.org").svg();
File file = QRCode.from("www.example.com").withSize(250, 250).withColor(30, 90).svg();

It's also possible to write svg to an OutputStream with terminal operation:

OutputStream outs = // ...
QRCode.from("www.example.org").svg(outs);

Android only

On Android you have a special method bitmap() which returns a android.graphics.Bitmap without creating a File object before, so you can use the generated android.graphics.Bitmap immediately inside an ImageView:

Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

License:

http://www.apache.org/licenses/LICENSE-2.0.html

最近版本更新:(数据更新于 2024-10-20 12:25:05)

2022-11-29 02:50:35 3.0.1

2022-11-24 22:36:20 2.7.0

2022-11-29 04:24:06 3.0.0

2019-07-18 16:28:02 2.6.0

2018-06-16 15:55:57 2.5.0

2018-01-06 07:13:25 2.4.0

2017-12-22 04:40:59 1.3.0

2017-12-22 04:39:57 1.4.0

2017-12-22 04:36:50 2.0.0

2017-12-22 04:21:55 2.1.0

kenglxn/QRGen同语言 Java最近更新仓库

2024-12-20 19:54:15 mybatis-flex/mybatis-flex

2024-12-19 21:53:46 spring-projects/spring-boot

2024-12-19 21:25:16 Stirling-Tools/Stirling-PDF

2024-12-19 16:28:14 Tornaco/Thanox

2024-12-19 10:42:01 alibaba/nacos

2024-12-18 20:58:34 dromara/forest