2016-11-19 49 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

我會盡快完成。感謝提示。我剛成爲堆棧溢出的活躍用戶。 –