1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 压缩图片大小的java代码_java按比例压缩图片的源代码 用java如何把图片处理到指定大小...

压缩图片大小的java代码_java按比例压缩图片的源代码 用java如何把图片处理到指定大小...

时间:2018-12-18 23:44:22

相关推荐

压缩图片大小的java代码_java按比例压缩图片的源代码 用java如何把图片处理到指定大小...

[要分析某个网页中的代码构成,需要某个结点下的内容。用此原始方法可以得到整个网页的源码。其实更简单的方法是使用 WebClient 或 HtmlUtil 等开源方式 。public class Ht

用java如何把图片处理到指定大小

前言:

朋友在做图片切割的时候遇到一个问题:如何用java如何把图片处理到指定大小?

切割程序如下:

public void cut(String srcImageFile,FileOutputStream fileout, int w, int h, int x1,

int y1, int sw, int sh) {

// TODO Auto-generated method stub

try {

Image img;

ImageFilter cropFilter;

// 读取源图像

BufferedImage bi = ImageIO.read(new File(srcImageFile));

if (sw >= w && sh >= h) {

Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);

// 剪切起始坐标点

int x = x1;

int y = y1;

int destWidth = w; // 切片宽度

int destHeight = h; // 切片高度[在前面几篇文章中,我们详细介绍了Android系统进程间通信机制Binder的原理,并且深入分析了系统提供的Binder运行库和驱动程序的源代码。细心的读者会发现,这几篇文章分析

// 图片比例

double pw = sw;

double ph = sh;

double m = (double) sw / pw;

double n = (double) sh / ph;

System.out.println(m);

int wth = (int) (destWidth * m);

int hth = (int) (destHeight * n);

int xx = (int) (x * m);

int yy = (int) (y * n);

// 四个参数分别为图像起点坐标和宽高

// 即: CropImageFilter(int x,int y,int width,int height)

cropFilter = new CropImageFilter(xx, yy, wth, hth);

img = Toolkit.getDefaultToolkit().createImage(

new FilteredImageSource(image.getSource(), cropFilter));

BufferedImage tag = new BufferedImage(w, h,

BufferedImage.TYPE_INT_RGB);

Graphics g = tag.getGraphics();

g.drawImage(img, 0, 0, null); // 绘制缩小后的图

g.dispose();

// 输出为文件

ImageIO.write(tag, "JPEG", fileout);

}

} catch (Exception e) {

e.printStackTrace();

}

}

进入正题:

解决方案参考源码,java按比例压缩图片的源代码如下:

public static void uploadImage(File p_in,File p_out,int height,int width,String ftype) throws FileNotFoundException,IOException

{

// 取得图片处理

// ConvertImageFactory l_factory = ConvertImageFactory.getInstance();

// AbstractProduct l_product = l_factory.createAbstractProduct(p_SourceFile.getContentType());

// boolean l_result = l_product.convertImageSize(p_SourceFile

// .getInputStream(), p_path );

InputStream l_in = new FileInputStream(p_in);

OutputStream l_out = new FileOutputStream(p_out);

chgPic(l_in,l_out,width,height,ftype);

}

//按比例压缩图片

public static boolean chgPic(InputStream in, OutputStream out,int newWidth, int newHeight,String ftype) {

BufferedImage img = null;

FileInputStream newin=null;

File tempfile=null;

try {

if(pareToIgnoreCase("bmp")==0){

PNGDecodeParam decodeParam = new PNGDecodeParam();

String l_tempfile = Tool.createNewFileName("jpg");

tempfile = new File(l_tempfile);

JPEGEncodeParam encodeParam = new JPEGEncodeParam();

//根据路径打开输出流

FileOutputStream tempout;

tempout = new FileOutputStream(tempfile);

ImageDecoder decoder = ImageCodec.createImageDecoder("BMP",in,decodeParam);

RenderedImage image = decoder.decodeAsRenderedImage();

ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG",tempout,encodeParam);

encoder.encode(image);

tempout.close();

newin = new FileInputStream(tempfile);

img = ImageIO.read(newin);

}else{

img = ImageIO.read(in);

}

int width = img.getWidth(null);

int height = img.getHeight(null);

if (newWidth >= width) {

if (newHeight < height) {

width = (int) (width * newHeight / height);

height = newHeight;

}

} else {

if (newHeight >= height) {

height = (int) (height * newWidth / width);

width = newWidth;

} else {

if (height > width) {

width = (int) (width * newHeight / height);

height = newHeight;

} else {

height = (int) (height * newWidth / width);

width = newWidth;

}

}

}

BufferedImage img2 = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

img2.getGraphics().drawImage(img, 0, 0, width, height, null);

if (pareToIgnoreCase("jpg") == 0 || pareToIgnoreCase("jpeg") == 0 ) {

ImageIO.write(img2, "jpg", out);

} else

ImageIO.write(img2, "png", out);

if( pareToIgnoreCase("bmp") == 0){

ImageIO.write(img2, "jpg", out);

newin.close();

tempfile.delete();

}

return true;

} catch (Exception ex) {

ex.printStackTrace();

return false;

}finally{

try{

in.close();

out.close();

} catch (IOException e) {

}

}

}[面向对象模型 面向对象模型是利用UML(统一建模语言)的图形来描述系统结构的模型,它从不同角度实现系统的工作状态。这些图形有助于用户,管理人员,系统分析人员,开发人

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。