① 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