JAVA图像缩放处理
————————————————————————————————————————————————
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import javax.imageio.ImageIO; public class ImageScale { private int width;
private int height;
private int scaleWidth;
double support = (double) 3.0;
double[] contrib;
double[] normContrib;
double[] tmpContrib;
int startContrib, stopContrib;
int nDots;
int nHalfDots; public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h, boolean lockScale) {
width = srcBufferImage.getWidth();
height = srcBufferImage.getHeight();
scaleWidth = w;
if (lockScale) {
h = w * height / width;
} if (DetermineResultSize(w, h) == 1) {
return srcBufferImage;
}
CalContrib();
BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
return pbFinalOut;
} /**
* 决定图像尺寸
*/
private int DetermineResultSize(int w, int h) {
double scaleH, scaleV;
scaleH = (double) w / (double) width;
scaleV = (double) h / (double) height;
// 需要判断一下scaleH,scaleV,不做放大操作
if (scaleH >= 1.0 && scaleV >= 1.0) {
return 1;
}
return 0; } // end of DetermineResultSize() private double Lanczos(int i, int inWidth, int outWidth, double Support) {
double x; x = (double) i * (double) outWidth / (double) inWidth; return Math.sin(x * Math.PI) / (x * Math.PI) * Math.sin(x * Math.PI / Support) / (x * Math.PI / Support); } // end of Lanczos() //
// Assumption: same horizontal and vertical scaling factor
//
private void CalContrib() {
nHalfDots = (int) ((double) width * support / (double) scaleWidth);
nDots = nHalfDots * 2 + 1;
try {
contrib = new double[nDots];
normContrib = new double[nDots];
tmpContrib = new double[nDots];
} catch (Exception e) {
System.out.println("init contrib,normContrib,tmpContrib" + e);
} int center = nHalfDots;
contrib[center] = 1.0; double weight = 0.0;
int i = 0;
for (i = 1; i <= center; i++) {
contrib[center + i] = Lanczos(i, width, scaleWidth, support);
weight += contrib[center + i];
} for (i = center - 1; i >= 0; i--) {
contrib[i] = contrib[center * 2 - i];
} weight = weight * 2 + 1.0; for (i = 0; i <= center; i++) {
normContrib[i] = contrib[i] / weight;
} for (i = center + 1; i < nDots; i++) {
normContrib[i] = normContrib[center * 2 - i];
}
} // end of CalContrib() // 处理边缘
private void CalTempContrib(int start, int stop) {
double weight = 0; int i = 0;
for (i = start; i <= stop; i++) {
weight += contrib[i];
} for (i = start; i <= stop; i++) {
tmpContrib[i] = contrib[i] / weight;
} } // end of CalTempContrib() private int GetRedValue(int rgbValue) {
int temp = rgbValue & 0x00ff0000;
return temp >> 16;
} private int GetGreenValue(int rgbValue) {
int temp = rgbValue & 0x0000ff00;
return temp >> 8;
} private int GetBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
} private int ComRGB(int redValue, int greenValue, int blueValue) { return (redValue << 16) + (greenValue << 8) + blueValue;
} // 行水平滤波
private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX, int start, int stop, int y,
double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j; for (i = startX, j = start; i <= stopX; i++, j++) {
valueRGB = bufImg.getRGB(i, y); valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
} valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen), Clip((int) valueBlue));
return valueRGB; } // end of HorizontalFilter() // 图片水平滤波
private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {
int dwInW = bufImage.getWidth();
int dwInH = bufImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iOutW, dwInH, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < iOutW; x++) { int startX;
int start;
int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);
int y = 0; startX = X - nHalfDots;
if (startX < 0) {
startX = 0;
start = nHalfDots - X;
} else {
start = 0;
} int stop;
int stopX = X + nHalfDots;
if (stopX > (dwInW - 1)) {
stopX = dwInW - 1;
stop = nHalfDots + (dwInW - 1 - X);
} else {
stop = nHalfDots * 2;
} if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start, stop, y, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start, stop, y, normContrib);
pbOut.setRGB(x, y, value);
}
}
} return pbOut; } // end of HorizontalFiltering() private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY, int start, int stop, int x,
double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j; for (i = startY, j = start; i <= stopY; i++, j++) {
valueRGB = pbInImage.getRGB(x, i); valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
} valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen), Clip((int) valueBlue));
// System.out.println(valueRGB);
return valueRGB; } // end of VerticalFilter() private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {
int iW = pbImage.getWidth();
int iH = pbImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iW, iOutH, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < iOutH; y++) { int startY;
int start;
int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5); startY = Y - nHalfDots;
if (startY < 0) {
startY = 0;
start = nHalfDots - Y;
} else {
start = 0;
} int stop;
int stopY = Y + nHalfDots;
if (stopY > (int) (iH - 1)) {
stopY = iH - 1;
stop = nHalfDots + (iH - 1 - Y);
} else {
stop = nHalfDots * 2;
} if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop, x, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop, x, normContrib);
pbOut.setRGB(x, y, value);
}
} } return pbOut; } // end of VerticalFiltering() int Clip(int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
} public static void main(String[] args) throws IOException {
ImageScale is = new ImageScale();
String path = "D:\\My Documents\\My Pictures\\pictrue\\";
BufferedImage image1 = ImageIO.read(new File(path + "test.jpg"));
int w = 200, h = 400;
BufferedImage image2 = is.imageZoomOut(image1, w, h, true);
FileOutputStream out = new FileOutputStream(path + "test_2.jpg");
ImageIO.write(image2, "jpeg", out);
}
}
程序运行的效果如下:
test.jpg(原图):
test_2.jpg(程序生成的图片):
JAVA图像缩放处理的更多相关文章
- 图像缩放_OpenCv
图像缩放是一种比较简单的图像处理操作,这里给出opencv中的代码, opencv的版本C语言接口 int resize_c() { const char *pstrImageName = " ...
- opencv2 矩阵方式 resize图像缩放代码(转载)
http://blog.sina.com.cn/s/blog_74a459380101r0yx.html opencv2 矩阵方式 resize图像缩放代码(转载) (2014-05-16 09:55 ...
- 【美工设计 - Adobe Illustrator】基本设置 (图像显示 | 图像缩放 | 置入导出 | 标尺 | 网格 | 参考线 | 画板)
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50232767 一. 基础操作 1. 设置图像显示效果 (1) ...
- 图像旋转与图像缩放及Matlab代码实现
本周的作业是自己通过公式编写图像旋转与缩放的代码.今天先通过调用函数的方法来实现. 图像的旋转: A=imread('2.jpg'); J=imrotate(A, 30); subplot(1,2,1 ...
- opencv3 图像处理(一)图像缩放( python与c++ 实现)
opencv3 图像处理 之 图像缩放( python与c++实现 ) 一. 主要函数介绍 1) 图像大小变换 Resize () 原型: void Resize(const CvArr* src,C ...
- 实现基于最近邻内插和双线性内插的图像缩放C++实现
平时我们写图像处理的代码时,如果需要缩放图片,我们都是直接调用图像库的resize函数来完成图像的缩放.作为一个机器视觉或者图像处理算法的工作者,图像缩放代码的实现应该是必须掌握的.在众多图像缩放算法 ...
- 20 个具有惊艳效果的 jQuery 图像缩放插件
jQuery相对与Flash的魔力已经贯穿整个网络.尽管,Flash层被认为是用于网页设计的首选,然而随着jQuery的出现,以及他的酷似Flash的交互式特效使得网页更加的优雅——Flash开始靠边 ...
- opencv学习笔记——图像缩放函数resize
opencv提供了一种图像缩放函数 功能:实现对输入图像缩放到指定大小 函数原型: void cv::resize ( InputArray src, OutputArray dst, Size ds ...
- 邻近双线性插值图像缩放的Python实现
最近在查找有关图像缩放之类的算法,因工作中需要用到诸如此类的图像处理算法就在网上了解了一下相关算法,以及其原理,并用Python实现,且亲自验证过,在次与大家分享. 声明:本文代码示例针对的是plan ...
随机推荐
- Notepad++的使用
\t 制表符. \n 新行. . 匹配任意字符. | 匹配表达式左边和右边的字符. 例如, "ab|bc" 匹配 "ab" 或者 "bc&quo ...
- javascript闭包传参就这么简单
var query = (function (a) { return a; })('fx'); alert(query);
- JavaScript中让元素动态发射指定的事件
var ev = document.createEvent('HTMLEvents'); //动态创建HTML事件 ev.initEvent('abort', false, true); //HTML ...
- 安卓camera拍照时序
转自:http://blog.csdn.net/tankai19880619/article/details/17147125 一.看看调用时序图 1.拍照命令时序图 2.拍照数据回调时序图 二.看看 ...
- 应用zip压缩的javascript以及Egret H5游戏实战
代码地址如下:http://www.demodashi.com/demo/11039.html 主要起因是策划对最快进入登录界面有硬性要求(3秒),那么最开始加载的文件越小越好.对H5的游戏程序进行压 ...
- 表格行与行的间距设置,通过margin无效,要这么设置
CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值 描述 separate 默认值.边框会被分开.不会忽略 border-spacing 和 empty-cell ...
- fs 小计
如果是export 就可以放到b-leg上 如果是set就可以对于a-leg
- 저장소system.runtime.remoting.messaging.callcontext
https://msdn.microsoft.com/ko-kr/library/system.runtime.remoting.messaging.callcontext(v=vs.110).asp ...
- 手动安装minGW
minGW是C语言编译包,将GCC编译器在Windows平台上编译软件提供支持. 手工安装minGW是一件很繁琐的事情,但是搞懂它很有用,因为C语言本身是一个很小的语法系统,全靠 各种库在支持,安装m ...
- Real-Time SQL Monitoring using DBMS_SQLTUNE
Real-Time SQL Monitoring reports are available from three locations: Enterprise Manager - Click the ...