Changeset 52a37d17


Ignore:
Timestamp:
Feb 13, 2016 8:13:12 PM (4 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
27d0a81
Parents:
23615579
Message:

Imagegen: Add text to QR codes

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • apps/imagegen/imagegen/webapp/src/main/java/net/i2p/imagegen/QRServlet.java

    r23615579 r52a37d17  
    11package net.i2p.imagegen;
    22
     3/* contains code adapted from jrobin: */
     4/*******************************************************************************
     5 * Copyright (c) 2001-2005 Sasa Markovic and Ciaran Treanor.
     6 * Copyright (c) 2011 The OpenNMS Group, Inc.
     7 *
     8 * This library is free software; you can redistribute it and/or
     9 * modify it under the terms of the GNU Lesser General Public
     10 * License as published by the Free Software Foundation; either
     11 * version 2.1 of the License, or (at your option) any later version.
     12 *
     13 * This library is distributed in the hope that it will be useful,
     14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16 * Lesser General Public License for more details.
     17 *
     18 * You should have received a copy of the GNU Lesser General Public
     19 * License along with this library; if not, write to the Free Software
     20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     21 *******************************************************************************/
     22
     23import java.awt.Color;
     24import java.awt.Font;
     25import java.awt.Graphics2D;
     26import java.awt.RenderingHints;
     27import java.awt.image.BufferedImage;
    328import java.io.ByteArrayOutputStream;
    429import java.io.IOException;
    530
     31import javax.imageio.ImageIO;
    632import javax.servlet.ServletConfig;
    733import javax.servlet.ServletException;
     
    2046import com.google.zxing.qrcode.QRCodeWriter;
    2147
     48import net.i2p.util.SystemVersion;
     49
    2250/**
    2351 * This servlet generates QR code images.
     
    3361        private static final String PARAM_IDENTICON_SIZE_SHORT = "s";
    3462        private static final String PARAM_IDENTICON_CODE_SHORT = "c";
     63        private static final String PARAM_IDENTICON_TEXT_SHORT = "t";
    3564        private static final String IDENTICON_IMAGE_FORMAT = "PNG";
    3665        private static final String IDENTICON_IMAGE_MIMETYPE = "image/png";
    3766        private static final long DEFAULT_IDENTICON_EXPIRES_IN_MILLIS = 24 * 60 * 60 * 1000;
     67        // TODO the fonts all look terrible. See also the rendering hints below, nothing helps
     68        private static final String DEFAULT_FONT_NAME = SystemVersion.isWindows() ?
     69                                                        "Lucida Sans Typewriter" : Font.SANS_SERIF;     
     70        private static final Font DEFAULT_LARGE_FONT = new Font(DEFAULT_FONT_NAME, Font.BOLD, 16);
     71
    3872        private int version = 1;
    3973        private IdenticonCache cache;
     
    111145                                        throw new IOException("encode failed", we);
    112146                                }
    113                                 MatrixToImageWriter.writeToStream(matrix, IDENTICON_IMAGE_FORMAT, byteOut);
     147                                String text = request.getParameter(PARAM_IDENTICON_TEXT_SHORT);
     148                                if (text != null) {
     149                                        BufferedImage bi = MatrixToImageWriter.toBufferedImage(matrix);
     150                                        Graphics2D g = bi.createGraphics();
     151                                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     152                                        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     153                                        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
     154                                        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
     155                                        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
     156                                        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
     157                                        Font font = DEFAULT_LARGE_FONT;
     158                                        g.setFont(font);
     159                                        // doesn't work
     160                                        Color color = Color.RED;
     161                                        g.setColor(color);
     162                                        int width = bi.getWidth();
     163                                        int height = bi.getHeight();
     164                                        double swidth = font.getStringBounds(text, 0, text.length(),
     165                                                                             g.getFontRenderContext()).getBounds().getWidth();
     166                                        int x = (width - (int) swidth) / 2;
     167                                        int y = height - 10;
     168                                        g.drawString(text, x, y);
     169                                        if (!ImageIO.write(bi, IDENTICON_IMAGE_FORMAT, byteOut))
     170                                                throw new IOException("ImageIO.write() fail");
     171                                } else {
     172                                        MatrixToImageWriter.writeToStream(matrix, IDENTICON_IMAGE_FORMAT, byteOut);
     173                                }
    114174                                imageBytes = byteOut.toByteArray();
    115175                                if (cache != null)
  • apps/imagegen/imagegen/webapp/src/main/webapp/index.html

    r23615579 r52a37d17  
    2020
    2121<h2>QR Test</h2>
    22 <img src="qr?c=https%3a%2f%2fgeti2p.net%2f&amp;s=128" width=128 height=128>
    23 <img src="qr?c=https%3a%2f%2fgeti2p.net%2f&amp;s=256" width=256 height=256>
     22<img src="qr?c=https%3a%2f%2fgeti2p.net%2f&amp;s=128&t=geti2p.net" width=128 height=128>
     23<img src="qr?c=https%3a%2f%2fgeti2p.net%2f&amp;s=256&t=geti2p.net" width=256 height=256>
    2424
    2525<h2>Random Art Test</h2>
  • apps/susidns/src/jsp/details.jsp

    r23615579 r52a37d17  
    145145<img src="/imagegen/id?s=320&amp;c=<%=addr.getB64().replace("=", "%3d")%>" width="320" height="320">
    146146</td><td>
    147 <img src="/imagegen/qr?s=320&amp;c=http%3a%2f%2f<%=addr.getName()%>%2f%3fi2paddresshelper%3d<%=addr.getDestination()%>">
     147<img src="/imagegen/qr?s=320&amp;t=<%=addr.getName()%>&amp;c=http%3a%2f%2f<%=addr.getName()%>%2f%3fi2paddresshelper%3d<%=addr.getDestination()%>">
    148148</td></tr></table></div>
    149149<%
  • history.txt

    r23615579 r52a37d17  
     12016-02-13 zzz
     2 * I2PTunnel:
     3   - Add options to block by referer and user-agent
     4   - Log blocked destinations in b32, not b64
     5 * Imagegen: Add text to QR codes
     6
     72016-02-10 zzz
     8 * Crypto: Use new internal key generation instead of calling
     9   out to keytool; save CRL for new su3 amd family keys
     10
    1112016-02-09 zzz
    212 * Crypto: Fix raw (su3) signing, broken in test2 prop (-2)
  • router/java/src/net/i2p/router/RouterVersion.java

    r23615579 r52a37d17  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 4;
     21    public final static long BUILD = 5;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.