Advertisement
bronydell

[Java] Generator

Nov 28th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Desktop;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.RenderingHints;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.io.PrintWriter;
  13. import java.io.UnsupportedEncodingException;
  14. import java.net.URL;
  15. import java.util.Random;
  16.  
  17. import javax.imageio.ImageIO;
  18. import javax.swing.JFrame;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTextField;
  21. import javax.swing.JTextPane;
  22. import javax.swing.text.DefaultStyledDocument;
  23. import javax.swing.text.SimpleAttributeSet;
  24. import javax.swing.text.StyleConstants;
  25. import javax.swing.text.StyledDocument;
  26.  
  27.  
  28. public class Textificator {
  29.  
  30. BufferedImage mainimage;
  31. String[][] maintext;
  32.  
  33. public void Texttifizer(BufferedImage img) throws IOException
  34. {
  35. PrintWriter out = null;
  36. img = resize(img, img.getWidth()/2, img.getHeight()/2);
  37. try {
  38. out = new PrintWriter("Out.html");
  39.  
  40. } catch (FileNotFoundException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n<title>Результат</title>\n <link rel=\"stylesheet\" href=\"Out.css\" type=\"text/css\">\n<body style=\"background-color: black;\"><pre style=\"font: 10px/6px monospace; text-align: center;\">");
  45. maintext = new String[img.getHeight()][img.getWidth()];
  46.  
  47. for(int i=0;i<img.getHeight();i=i+3)
  48. {
  49. out.write("<br>");
  50. System.out.println(i);
  51. for(int j=0;j<img.getWidth();j=j+3)
  52. {
  53.  
  54. if(getColor(j, i, img)!=null)
  55. {
  56. Random r = new Random();
  57. maintext[i][j] = "<font color="+getColor(j, i, img)+">$</font>";
  58.  
  59. out.write(maintext[i][j]);
  60. }
  61. else
  62. {
  63. maintext[i][j] = " ";
  64. out.write(maintext[i][j]);}
  65. }
  66. out.write("</body>\n</html>");
  67.  
  68. }
  69. String htmlFilePath = "Out.html"; // path to your new file
  70. File htmlFile = new File(htmlFilePath);
  71.  
  72.  
  73. }
  74. public String getColor(int x, int y, BufferedImage img)
  75. {
  76.  
  77. int rgb = img.getRGB(x, y);
  78. int r = (rgb >> 16) & 0xFF;
  79. int g = (rgb >> 8) & 0xFF;
  80. int b = (rgb & 0xFF);
  81. int alpha = (rgb>>24) & 0xff;
  82. if(alpha<100)
  83. return null;
  84. System.out.println("R="+r+" G="+g+" B="+b+" Alpha="+alpha);
  85. Color color = new Color(r, g, b);
  86. String hex = String.format("#%02x%02x%02x", r, g, b);
  87. return hex;
  88. }
  89.  
  90. public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  91. int w = img.getWidth();
  92. int h = img.getHeight();
  93. BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
  94. Graphics2D g = dimg.createGraphics();
  95. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  96. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  97. g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
  98. g.dispose();
  99. return dimg;
  100. }
  101. public void Test()
  102. {
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement