① java怎麼在圖片右邊加文字
1、讀取Java本地圖片。
2、讀取網路圖片。
3、創建Java畫筆。
4、添加文字水印。
5、這樣就可以在Java圖片右邊加文字。
② java在圖片上繪制文字,文字不完全在圖片內時自動向內移動
可使使用界面來解決的
javatable
1 /**
工具欄
JToolBar
採用從左開始的
FlowLayout
布局
*/
2 JToolBar toolBar = new JToolBar();
3 toolBar.setBorderPainted(false); //
不畫邊界
4 toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
5
6 /**
窗體採用動態的
BorderLayout
布局,通過獲取工具欄或狀態欄的復選標記
進行界面的動態調整
*/
7 JSplitPane splitPane = new JSplitPane();
8 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); //
設置統計窗口分隔條
的方向
9 splitPane.setDividerLocation(300); //
設置分隔條的位置
10 splitPane.setOneTouchExpandable(true);
11 JCheckBoxMenuItem toolBarItem = new JCheckBoxMenuItem("
工具欄
(T)",
true);
12 JLabel statusLabel = new JLabel("
當前統計目標
:");
13 JCheckBoxMenuItem statusBarItem = new JCheckBoxMenuItem("
狀態欄
(S)",
true);
14 /**
設置系統窗體布局並動態設置工具欄和狀態欄
*/
15 private void setLayout()
16 {
5
17 if (toolBarItem.getState() &&' statusBarItem.getState())
18 {
19 this.getContentPane().add(BorderLayout.NORTH, toolBar);
20 this.getContentPane().add(BorderLayout.CENTER, splitPane);
21 this.getContentPane().add(BorderLayout.SOUTH, statusLabel);
22 }
23 else if (toolBarItem.getState() && !statusBarItem.getState())
24 {
25 this.getContentPane().add(BorderLayout.NORTH, toolBar);
26 this.getContentPane().remove(statusLabel);
27 }
28 else if (statusBarItem.getState() && !toolBarItem.getState())
29 {
30 this.getContentPane().add(BorderLayout.SOUTH, statusLabel);
31 this.getContentPane().remove(toolBar);
32 }
33 else if (!toolBarItem.getState() && !statusBarItem.getState())
34 {
35 this.getContentPane().remove(toolBar);
36 this.getContentPane().remove(statusLabel);
37 }
38 this.show(); //
添加或移去組件後刷新界面
39 }
③ 如何利用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.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 在圖片中填寫文字,字體大小自適應自動換行
如果強制換行的話,就在要換行的文字處插入游標,然後按住alt+enter鍵,就可以了。至於能不能顯示兩行文字就看格子有沒有那麼高了。
⑥ java 如何讀取附加到圖片上的文字
圖片上的文字是沒法讀取的,以為這涉及到圖像處理。非常非常復雜!因為如果你非要讀取圖片上的文字,不是幾行代碼可以搞定的,首相從matlaB開始學,了解什麼是圖像處理。然後再開發相應的jar包。當然,你也可以使用相關的軟體工具,比如識圖軟體,通過讀取軟體的反饋也算是讀取了圖片上的文字
⑦ 如何在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;
}
}
⑧ 有關Java的awt或者swing實現在圖片上添加文字的處理(像素和字元串長度單位轉換的問題)
先用java.awt.Graphics2D.getFont()取得當前正在使用的字體f,java.awt.Graphics2D.getFontRenderContext()取得當前正在使用的渲染上下文frc
然後再用Rectangle2D rect=f.getStringBounds(str,frc)就可以得到你渲染這些文字所佔據的矩形,使用rect.getWidth(), rect,getHeight()就可以知道這個矩形的大小了。
反過來通過像素大小決定字元串的長度沒有現成的方法。如果你的字體是等寬的可以先用上面的方法求出一個字元的大小然後自己算,不然就只能先試試一個字元,再試試兩個字元……直到超過你要的長度就返回的辦法了(看起來效率比較低,不過實際使用的時候不是很影響性能)
⑨ java 實現圖片的文字識別
摘要圖像識別是目前很熱門的研究領域,涉及的知識很廣,包括資訊理論、模式識別、模糊數學、圖像編碼、內容分類等等。本文僅對使用Java實現了一個簡單的圖像文本二值處理,關於識別並未實現。
步驟
建立文本字元模板二值矩陣
對測試字元進行二值矩陣化處理
代碼
/*
* @(#)StdModelRepository.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package cn.e.ynu.sei.recognition.util;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;/** * Hold character charImgs as standard model repository.
* @author 88250
* @version 1.0.0.0, Mar 20, 2008
*/
public class StdModelRepository {
/** * hold character images
*/ List charImgs = new ArrayList();
/** * default width of a character
*/ static int width = 16 /** * default height of a character
*/ static int height = 28 /** * standard character model matrix
*/ public int[][][] stdCharMatrix = new int[27][width][height];
/** * Default constructor.
*/ public StdModelRepository() {
BufferedImage lowercase = null try {
lowercase = ImageIO.read(new File("lowercase.png"));
} catch (IOException ex) {
Logger.getLogger(StdModelRepository.class.getName()).
log(Level.SEVERE, null, ex);
}
for (int i = 0 i < 26 i++) {
charImgs.add(lowercase.getSubimage(i * width,
0,
width,
height));
}
for (int i = 0 i < charImgs.size(); i++) {
Image image = charImgs.get(i);
int[] pixels = ImageUtils.getPixels(image,
image.getWidth(null),
image.getHeight(null));
stdCharMatrix[i] = ImageUtils.getSymbolMatrix(pixels, 0).clone();
ImageUtils.displayMatrix(stdCharMatrix[i]);
}
}
}
/*
* @(#)ImageUtils.java
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package cn.e.ynu.sei.recognition.util;import java.awt.Image;import java.awt.image.PixelGrabber;import java.util.logging.Level;import java.util.logging.Logger;/** * Mainipulation of image data.
* @author 88250
* @version 1.0.0.3, Mar 20, 2008
*/
public class ImageUtils {
/** * Return all of the pixel values of sepecified <code>image< .>* @param image the sepecified image
* @param width width of the image
* @param height height of the image
* @return */ public static int[] getPixels(Image image, int width, int height) {
int[] pixels = new int[width * height];
try {
new PixelGrabber(image, 0, 0, width, height, pixels, 0, width).grabPixels();
} catch (InterruptedException ex) {
Logger.getLogger(ImageUtils.class.getName()).
log(Level.SEVERE, null, ex);
}
return pixels;
}
資源來自:
http://blog.csdn.net/chief1985/article/details/2229572