private static final String FILEINPUT="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣.jpg"; private static final String FILEMARK="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/http_imgload.jpg"; private static final String FILEDEST1="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣1.jpg"; private static final String FILEDEST2="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣2.jpg";/*** 給圖片添加文字水印 * @param filePath 需要添加水印的圖片的路徑 * @param markContent 水印的文字 * @param markContentColor 水印文字的顏色 * @param qualNum 圖片質量 * @return 布爾類型*/public boolean createStringMark(String filePath,String markContent,Color markContentColor,float qualNum){ImageIcon imgIcon=new ImageIcon(filePath); Image theImg =imgIcon.getImage(); int width=theImg.getWidth(null); int height= theImg.getHeight(null); System.out.println(theImg); BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); Graphics2D g=bimage.createGraphics(); g.setColor(markContentColor); g.setBackground(Color.white); g.drawImage(theImg, 0, 0, null ); g.setFont(new Font(null,Font.BOLD,32)); //字體、字型、字型大小 g.drawString(markContent,width/10,height/10); //畫文字 g.dispose();try{FileOutputStream out=new FileOutputStream(FILEDEST1); //先用一個特定的輸出文件名 JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); param.setQuality(qualNum, true); encoder.encode(bimage, param); out.close();}catch(Exception e)
⑵ 在JAVA中,怎麼在圖片上寫文字
將圖片放在底層,讓後將整理好的文字挪到想要的位置上
⑶ 如何實現java畫圖程序,並可在已畫的圖上添加文字等,求大神T-T
只提指導,沒有源碼。
在 Java 中要自定義組件,一般是覆蓋掉 protected void paintComponent(Graphics g); 方法就可以了,對於你這個類似畫筆的程序,首先,整個畫布是一個自定義的繼承自像 JPanel 一樣的東西,不過我們需要覆蓋它的 paintComponent 方法,因為:一、我們需要在當滑鼠拖放一個東西時我們移動這個選中的圖形時記住它的位置;二、設定 label 時需要保存它的 Label。三、知道圖形的形狀。
,然後在 paintComponent 時依次畫出各個圖形來。
要繪圖:
1、直線,g.drawLine(x,y,x2,y2); // 參數分別是起止點坐標。
2、矩形,g.fillRect(x, y, w, h); // 參數分別是左上角坐標和寬及高。
3、橢圓,g.fillOval(x, y, w, h); // 參數分別是橢圓形的外切矩形的左上角坐標及寬和高,當w 和 h 相等時是個圓。
准備繪圖前 g.setColor() 設置前景色;先把整個畫布用 g.fillRect() 塗成白色,再分別畫各個圖形,最後繪制 Label 應該在畫圖形之後再做。
移動圖形,是給 畫布組件 addMouseMotionListener 來監聽事件的,在拖動時先通過 mouseDragged 事件的 MouseEvent.point 知道它的位置是在哪個圖形的內部,之後的移動就修改這個圖形的位置。
⑷ java swing,怎麼在圖片里讓別人添加文字
swing裡面有沒現成的控制項 只能自己寫一個
簡單舉例來說 可以繼承JPanel控制項寫個JDrawPanel
用BufferedImage image來裝載你需要修改的圖片
在JDrawPanel中重寫printComponents(Graphics g)方法,將image draw到背景上
在panel上加mouse事件 當點擊時彈出對話框 讓用戶輸入文字 然後調用image的 image.createGraphics().drawString(String s)方法 把文字加上去
再調用panel的repaint()方法 把文字顯示出來
還有Canvas組件還有一些別的組件可以使用 但是都需要自己加功能
⑸ java中導入一個圖片,然後在圖片上加上幾個文字再輸出保存,應該如何用代碼實現
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test{
public static void main(String[] args){
try {
File f1 = new File("a.jpg");
BufferedImage image = ImageIO.read(f1);
Graphics g = image.getGraphics();
g.setFont(new Font("Serif",Font.BOLD,20));
g.setColor(Color.red);
g.drawString("happy new year!", 10, 20);
File f2 = new File(".jpg");
ImageIO.write(image, "JPEG", f2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
⑹ java按鈕背景圖片上文字的問題
同問題,找了好久終於解決了。當你設置好Button的文本和背景圖後,在你的按鈕控制項代碼中加上btnNewButton.setHorizontalTextPosition(SwingConstants.CENTER);注意!!!btnNewButton是我按鈕的實例化對象名,用這個指令時把你的實例化按鈕對象名替換了btnNewButton就好了
⑺ 如何利用java在圖片上添加文字
最好能給出代碼 補充: 你的代碼我用過 多餘的字全部忽略 補充: //為了例子簡單,暫時用固定的文件名。
private static final String FILEINPUT="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣.jpg";
private static final String FILEMARK="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/http_imgload.jpg";
private static final String FILEDEST1="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣1.jpg";
private static final String FILEDEST2="C:/apache-tomcat-6.0.18/webapps/elePhoto/images/許慧欣2.jpg";/*** 給圖片添加文字水印
* @param filePath 需要添加水印的圖片的路徑
* @param markContent 水印的文字
* @param markContentColor 水印文字的顏色
* @param qualNum 圖片質量
* @return 布爾類型*/public boolean createStringMark(String filePath,String markContent,Color markContentColor,float qualNum){ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcon.getImage();
int width=theImg.getWidth(null);
int height= theImg.getHeight(null);
System.out.println(theImg);
BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
Graphics2D g=bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.setFont(new Font(null,Font.BOLD,32)); //字體、字型、字型大小
g.drawString(markContent,width/10,height/10); //畫文字
g.dispose();try{FileOutputStream out=new FileOutputStream(FILEDEST1); //先用一個特定的輸出文件名
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();}catch(Exception e)
⑻ Java中如何把文字放到圖片上面去
<style type="text/css">
.tip-tx{
height:25px;
width:122px;
text-align:center;
background-color: #666;
}
.tip-tx a {
color:#fff;
}
</style>
<img style="width: 120px;height: 120px;" src="" />
<div class="tip-tx"><a href="javascript:;" >圖片上傳</a></div>
⑼ 如何利用java在圖片上添加文字
// 讀取模板圖片內容
BufferedImage image = ImageIO.read(new FileInputStream("c:\\test.jpg"));
Graphics2D g = image.createGraphics();// 得到圖形上下文
g.setColor(Color.BLACK); // 設置畫筆顏色
// 設置字體
g.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, 15));// 寫入簽名
// 下面這一句中的43,image.getHeight()-10可以改成你要的坐標。
g.drawString("這是新加入的文字", 43, image.getHeight() - 10);
g.dispose();
FileOutputStream out = new FileOutputStream("c:\\test1.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
希望我的回答能幫助你 謝謝 呵呵 答案補充 肯定了 在一張紙上寫字 肯定會自動換行的 否則寫外面?邏輯都通不過去~ 答案補充 把你的意思說出了 是不是隨便的寫字然後就提交 就把字寫在了圖片上還是?多少字 有沒有約束? 答案補充 g.drawString("這是新加入的文字", 43, image.getHeight() - 10);
在這個做處理 你規定了字體的大小 那麼獲取字元的長度*大小 一次判斷不要大於寬度 否則image.getHeight() - 10++ 你試試 應該可以的 我這里沒有Eclipse 答案補充 public class mains {
/**
* @param args
*/
private static int fontsize = 15;
static String jj(String str)
{
String sContent = str;
sContent=sContent.replaceAll(" "," ");
sContent=sContent.replaceAll("<br/>","/");
sContent=sContent.replaceAll("<br>","/");
return sContent;
} 答案補充 static void hh(String str)
{
BufferedImage image;
try {
image = ImageIO.read(new FileInputStream("E:\\dian zi za /789.jpg"));
Graphics2D g = image.createGraphics();// 得到圖形上下文
g.setColor(Color.BLACK); // 設置畫筆顏色
// 設置字體
g.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, fontsize));// 寫入簽名
// 下面這一句中的43,image.getHeight()-10可以改成你要的坐標。
String text = jj(str); 答案補充 String [] text1 = text.split("/");
int h = image.getHeight();
int w = image.getWidth();
for(int i=0;i<text1.length;i++)
{
g.drawString(text1[i],0,fontsize+i*fontsize);
}
g.dispose();
FileOutputStream out = new FileOutputStream("E:\\dian zi za /789.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close(); 答案補充 public static void main(String[] args) {
// TODO Auto-generated method stub
hh("sdakljsdhww<br/>asdasd<br/>a");
}
}
試試 可以換行 答案補充 關於 圖片的尺寸以及字體是否超出 你自己試著解決下 不要過於依賴問
⑽ 如何在java的背景圖片上添加文字
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
public class DrawString extends Frame{
private static final int spacing = 20 ; //文字與圖片邊界的空白
private static final int wordNum = 7; //文字字型大小
private Color strColor;
private StringBuffer message;
private int pictureWidth;
private int pictureHight;
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(strColor);
StringBuffer msg = message;
int strNumPerLine = (pictureWidth - spacing) / wordNum;
int strLineNum = message.length() / strNumPerLine;
for(int l=strLineNum,n=0 ;l<0; l--){
msg.substring(n, strNumPerLine);
g.drawString(msg.toString(), wordNum, wordNum * strNumPerLine);
n = strNumPerLine;
}
g.setColor(c);
//this.draw(g);
}
public DrawString(Color strColor, StringBuffer message, int pictureWidth, int pictureHight) {
this.strColor = strColor;
this.message = message;
this.pictureWidth = pictureWidth;
this.pictureHight = pictureHight;
}
}