Giter Site home page Giter Site logo

zhe-si / multiavatar-java Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 33 KB

Names-based avatars (identicons) in vector SVG and raster PNG formats for your website, community, or an app. An unofficial implementation of Multiavatar in Java. 为您的网站、社区或应用程序提供海量基于名称的头像,支持 svg 和 png。Multiavatar 在 Java 语言的非官方实现。

Home Page: https://multiavatar.com/

Java 100.00%
avatar generator java multiavatar

multiavatar-java's Introduction

Multiavatar for Java

Multiavatar is a multicultural avatar generator.

In total, it is possible to generate 12,230,590,464 cryptographically unique avatars.

Initially coded in JavaScript, this version of Multiavatar is re-created in Java.

For more details about the Multiavatar Generator, please refer to the readme available in the JS repository.

为您的网站、社区或应用程序提供海量基于名称的头像。Multiavatar 在 Java 语言的非官方实现。

Installation

Install Multiavatar with download jar:

  • Download release jar and import in project.
  • If use maven, you can copy multiavatar-1.0.0.jar into lib folder of project directory and add this config in pom.xml.
    <dependency>
        <groupId>com.zhesi</groupId>
        <artifactId>multiavatar</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/multiavatar-1.0.0.jar</systemPath>
    </dependency>
    

Usage

String avatarSvg = MultiavatarGen.genMultiavatarSvg("Binx Bond", false, null);
Files.write(Path.of("targetpath/xxx.svg"), avatarSvg.getBytes(StandardCharsets.UTF_8));

Is equal to this.

byte[] avatarSvgBytes = MultiavatarGen.genAvatarSvg("Binx Bond");
Files.write(Path.of("targetpath/xxx.svg"), avatarSvgBytes);

For advanced usage, pass boolean true as the second parameter if you wish to generate an avatar without the environment part.

Pass a dictionary as the third parameter to generate a specific avatar version.

String avatarSvg = MultiavatarGen.genMultiavatarSvg("Binx Bond", true, Map.of(
        "part", "11",
        "theme", "C"
));

If you want to generate a png type avatar picture, you can use the batik library of apache to convert the svg picture, you can refer to the following methods.

First, introduce the batik dependency.

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>batik-all</artifactId>
    <version>1.16</version>
</dependency>

You can refer to the tool class for converting svg to png images below.

/**
 * <b>SvgHelper</b>
 * <p>
 * svg 工具类
 * - svg 转 png
 * </p>
 *
 * @author zhe-si
 * @version 1.0
 */
public class SvgHelper {
  public static byte[] convertSvgFile2Png(byte[] svgBytes) {
    ByteArrayInputStream svgInStream = new ByteArrayInputStream(svgBytes);
    byte[] bytes = convertSvgFile2Png(svgInStream);
    try {
      svgInStream.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return bytes;
  }

  public static byte[] convertSvgFile2Png(InputStream svgInStream) {
    ByteArrayOutputStream pngOutStream = new ByteArrayOutputStream();
    convert2Png(svgInStream, pngOutStream);
    byte[] byteArray = pngOutStream.toByteArray();
    try {
      pngOutStream.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return byteArray;
  }

  public static void convert2Png(InputStream svgInStream, OutputStream pngOutStream) {
    Transcoder tr = new PNGTranscoder();
    try {
      TranscoderInput input = new TranscoderInput(svgInStream);
      TranscoderOutput output = new TranscoderOutput(pngOutStream);
      tr.transcode(input, output);
    } catch (TranscoderException e) {
      throw new RuntimeException(e);
    }
  }
}

A complete example of generating png is as follows.

byte[] avatarSvgBytes = MultiavatarGen.genAvatarSvg("Binx Bond");
byte[] avatarPngBytes = SvgHelper.convertSvgFile2Png(avatarSvgBytes);

Testing

To catch bugs, the representation of tests should be visual because not all bugs have programmatic errors. For example, if an equal length color array is mixed, or a double semicolon appears in a color string, in such cases an error is not thrown, but the visual representation of avatar(-s) becomes broken.

There are two types of tests, currently available in index.html and other.html files. Simply open these files in your browser. Build these files by running the Python build scripts in the tests folder.

In the index.html file, visually presented are all 48 base versions or avatars. If all 48 base versions are good, then it means that all 12 billion are also good, because the 12 billion are constructed from different parts of the 48 base versions. All avatars in this file should look exactly the same as in the JavaScript repository's \svg\index.html file.

In the other.html file, additional various tests are performed to test different approaches how to generate avatars, specific avatar versions, or some repository-specific cases.

API

Alternatively to this Python script, you can use Multiavatar through the web API.

To get an avatar as SVG code, add the avatar's ID to the URL:

https://api.multiavatar.com/Binx Bond

To get an avatar as SVG file, add .svg to the end of the URL:

https://api.multiavatar.com/Binx Bond.svg

To get an avatar as PNG file, add .png to the end of the URL:

https://api.multiavatar.com/Binx Bond.png

License

You can use Multiavatar for free, as long as the conditions described in the LICENSE are followed.

Screenshots

More info

For additional information and extended functionality, visit the multiavatar.com web-app.

The app is based on static html for the home page, and on Laravel 8 + Vue.js for extended functionality, including the web store.

The product mockup generator for the Merch Maker is based on the ImageMagick PHP library.

multiavatar-java's People

Contributors

zhe-si avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.