java.awt.image.BufferedImage
10年積累的網(wǎng)站建設、成都網(wǎng)站設計經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有廣昌免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
javax.imageio.ImageIO
//獲得Graphics
Graphics graphics = BufferedImage.getGraphics()
//將BufferedImage 寫到文件流中
ImageIO.write(BufferedImage , "JPEG", OutputStream);
1、下載生成二維碼所需要的jar包qrcode.jar;2、直接上生成二維碼的java代碼 //需要導入的包import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;import com.swetake.util.Qrcode; /** * 生成二維碼(QRCode)圖片 * @param content 二維碼圖片的內(nèi)容 * @param imgPath 生成二維碼圖片完整的路徑 * @param ccbpath 二維碼圖片中間的logo路徑 */ public static int createQRCode(String content, String imgPath,String ccbPath) { try { Qrcode qrcodeHandler = new Qrcode(); qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); qrcodeHandler.setQrcodeVersion(7); // System.out.println(content); byte[] contentBytes = content.getBytes("gb2312"); //構(gòu)造一個BufferedImage對象 設置寬、高 BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, 140, 140); // 設定圖像顏色 BLACK gs.setColor(Color.BLACK); // 設置偏移量 不設置可能導致解析出錯 int pixoff = 2; // 輸出內(nèi)容 二維碼 if (contentBytes.length 0 contentBytes.length 120) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i codeOut.length; i++) { for (int j = 0; j codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. "); return -1; } Image img = ImageIO.read(new File(ccbPath));//實例化一個Image對象。 gs.drawImage(img, 55, 55, 30, 30, null); gs.dispose(); bufImg.flush(); // 生成二維碼QRCode圖片 File imgFile = new File(imgPath); ImageIO.write(bufImg, "png", imgFile); }catch (Exception e){ e.printStackTrace(); return -100; } return 0; }
來自網(wǎng)友 孤獨青鳥的博客
1、代碼如下:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World!");
//主循環(huán)
? for(int i =10;i0;i--){
? ? ? //輸出空格
? ? ? for(int k=i;k0;k--){System.out.print(" ");}
//輸出數(shù)字
? ? ? for(int j=i;j=10;j++){
? ? ? ? ? System.out.print(j+" ");? ? ? ? ? ? ?
? ? ? }System.out.println(" ");
? }
}
}
2、效果如圖
用java代碼模擬一張圖片可以這樣操作:1.創(chuàng)建BufferedImage類
2.根據(jù)BufferedImage類得到一個Graphics2D對象
3.根據(jù)Graphics2D對象進行邏輯操作
4.處理繪圖
5.將繪制好的圖片寫入到圖片
分享標題:java代碼怎樣生成圖片 java生成圖片
網(wǎng)站地址:http://chinadenli.net/article16/dodohgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作、面包屑導航、App開發(fā)、網(wǎng)站導航、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)