让Google为你生成圆角图片

注:此文首发于煎蛋,原作者为sfufoet (专栏)。

现在很多网站、博客都使用了圆角效果。圆角效果让整个页面看起来不会很死板。实现圆角效果,可以直接使用 CSS 特效来实现,也有使用图片实现。cornershop,就是一个自动生成圆角效果网页的自动生成器。

  Zach 就是一个狂热的圆角爱好者,就是 cornershop 让 Zach 变成这样的——他每次在网上看到漂亮的圆角效果总要习惯性地点击右键,查看源代码。他就是想知道那些圆角是用了 javascript, GIF, PNG ,是手工做成的还是自动生成的。

某次他路过 Google Group 的时候,习惯性地查看源代码。发现 Google 用了这样的链接来自动生成圆角:http://groups-beta.google.com/groups/roundedcorners?c=999999&bc=white&w=4&h=4&a=tr
你点击上面的链接就看到那个圆角图片了。
   c 的值代表圆角颜色,你可以使用颜色的名称来表示,也可以使用颜色的RGB代码来表示,不用带 # 符号;
   bc 的值代表圆角的背景颜色;
   w 的值分别代表圆角的宽;
   h 的值分别代表圆角的高;
   a 的值代表圆角所在的方向,tl是左上角,tr是右上角,bl是左下角,br是右下角。

  参数解释:

  右图用的参数是这样的:
  c=FF0000   bc=white   w=150   h=100   a=tr

  -_____-
  Google肯定是用了一个椭圆函数来生成圆角图片的。

 

要将Java中的一张图片生成为二维码,可以使用ZXing库。以下是一个示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class ImageToQRCode { public static void main(String[] args) throws Exception { String imagePath = "path/to/image.jpg"; String qrCodePath = "path/to/qrcode.png"; String qrCodeText = "This is the text to be encoded into QR code."; // Read the image BufferedImage image = ImageIO.read(new File(imagePath)); // Encode the text into QR code QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, image.getWidth(), image.getHeight(), hints); // Convert the QR code into an image int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage qrCodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { qrCodeImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } // Combine the image and the QR code BufferedImage combinedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); combinedImage.getGraphics().drawImage(image, 0, 0, null); combinedImage.getGraphics().drawImage(qrCodeImage, (image.getWidth() - width) / 2, (image.getHeight() - height) / 2, null); // Save the combined image ImageIO.write(combinedImage, "png", new File(qrCodePath)); } } ``` 这段代码将会读取指定路径下的图片,将指定文本编码为二维码,并将二维码插入到图片中央,最后将生成图片保存为PNG格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值