2016-11-19 68 views
0

我见过很多关于这个的问题,但都是C#。它们都不是Java,我也找不到适合的库。如何使用Java或Android创建identicon?

image

什么库可以为我通过编程给它一个串/散做到这一点?这个算法实际上是在StackExchange上实现的。

回答

1

我解决了这个问题。

我用过Gravatar。我第一次的图像的链接,并存储它作为一个String这样的:

String identiconURL = "http://www.gravatar.com/avatar/" + userID + "?s=55&d=identicon&r=PG"; 

然后,我用滑翔:

Glide.with(ProfilePictureChooserActivity.this) 
     .load(identiconURL) 
     .centerCrop() 
     .into(imageView); 
0

你可以看看这个链接。还有就是你可以用它来生成你的identicons http://www.davidhampgonsalves.com/Identicons

Java的代码的代码是以下之一:

public static BufferedImage generateIdenticons(String text, int image_width, int image_height){ 
     int width = 5, height = 5; 

     byte[] hash = text.getBytes(); 

     BufferedImage identicon = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
     WritableRaster raster = identicon.getRaster(); 

     int [] background = new int [] {255,255,255, 0}; 
     int [] foreground = new int [] {hash[0] & 255, hash[1] & 255, hash[2] & 255, 255}; 

     for(int x=0 ; x < width ; x++) { 
      //Enforce horizontal symmetry 
      int i = x < 3 ? x : 4 - x; 
      for(int y=0 ; y < height; y++) { 
       int [] pixelColor; 
       //toggle pixels based on bit being on/off 
       if((hash[i] >> y & 1) == 1) 
        pixelColor = foreground; 
       else 
        pixelColor = background; 
       raster.setPixel(x, y, pixelColor); 
      } 
     } 

     BufferedImage finalImage = new BufferedImage(image_width, image_height, BufferedImage.TYPE_INT_ARGB); 

     //Scale image to the size you want 
     AffineTransform at = new AffineTransform(); 
     at.scale(image_width/width, image_height/height); 
     AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
     finalImage = op.filter(identicon, finalImage); 

     return finalImage; 
} 
+0

虽然这在理论上可以回答的问题,[这将是优选的] (//meta.stackoverflow.com/q/8259)在这里包含答案的基本部分,并提供参考链接。该链接提供了用于普通Java而不是用于Android的可能会混淆未来读者的内容。举一个例子,并将生成的位图设置为一个图像视图 –

+0

我会尽快完成。感谢提示。我刚成为堆栈溢出的活跃用户。 –