新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java截取图片缩放代码 java截取图片缩放代码是什么

java 图片缩放代码

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

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站建设、武陵源网络推广、小程序开发、武陵源网络营销、武陵源企业策划、武陵源品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供武陵源建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

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怎样等比缩放截图

Image srcImg = ImageIO.read(new FileInputStream(fnSrc) );//取源图

int width = 600; //假设要缩小到600点像素

int height = srcImg.getHeight(null)*600/srcImg.getWidth(null);//按比例,将高度缩减

System.out.println("Width: "+srcImg.getWidth(null));// 这几行是调试用

System.out.println("Height: "+srcImg.getHeight(null));

System.out.println("Width2: "+width);

System.out.println("Height2: "+height);

Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//缩小

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代码放大或缩小图片不失真。

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

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截取图片缩放代码 java截取图片缩放代码是什么
当前网址:http://www.wjwzjz.com/article/hjiidd.html
在线咨询
服务热线
服务热线:028-86922220
TOP