java图片比例缩小代码,java实现图片放大和缩小代码

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

90 * 90的图片

目前创新互联已为1000多家的企业提供了网站建设、域名、虚拟空间、网站托管、企业网站设计、道里网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

应该是分成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图片缩放

根据你的鼠标移动事件,判断你第一次点击的点和最后一次的点,就可以算出这个句型区域的长和宽了,

下面代码自己看

package com.itheima.util;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* 制作图片缩略图

*

* @author seawind

*

*/

public class PicUtils {

private String srcFile;

private String destFile;

private int width;

private int height;

private Image img;

/**

* 构造函数

*

* @param fileName

* String

* @throws IOException

*/

public PicUtils(String fileName) throws IOException {

File _file = new File(fileName); // 读入文件

this.srcFile = fileName;

// 查找最后一个.

int index = this.srcFile.lastIndexOf(".");

String ext = this.srcFile.substring(index);

this.destFile = this.srcFile.substring(0, index) + "_s" + ext;

img = javax.imageio.ImageIO.read(_file); // 构造Image对象

width = img.getWidth(null); // 得到源图宽

height = img.getHeight(null); // 得到源图长

}

/**

* 强制压缩/放大图片到固定的大小

*

* @param w

* int 新宽度

* @param h

* int 新高度

* @throws IOException

*/

public void resize(int w, int h) throws IOException {

BufferedImage _image = new BufferedImage(w, h,

BufferedImage.TYPE_INT_RGB);

_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图

FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(_image); // 近JPEG编码

out.close();

}

/**

* 按照固定的比例缩放图片

*

* @param t

* double 比例

* @throws IOException

*/

public void resize(double t) throws IOException {

int w = (int) (width * t);

int h = (int) (height * t);

resize(w, h);

}

/**

* 以宽度为基准,等比例放缩图片

*

* @param w

* int 新宽度

* @throws IOException

*/

public void resizeByWidth(int w) throws IOException {

int h = (int) (height * w / width);

resize(w, h);

}

/**

* 以高度为基准,等比例缩放图片

*

* @param h

* int 新高度

* @throws IOException

*/

public void resizeByHeight(int h) throws IOException {

int w = (int) (width * h / height);

resize(w, h);

}

/**

* 按照最大高度限制,生成最大的等比例缩略图

*

* @param w

* int 最大宽度

* @param h

* int 最大高度

* @throws IOException

*/

public void resizeFix(int w, int h) throws IOException {

if (width / height w / h) {

resizeByWidth(w);

} else {

resizeByHeight(h);

}

}

/**

* 设置目标文件名 setDestFile

*

* @param fileName

* String 文件名字符串

*/

public void setDestFile(String fileName) throws Exception {

if (!fileName.endsWith(".jpg")) {

throw new Exception("Dest File Must end with \".jpg\".");

}

destFile = fileName;

}

/**

* 获取目标文件名 getDestFile

*/

public String getDestFile() {

return destFile;

}

/**

* 获取图片原始宽度 getSrcWidth

*/

public int getSrcWidth() {

return width;

}

/**

* 获取图片原始高度 getSrcHeight

*/

public int getSrcHeight() {

return height;

}

}

java 图片缩放代码

直接给你一个类,直接套用就好了

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;

import java.awt.image.WritableRaster;

import java.io.File;

import javax.imageio.ImageIO;

public class Resize {

BufferedImage bufImage;

int width;

int height;

public Resize() {

// TODO Auto-generated constructor stub

}

public Resize(String srcPath,int width,int height) {

this.width = width;

this.height = height;

try{

this.bufImage = ImageIO.read(new File(srcPath));

}catch(Exception e){

e.printStackTrace();

}

}

public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){

BufferedImage bufTarget = null;

double sx = (double) width / srcBufImage.getWidth();

double sy = (double) height / srcBufImage.getHeight();

int type = srcBufImage.getType();

if(type == BufferedImage.TYPE_CUSTOM){

ColorModel cm = srcBufImage.getColorModel();

WritableRaster raster = cm.createCompatibleWritableRaster(width,

height);

boolean alphaPremultiplied = cm.isAlphaPremultiplied();

bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);

}else

bufTarget = new BufferedImage(width, height, type);

Graphics2D g = bufTarget.createGraphics();

g.setRenderingHint(RenderingHints.KEY_RENDERING,

RenderingHints.VALUE_RENDER_QUALITY);

g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));

g.dispose();

return bufTarget;

}

}

java如何实现把一个大图片压缩到指定大小的图片且长宽比不变

也就是图片压缩,可以不修改任何大小的压缩(速度快),也可等比例修改大小压缩(较慢)

下面这是一段等比例缩小图片的方法。

public String compressPic(String inputDir, String outputDir,

String inputFileName, String outputFileName, int width,

int height, boolean gp,String hzm) {

try {

if (!image.exists()) {

return "";

}

Image img = ImageIO.read(image);

// 判断图片格式是否正确

if (img.getWidth(null) == -1) {

return "no";

} else {

int newWidth; int newHeight;

// 判断是否是等比缩放

if (gp == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null)) / (double) width ;

double rate2 = ((double) img.getHeight(null)) / (double) height ;

// 根据缩放比率大的进行缩放控制

double rate = rate1 rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {

newWidth = img.getWidth(null); // 输出的图片宽度

newHeight = img.getHeight(null); // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的

* 优先级比速度高 生成的图片质量比较好 但速度慢

*/

Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

tag.getGraphics().drawImage(im, 0, 0, null);

FileOutputStream out = new FileOutputStream(outputDir + outputFileName);

//JPEGImageEncoder可适用于其他图片类型的转换

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

return "ok";

}

怎么用java代码放大或缩小图片不失真。

放大图像不会导致失真,而缩小图像将不可避免的失真。

Java中也同样是这样。

但java提供了4个缩放的微调选项。

image.SCALE_SMOOTH

//平滑优先

image.SCALE_FAST//速度优先

image.SCALE_AREA_AVERAGING

//区域均值

image.SCALE_REPLICATE

//像素复制型缩放

image.SCALE_DEFAULT

//默认缩放模式

调用方法

Image

new_img=old_img.getScaledInstance(1024,

768,

Image.SCALE_SMOOTH);

得到一张缩放后的新图。

Java 按比例缩小图片应该怎么实现?

public Image resizeImg(Image img, int newWidth, int newHeight) {

int imgW = img.getWidth();

int imgH = img.getHeight();

int[] srcPixels = new int[imgW * imgH];

int[] destPixels = new int[newWidth * newHeight];

img.getRGB(srcPixels, 0, imgW, 0, 0, imgW, imgH);

int srcX;

int srcY;

for (int y = 0; y newHeight; ++y) {

for (int x = 0; x newWidth; ++x) {

srcX = (x * imgW) / newWidth;

srcY = (y * imgH) / newHeight;

destPixels[x + y * newWidth] = srcPixels[srcX + srcY * imgW];

}

}

return Image.createRGBImage(destPixels, newWidth, newHeight, true);

}


本文名称:java图片比例缩小代码,java实现图片放大和缩小代码
当前链接:http://pwwzsj.com/article/hegecj.html