导航:首页 > 编程语言 > java图片

java图片

发布时间:2022-02-10 11:33:04

A. java截取图片

呵呵,很明确的告诉你:可以!

代码半小时后出来!!!

……

终于出来了(呵呵,好像超过了半小时哈)且看代码:

importjava.awt.Color;

importjava.awt.Graphics;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

importjavax.imageio.ImageIO;

importjavax.swing.JApplet;

publicclassTestextendsJApplet{

Stringaddrs="F:\images\mm.bmp";//改成自己的图片路径

BufferedImagemm,child;

CutImageci;

publicTest(){

try{

mm=ImageIO.read(newFile(addrs));

}catch(IOExceptione){

System.out.println("图片读取失败!");

e.printStackTrace();

}

ci=newCutImage(mm);

child=ci.getChildImage(50,0,150,220);

}

publicvoidinit(){

}

publicvoidpaint(Graphicsg){

g.setColor(Color.red);

g.drawString("原图:",0,10);

g.drawImage(mm,20,10,this);

g.drawString("ci.getChildImage(50,0,150,220)截取后的图片",mm.getWidth()+30,10);

g.drawImage(child,mm.getWidth()+50,20,this);

}

}

importjava.awt.Image;

importjava.awt.image.BufferedImage;

publicclassCutImage{

privateBufferedImageimg;

privateBufferedImagechild;

publicCutImage(){

}

publicCutImage(BufferedImageim){

img=im;

}

publicCutImage(Imageim){

img=(BufferedImage)im;

}

publicvoidsetImg(BufferedImageimg){

this.img=img;

}

(intx,inty,intwidth,intheight){

intcw=width;

intch=height;

intpw=img.getWidth();

intph=img.getHeight();

if(pw<x+width){

System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");

cw=pw-x;

}

if(ph<y+height){

System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");

ch=ph-y;

}

child=newBufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB);

for(inti=0;i<ch;i++){

for(intj=0;j<cw;j++){

child.setRGB(j,i,img.getRGB(x+j,y+i));

}

}

returnchild;

}

}

呵呵,希望楼主能够满意哦,如果你愿意的话,稍微改一下代码就可以把截取的图片child报春到你的电脑上了。下面程序的运行效果吧!

B. java中的使用图片图标

你代码里面的这句:

image.class.getResource("image.jpg");

里面是相对于你创建的image类的,路径是相对于image类文件的,所以要把图片文件和image.classs类文件放在一起。

几年过去了。想必问这个问题的人现在早就对java很熟悉了,回答下给以后搜索到这个问题的人把吧

C. 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);
}
}
}

D. JAVA添加图片

JAVA是非常灵活的语言,你自己稍动一下手指就能编个控件的。
class ImageBox extends JLabel ...

E. JAVA 图片语句

使用jsp小脚本
<%
for(int i=0;i<num;i++){
%>

<img src="" >//在双引号中可以写路径也可以在for之前把图片路径存放在一个数组中,然后在双引号内写<%=数组[下标] %>
<%
}
%>
例如你可以这样写:
<%
String [] parth=new String[];//定义数组,用来存放图片路径,本代码中的图片路径为上一级文件夹的image文件夹中.
for(int i=0;i<parth.length;i++){
%>
<img src="<%=parth[i] %>">
<%} %>
也可把数组内容写为:String [] parth=new String[];
这样的话要在使用时这样写:<img src="../image/<%=parth[i] %>">

两中路径的写法是一样的.以后有什么可以交流的可以给我邮件. 加我好友

F. java语言怎样 删除本地图片

代码:

File file=new File("图片路径");
file.delete();

G. java窗口中加载图片

public class TestFrame {

public static void main(String[] args) {
new TestFrame().init();
}

private void init() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 800, 600);
JLabel label = new JLabel();
label.setSize(800, 600);
label.setIcon(new javax.swing.ImageIcon("D:\\Backup\\我的文档\\My Pictures\\20070804115158926.jpg")); // NOI18N
frame.getContentPane().add(label);
frame.setVisible(true);
}
}

label.setIcon(new javax.swing.ImageIcon("D:\\Backup\\我的文档\\My Pictures\\20070804115158926.jpg"));这一大段是图片路径,注意是双斜杠啊。
另外这个问题最麻烦的是怎么读图片,你也可以用ImageIO来读图,详细看下api。

H. java生成jpg图片 并且实现文字和图片混排

response.setHeader("Cache-Control","no-cache");
String str="";
String sum="";
for(int i=0;i<4;i++){
Random random=new Random();
int j=Math.round(random.nextFloat()*35);
char x=str.charAt(j);
sum+=x+"";
}
request.getSession().setAttribute("Code",sum);
BufferedImage bufferedImage=new BufferedImage(50,20,BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics2D=(Graphics2D)bufferedImage.getGraphics();
graphics2D.setColor(Color.blue);
graphics2D.fill3DRect(0,0,50,20,false);
graphics2D.setColor(Color.YELLOW);
graphics2D.drawString(sum,10,12);
response.setContentType("image/jpeg");
ServletOutputStream output;
try {
output = response.getOutputStream();
JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(output);
encoder.encode(bufferedImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I. java 加载图片的几种方式

第一种方法:

Image bg; //或者是BufferedImage bg;

Pool2(){

try{

String file="图片的路径";

bg =ImageIO.read(new File(file));

}catch(Exception e){

e.printStackTrace();

}

}

第二种方法:这种方法用BufferedReader就不行

Image background;

Pool2(){

try{

String file="文件的路径";

background=Toolkit.getDefaultToolkit().getImage(file);

}catch(Exception e){

e.printStackTrace();

}

}

另外我们有时喜欢把图片放在工程的src目录下这样访问这些图片时就要用下面的方法。以src下的image文件下的01.jpg为例

Image image=null;

image = ImageIO.read(getClass().getResourceAsStream("/image/01.jpg"));

J. 关于java图像处理

Java图像处理技巧四则
下面代码中用到的sourceImage是一个已经存在的Image对象

图像剪切
对于一个已经存在的Image对象,要得到它的一个局部图像,可以使用下面的步骤:

//import java.awt.*;
//import java.awt.image.*;
Image croppedImage;
ImageFilter cropFilter;
CropFilter =new CropImageFilter(25,30,75,75); //四个参数分别为图像起点坐标和宽高,即CropImageFilter(int x,int y,int width,int height),详细情况请参考API
CroppedImage= Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(sourceImage.getSource(),cropFilter));

如果是在Component的子类中使用,可以将上面的Toolkit.getDefaultToolkit().去掉。FilteredImageSource是一个ImageProcer对象。
图像缩放
对于一个已经存在的Image对象,得到它的一个缩放的Image对象可以使用Image的getScaledInstance方法:

Image scaledImage=sourceImage. getScaledInstance(100,100, Image.SCALE_DEFAULT); //得到一个100X100的图像
Image doubledImage=sourceImage. getScaledInstance(sourceImage.getWidth(this)*2,sourceImage.getHeight(this)*2, Image.SCALE_DEFAULT); //得到一个放大两倍的图像,这个程序一般在一个swing的组件中使用,而类Jcomponent实现了图像观察者接口ImageObserver,所有可以使用this。
//其它情况请参考API

灰度变换
下面的程序使用三种方法对一个彩色图像进行灰度变换,变换的效果都不一样。一般而言,灰度变换的算法是将象素的三个颜色分量使用R*0.3+G*0.59+ B*0.11得到灰度值,然后将之赋值给红绿蓝,这样颜色取得的效果就是灰度的。另一种就是取红绿蓝三色中的最大值作为灰度值。java核心包也有一种算法,但是没有看源代码,不知道具体算法是什么样的,效果和上述不同。

/* GrayFilter.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayFilter extends RGBImageFilter {
int modelStyle;
public GrayFilter() {
modelStyle=GrayModel.CS_MAX;
canFilterIndexColorModel=true;
}
public GrayFilter(int style) {
modelStyle=style;
canFilterIndexColorModel=true;
}
public void setColorModel(ColorModel cm) {
if (modelStyle==GrayModel
else if (modelStyle==GrayModel
}
public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}
/* GrayModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class GrayModel extends ColorModel {
public static final int CS_MAX=0;
public static final int CS_FLOAT=1;
ColorModel sourceModel;
int modelStyle;

public GrayModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=0;
}

public GrayModel(ColorModel sourceModel,int style) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
modelStyle=style;
}

public void setGrayStyle(int style) {
modelStyle=style;
}

protected int getGrayLevel(int pixel) {
if (modelStyle==CS_MAX) {
return Math.max(sourceModel.getRed(pixel),Math.max(sourceModel.getGreen(pixel),sourceModel.getBlue(pixel)));
}
else if (modelStyle==CS_FLOAT){
return (int)(sourceModel.getRed(pixel)*0.3+sourceModel.getGreen(pixel)*0.59+sourceModel.getBlue(pixel)*0.11);
}
else {
return 0;
}
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return getGrayLevel(pixel);
}

public int getGreen(int pixel) {
return getGrayLevel(pixel);
}

public int getBlue(int pixel) {
return getGrayLevel(pixel);
}

public int getRGB(int pixel) {
int gray=getGrayLevel(pixel);
return (getAlpha(pixel)<<24)+(gray<<16)+(gray<<8)+gray;
}
}

如果你有自己的算法或者想取得特殊的效果,你可以修改类GrayModel的方法getGrayLevel()。
色彩变换
根据上面的原理,我们也可以实现色彩变换,这样的效果就很多了。下面是一个反转变换的例子:

/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseColorModel extends ColorModel {
ColorModel sourceModel;
public ReverseColorModel(ColorModel sourceModel) {
super(sourceModel.getPixelSize());
this.sourceModel=sourceModel;
}

public int getAlpha(int pixel) {
return sourceModel.getAlpha(pixel);
}

public int getRed(int pixel) {
return ~sourceModel.getRed(pixel);
}

public int getGreen(int pixel) {
return ~sourceModel.getGreen(pixel);
}

public int getBlue(int pixel) {
return ~sourceModel.getBlue(pixel);
}

public int getRGB(int pixel) {
return (getAlpha(pixel)<<24)+(getRed(pixel)<<16)+(getGreen(pixel)<<8)+getBlue(pixel);
}
}
/* ReverseColorModel.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.image.*;
public class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
canFilterIndexColorModel=true;
}

public void setColorModel(ColorModel cm) {
substituteColorModel(cm,new ReverseColorModel(cm));
}

public int filterRGB(int x,int y,int pixel) {
return pixel;
}
}

要想取得自己的效果,需要修改ReverseColorModel.java中的三个方法,getRed、getGreen、getBlue。
下面是上面的效果的一个总的演示程序。

/*GrayImage.java*/
/*@author:cherami */
/*email:[email protected]*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.color.*;
public class GrayImage extends JFrame{
Image source,gray,gray3,clip,bigimg;
BufferedImage bimg,gray2;
GrayFilter filter,filter2;
ImageIcon ii;
ImageFilter cropFilter;
int iw,ih;

public GrayImage() {
ii=new ImageIcon(\"images/11.gif\");
source=ii.getImage();
iw=source.getWidth(this);
ih=source.getHeight(this);
filter=new GrayFilter();
filter2=new GrayFilter(GrayModel.CS_FLOAT);
gray=createImage(new FilteredImageSource(source.getSource(),filter));
gray3=createImage(new FilteredImageSource(source.getSource(),filter2));
cropFilter=new CropImageFilter(5,5,iw-5,ih-5);
clip=createImage(new FilteredImageSource(source.getSource(),cropFilter));
bigimg=source.getScaledInstance(iw*2,ih*2,Image.SCALE_DEFAULT);
MediaTracker mt=new MediaTracker(this);
mt.addImage(gray,0);
try {
mt.waitForAll();
} catch (Exception e) {
}

阅读全文

与java图片相关的资料

热点内容
数控铣床法兰克子程序编程 浏览:173
linux打包命令targz 浏览:996
抖音app是哪个 浏览:407
苹果app怎么上架 浏览:255
NA服务器地址 浏览:427
我的世界如何初始化服务器 浏览:97
哪个手机app天气预报最准 浏览:752
怎样把视频压缩至25m 浏览:570
vivox27文件夹怎么改变 浏览:727
新手玩狼人杀用什么app 浏览:615
pdf在线查看 浏览:954
安卓tv90如何关闭后台 浏览:683
php读取word乱码 浏览:755
minicom源码 浏览:1001
海尔冷柜压缩机 浏览:416
联通服务器如何调试信号 浏览:136
stata新命令 浏览:941
单调栈算法python 浏览:606
微信解压游戏怎么下载 浏览:962
忍三服务器不同如何登上账号 浏览:822