图片缩放与转换

通过对图片重绘,达到图片缩放、压缩编码转换功能。

Java代码

import java.awt.Image;
import java.awt.image.BufferedImage;
import  java.awt.image.RenderedImage;
import java.io.File;
import  java.io.InputStream;
import java.io.OutputStream;
import  javax.imageio.ImageIO;
/**
 *
 * @author 梁栋
 * @version 1.0
 *  @since 1.0
 */
public abstract class ImageUtils {
/**
 * 缩放图片
  *
 * @param width
 *      输出宽度
 * @param height
 *       输出高度
 * @param input
 *      输入流
 * @param output
 *       输出流
 * @param format
 *      输出格式
 * @return
 *  @throws Exception
 */
public static boolean convert(int width, int height,  InputStream input,

OutputStream output, String format) throws Exception  {
// 输入
BufferedImage inputImage = ImageIO.read(input);
// 转换
RenderedImage im = (RenderedImage) convert(height, height, inputImage);
// 输 出
return ImageIO.write(im, format, output);
}

/**
 * 转换压缩算法
 *
 * @param input
 *      输入文件
 * @param output
 *       输出文件
 * @return
 * @throws Exception
 */
public static boolean  convert(File input, File output) throws Exception {
// 输入
BufferedImage  inputImage = ImageIO.read(input);

// 转换
int width = inputImage.getWidth ();
int height = inputImage.getHeight();

RenderedImage im = (RenderedImage)  convert(width, height, inputImage);
String outputFilename = output.getName ();
String format = outputFilename.substring(outputFilename

.lastIndexOf('.')  + 1);
// 输出
return ImageIO.write(im, format, output);
}

/**
 *  缩放图片
 *
 * @param width
 *      输出宽度
 * @param height
 *      输出高度
 * @param input
 *      输入文件
 * @param  output
 *      输出文件
 * @return
 * @throws Exception
  */
public static boolean convert(int width, int height, File input, File  output)

throws Exception {
// 输入
BufferedImage inputImage =  ImageIO.read(input);
// 转换
RenderedImage im = (RenderedImage) convert(width,  height, inputImage);
String outputFilename = output.getName();
String format =  outputFilename.substring(outputFilename

.lastIndexOf('.') + 1);
// 输出
return ImageIO.write(im, format, output);
}

/**
 * 缩放图片
 *
 * @param width
 *      输出宽度
 * @param height
 *      输出 高度
 * @param input
 *      输入路径
 * @param output
 *       输出路径
 * @return
 * @throws Exception
 */
public static boolean  convert(int width, int height, String inputPath,

String outputPath) throws  Exception {
return convert(width, height, new File(inputPath), new File (outputPath));
}

/**
 * 转换
 *
 * @param width
 *       输出宽度
 * @param height
 *      输出高度
 * @param input
 *       BufferedImage
 * @return BufferedImage
 * @throws Exception
  */
private static BufferedImage convert(int width, int height,

BufferedImage  input) throws Exception {
// 初始化输出图片
BufferedImage output = new  BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

// 重新绘图
Image image = input.getScaledInstance(output.getWidth(), output

.getHeight(),  output.getType());

output.createGraphics().drawImage(image, null,  null);

return output;
}

/**
 * 等比缩放图片
 *
 * @param  width
 *      输出宽度
 * @param height
 *      输出高度
 *  @param input
 *      输入流
 * @param output
 *      输出流
  * @return
 * @throws Exception
 */
public static boolean  equimultipleConvert(int width, int height,

String input, String output) throws  Exception {
return equimultipleConvert(width, height, new File(input), new File (

output));
}

/**
 * 等比缩放图片
 *
 * @param width
 *       输出宽度
 * @param height
 *      输出高度
 * @param  input
 *      输入流
 * @param output
 *      输出流
 *  @return
 *
 * @throws Exception
 */
public static boolean  equimultipleConvert(int width, int height,

File input, File output) throws  Exception {
// 输入
BufferedImage image = ImageIO.read(input);

// 重新核 算尺寸
if (image.getWidth() > 0 && image.getHeight() > 0)  {

if ((image.getWidth() / image.getHeight()) >= (width / height))  {

if (image.getWidth() > width) {

height = (image.getHeight() *  width) / image.getWidth();

} else {

width = image.getWidth ();

height = image.getHeight();

}

} else {

if  (image.getHeight() > height) {

width = (image.getWidth() * height) /  image.getHeight();

} else {

width = image.getWidth();

height =  image.getHeight();

}

}
}

// 转换 输出
return convert(width,  height, input, output);
}
}

时间: 2024-09-19 08:52:37

图片缩放与转换的相关文章

jQuery自定义图片缩放拖拽插件imageQ实现方法(附demo源码下载)_jquery

本文实例讲述了jQuery自定义图片缩放拖拽插件imageQ实现方法.分享给大家供大家参考,具体如下: 综合网上一些代码 自己写的一个图片缩放拖拽的小插件 /** * * <a href="http://lib.csdn.net/base/22" class='replace_word' title="jQuery知识库" target='_blank' style='color:#df3434; font-weight:bold;'>jQuery<

Android 图片缩放与旋转的实现详解

本文使用Matrix实现Android实现图片缩放与旋转.示例代码如下: 复制代码 代码如下: package com.android.matrix; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDra

excel数据-excel中使用kutools将图片从tif转换成jpg格式后图片序列改变了,怎么破?

问题描述 excel中使用kutools将图片从tif转换成jpg格式后图片序列改变了,怎么破? 办公时excel中有大量的图片需要从tif转成jpg格式,使用kutools可以迅速直接转换格式,但是导回excel中时图片顺序发生改变. 是否有什么编程语言或者改变转换图片格式的软件也可以,解决这个问题? 非常感谢!

【IOS-COCOS2D游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/图片缩放后模糊透明/图片不清晰【2013年12月13日补充】/动画播放出现毛边以及禁止游戏中自动锁屏问题!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/507.html 本章节主要为大家介绍在游戏开发过程中经常遇到的两个问题:  1. 解决滚屏背景或拼接地图有黑边! 对于游戏开发中,背景(游戏地图)是必要的元素之一,那么对于大部分游戏的背景都是动态,或者不断移动的:例如RPG中的背景随着人物.主角而移动,那么一般情况下背景都是由地图编辑器(图块)拼出来的,要不就

[Android] 触屏setOnTouchListener实现图片缩放、移动、绘制和添加水印

    前一篇文章讲述了Android实现图片Matrix矩阵类缩放.旋转.对比度.亮度.饱和度处理,但是真正的图片软件都是使用触屏实现图片缩放.移动.添加水印等功能,所以该篇文章主要通过setOnTouchListener监听实现该功能.希望文章对大家有所帮助. 一.图片缩放实现     首先先简单介绍Android如何实现触屏缩放图片和移动图片,新建TouchImageViw工程.设计XML中activity_main.xml布局: <RelativeLayout xmlns:android

PHP图片缩放函数:实现等比例不失真缩放

在PHP网站开发过程中,如果你建立的网站涉及大量的图片处理,必然涉及到图片上传,缩放,而如何保持图片不失真,是很多初级PHP网站开发者比较头疼的一件事,今天David就和大家分享一下如何进行图片缩放.使用之前你需要下载安装GD库,以支持PHP图片处理.下面我们结合代码讲解具体的PHP图片缩放处理的思路. 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253

python实现通过pil模块对图片格式进行转换的方法

 这篇文章主要介绍了python实现通过pil模块对图片格式进行转换的方法,涉及Python中pil模块的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了python实现通过pil模块对图片格式进行转换的方法.分享给大家供大家参考.具体分析如下: python的pil模块相当的智能,如果你需要对图片格式进行转换,比如jpg转转成tif,jpg转换成gif,png转换成jpg只需要在保存文件时指定正确的图片扩展名即可. 代码如下: ? 1 2 3 import Imag

把图片上文字转换成word文字

大 家好,我们可能常常会碰到将图片文字转换成word的难题,通常要将图片文字转换成word的图片,有些是扫描的,有些是用相机拍下来的,但不能在电脑上 编辑,如果图片文字转换成word后就可以在电脑上编辑了!图片上的文字是不能复制,这是一件令人非常郁闷的事情.很多工作上的各种需求,需要把图片文字 转换成word,可编辑的文本,一般人都会想到依着图片手打,可能一两张图片的时候还可以接受,但如果一本实物书要转换成可以编辑的word,那就不是手 打那么简单了,光靠手打根本完成不了整本书的图片文字转换成w

php实现图片缩放功能类

 Images类是一个图片处理类,实现了图片缩放类,大家参考使用吧 代码如下: <?php  /**  *  Images类是一个图片处理类  *  @package application.controllers   *  @since 1.0   */ class Images  {      /**   * 缩放图片   * @param $source原图片   * @param $newfile新图片   * @param $pre缩放比例   */  public function