资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java图片处理代码 java编程图片

用java处理图片,使图片像素和长宽成比例变小,请各位高手帮忙解决一下,急

90 * 90的图片

创新互联主要从事网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务五华,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

应该是分成9个10 * 10的吧

static Image createImage(Image image, int x, int y, int width, int height, int transform)

Image类里面自带方法创建分割图片

可以这样创建:

Image imgBase = Image.createImage("/*.png");

Image img[] = new Image[9];

for(int i = 0; i 9; i++)

{

img[i] = Image.createImage(imgBase, (i % 3) * 10, (i / 3) * 10, 10, 10, Sprite.TRANS_NONE); //参数分别是:源图片,截取的X坐标,Y坐标,宽,高,翻转类型

}

这样就可以了

当然以上代码需要放在try里面

如果想分成其他的小图片,可以按照需要变动坐标和宽高等参数

java 图片处理。如题。。

每次画图之前先用背景色填充一个框就可以啊

g2d.setColor(Color.white);//设背景色

g2d.fillRect(0,0,600,600);//填充整个窗口

java怎么处理获得内切圆图片

java处理获得内切圆图片的代码如下:

public static void main(String[] args) throws IOException {

BufferedImage bi1 = ImageIO.read(new File("d:/1.jpg"));

// 根据需要是否使用 BufferedImage.TYPE_INT_ARGB

BufferedImage image = new BufferedImage(bi1.getWidth(),

bi1.getHeight(), BufferedImage.TYPE_INT_ARGB);

Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1

.getHeight());

Graphics2D g2 = image.createGraphics();

image = g2.getDeviceConfiguration().createCompatibleImage(

bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT);

g2 = image.createGraphics();

g2.setPaint(new Color (255,255,255));

g2.fill(new Rectangle(image.getWidth(), image.getHeight()));

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));

g2.setClip(shape);

// 使用 setRenderingHint 设置抗锯齿

g2.drawImage(bi1, 0, 0, null);

g2.dispose();

try {

ImageIO.write(image, "PNG", new File("d:/2.png"));

} catch (IOException e) {

e.printStackTrace();

}

}

怎么用java代码模拟一张图片

用java代码模拟一张图片可以这样操作:1.创建BufferedImage类

2.根据BufferedImage类得到一个Graphics2D对象

3.根据Graphics2D对象进行逻辑操作

4.处理绘图

5.将绘制好的图片写入到图片

Java图片处理 文字水印 图片水印 缩放 补白

package util;

import java awt AlphaComposite;

import java awt Color;

import java awt Font;

import java awt Graphics D;

import java awt Image;

import java awt geom AffineTransform;

import java awt image AffineTransformOp;

import java awt image BufferedImage;

import java io File;

import java io IOException;

import javax imageio ImageIO;

/**

* @author Eric Xu

*

*/

public final class ImageUtils {

/**

* 图片水印

* @param pressImg 水印图片

* @param targetImg 目标图片

* @param x 修正值 默认在中间

* @param y 修正值 默认在中间

* @param alpha 透明度

*/

public final static void pressImage(String pressImg String targetImg int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIO read(img);

int wideth = src getWidth(null);

int height = src getHeight(null);

BufferedImage image = new BufferedImage(wideth height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g drawImage(src wideth height null);

//水印文件

Image src_biao = ImageIO read(new File(pressImg));

int wideth_biao = src_biao getWidth(null);

int height_biao = src_biao getHeight(null);

g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));

g drawImage(src_biao (wideth wideth_biao) / (height height_biao) / wideth_biao height_biao null);

//水印文件结束

g dispose();

ImageIO write((BufferedImage) image jpg img);

} catch (Exception e) {

e printStackTrace();

}

}

/**

* 文字水印

* @param pressText 水印文字

* @param targetImg 目标图片

* @param fontName 字体名称

* @param fontStyle 字体样式

* @param color 字体颜色

* @param fontSize 字体大小

* @param x 修正值

* @param y 修正值

* @param alpha 透明度

*/

public static void pressText(String pressText String targetImg String fontName int fontStyle Color color int fontSize int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIO read(img);

int width = src getWidth(null);

int height = src getHeight(null);

BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g drawImage(src width height null);

g setColor(color);

g setFont(new Font(fontName fontStyle fontSize));

g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));

g drawString(pressText (width (getLength(pressText) * fontSize)) / + x (height fontSize) / + y);

g dispose();

ImageIO write((BufferedImage) image jpg img);

} catch (Exception e) {

e printStackTrace();

}

}

/**

* 缩放

* @param filePath 图片路径

* @param height 高度

* @param width 宽度

* @param bb 比例不对时是否需要补白

*/

public static void resize(String filePath int height int width boolean bb) {

try {

double ratio = ; //缩放比例

File f = new File(filePath);

BufferedImage bi = ImageIO read(f);

Image itemp = bi getScaledInstance(width height bi SCALE_SMOOTH);

//计算比例

if ((bi getHeight() height) || (bi getWidth() width)) {

if (bi getHeight() bi getWidth()) {

ratio = (new Integer(height)) doubleValue() / bi getHeight();

} else {

ratio = (new Integer(width)) doubleValue() / bi getWidth();

}

AffineTransformOp op = new AffineTransformOp(AffineTransform getScaleInstance(ratio ratio) null);

itemp = op filter(bi null);

}

if (bb) {

BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g setColor(Color white);

g fillRect( width height);

if (width == itemp getWidth(null))

g drawImage(itemp (height itemp getHeight(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);

else

g drawImage(itemp (width itemp getWidth(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);

g dispose();

itemp = image;

}

ImageIO write((BufferedImage) itemp jpg f);

} catch (IOException e) {

e printStackTrace();

}

}

public static void main(String[] args) throws IOException {

pressImage( D:\\shuiyin png D:\\test jpg f);

pressText( 我是文字水印 D:\\test jpg 微软雅黑 Color white f);

resize( D:\\test jpg true);

}

public static int getLength(String text) {

int length = ;

for (int i = ; i text length(); i++) {

if (new String(text charAt(i) + ) getBytes() length ) {

length += ;

} else {

length += ;

}

}

return length / ;

}

lishixinzhi/Article/program/Java/hx/201311/26874

关于图片处理的,这段java代码 怎么实现功能的?设计思想 算法思想 大家说说,我会再加分的 谢谢

按冒泡排序思想,有8颗豆子(大小不一)放在8袋子里, 从第1个袋了拿出豆子,与第2个袋子里的豆子相比较,如果第2个袋子里豆子比第1个袋子的豆子大,就把第2个袋子里的豆子放入第1个袋子,把第1个袋子的豆子放入第2个袋子。然后,第1个袋子继续和第3个袋子比较。如果第2袋子的豆子不会比第1个袋子的大,就和第3个袋子比较,这样一一个下直到和所有的袋子比较过。之后第2个袋子也与第2个袋子以后的相比较过.......!

另外还要用到交换的方法:

代码主要是用循环嵌套:

public class NewMain {

public static void main(String[] args) {

// TODO code application logic here

int a[]={1,5,8,11,16,30,40,50,199};//定义一个数组,也可写成 int[] a={1,5,8,11,16,30,40,50,199};

int c;//用于交换用的

for(int i=0;ia.length;i++){ //a.length 数组的长度

for(int j=i+1;ja.length;j++){

if(a[j]a[i]){ // 进行交换

c=a[j];

a[j]=a[i];

a[i]=c;

}

}

System.out.println(a[i]);

}

}


网站标题:java图片处理代码 java编程图片
分享链接:http://cdkjz.cn/article/hhjeej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220