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) {
}