本文目录一览:
- 1、JAVA图片输出
- 2、用JAVA打印出图片中的图形,求完整代码。
- 3、java如何调用本地打印机进行图片打印
- 4、java如何调用打印机打印图片
- 5、怎样用JAVA编写一个小应用程序,输出一张图片或自己的照片
- 6、java怎么在控制台输出一张jpg的图片
JAVA图片输出
等着拿分.......
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* @author alanwei
*
*/
public class Test {
public static BufferedImage createImage(int width, int height, String s) {
Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int)x, (int)baseY);
return bi;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedImage image = createImage(100, 20, "123456789");
File file = new File("image.jpg");
if (!file.exists()) {
file.createNewFile();
}
if (image != null) {
ImageIO.write(image, "jpg", file);
}
}
}
用JAVA打印出图片中的图形,求完整代码。
您好,您这样:
public class search {
public int counter(String inputs, String word) {
int counter = 0;
for (int i = 0; i inputs.length(); i++) {
if(word.equals(inputs.charAt(i)+"")){
counter++;
}
}
return counter;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = sc.next();
System.out.print("请输入要查找的字符:");
String s = sc.next();
search sear = new search();
int c = sear.counter(str, s);
System.out.println("\"" + str + "\"中包含" + c + "个\"爱\"");
}
}
java如何调用本地打印机进行图片打印
用java实现本地打印:java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出。
下面代码实现了简单的打印功能:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.AttributedString;
import javax.swing.JApplet;
public class PrintTest implements Printable{
/**
* @param Graphic指明打印的图形环境
* @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×842点)
* @param pageIndex指明页号
**/
public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
System.out.println("pageIndex="+pageIndex);
Component c = null;
//print string
String str = "中华民族是勤劳、勇敢和富有智慧的伟大民族。";
//转换成Graphics2D
Graphics2D g2 = (Graphics2D) gra;
//设置打印颜色为黑色
g2.setColor(Color.black);
//打印起点坐标
double x = pf.getImageableX();
double y = pf.getImageableY();
switch(pageIndex){
case 0:
//设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
//Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
Font font = new Font("新宋体", Font.PLAIN, 9);
g2.setFont(font);//设置字体
//BasicStroke bs_3=new BasicStroke(0.5f);
float[] dash1 = {2.0f};
//设置打印线的属性。
//1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
//g2.setStroke(bs_3);//设置线宽
float heigth = font.getSize2D();//字体高度
System.out.println("x="+x);
// -1- 用Graphics2D直接输出
//首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
//g2.drawLine(10,10,200,300);
Image src = Toolkit.getDefaultToolkit().getImage("D:\\EclipseWorkSpace3.1\\Kfc-wuxi\\WebRoot\\image\\KFC.jpg");
g2.drawImage(src,(int)x,(int)y,c);
int img_Height=src.getHeight(c);
int img_width=src.getWidth(c);
//System.out.println("img_Height="+img_Height+"img_width="+img_width) ;
g2.drawString(str, (float)x, (float)y+1*heigth+img_Height);
g2.drawLine((int)x,(int)(y+1*heigth+img_Height+10),(int)x+200,(int)(y+1*heigth+img_Height+10));
g2.drawImage(src,(int)x,(int)(y+1*heigth+img_Height+11),c);
return PAGE_EXISTS;
default:
return NO_SUCH_PAGE;
}
}
public static void main(String[] args) {
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(590,840);//纸张大小
p.setImageableArea(10,10, 590,840);//A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new PrintTest(), pf);
//获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 设置打印类
job.setPageable(book);
try {
//可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
//boolean a=job.printDialog();
//if(a)
//{
job.print();
//}
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
java如何调用打印机打印图片
package com.iba.cxx.adm.controller;import javax.print.*;import javax.print.attribute.DocAttributeSet;import javax.print.attribute.HashDocAttributeSet;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.swing.*;import java.io.File;import java.io.FileInputStream;/** * Created by Administrator on 2017/9/8 0008. */public class TestController { public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser(); //创建打印作业 int state = fileChooser.showOpenDialog(null); if(state == fileChooser.APPROVE_OPTION){ // File file = new File("D:/haha.txt"); //获取选择的文件 File file = fileChooser.getSelectedFile();//获取选择的文件 //构建打印请求属性集 HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); //设置打印格式,因为未确定类型,所以选择autosense DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; //查找所有的可用的打印服务 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); //定位默认的打印服务 PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); //显示打印对话框 PrintService service = ServiceUI.printDialog(null, 200, 200, printService,
defaultService, flavor, pras); if(service != null){ try {
DocPrintJob job = service.createPrintJob(); //创建打印作业 FileInputStream fis = new FileInputStream(file); //构造待打印的文件流 DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
怎样用JAVA编写一个小应用程序,输出一张图片或自己的照片
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class text
{
JFrame jf;
JLabel jl;
JPanel jp1;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
jf=new JFrame();
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp1.add(jl);
jf.getContentPane().add(jp1);
jf.setVisible(true);
}
}
把你的找一张gif 格式的照片 .名字改成1.gif 放在你的项目根目录下
java怎么在控制台输出一张jpg的图片
输出图片的base64编码
//imgFile是图片的路径
public static void getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} // 加密
BASE64Encoder encoder = new BASE64Encoder();
System.out.println(encoder.encode(data));
}