java生成缩略图的方法示例

2025-05-29 0 22

本文实例讲述了java生成缩略图的方法。分享给大家供大家参考,具体如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366
package com.util;

import java.awt.image.BufferedImage;

import java.io.File;

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 PI = (double) 3.14159265358978;

double[] contrib;

double[] normContrib;

double[] tmpContrib;

int startContrib, stopContrib;

int nDots;

int nHalfDots;

/**

* Start: Use Lanczos filter to replace the original algorithm for image

* scaling. Lanczos improves quality of the scaled image modify by :blade

*/

public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {

width = srcBufferImage.getWidth();

height = srcBufferImage.getHeight();

scaleWidth = w;

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;

// update by libra

double wt = w > width ? width : w;

double ht = h > height ? height : h;

scaleH = (double) wt / (double) width;

scaleV = (double) ht / (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 * PI) / (x * PI) * Math.sin(x * PI / Support)

/ (x * 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];

// System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-");

//

// System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-");

// System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->");

}

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;

}

/**

* End: Use Lanczos filter to replace the original algorithm for image

* scaling. Lanczos improves quality of the scaled image modify by :blade

*/

public boolean scale(String source, String target, int width, int height) {

File f = new File(source);

try {

BufferedImage bi = ImageIO.read(f);

BufferedImage out = null;

ImageScale scal = new ImageScale();

int _width = bi.getWidth();// add

int _height = bi.getHeight();// add

int[] _arr = this.getImageWidthAndHeight(_width, _height, width,

height);// add

// out = scal.imageZoomOut(bi, width, height);

out = scal.imageZoomOut(bi, _arr[0], _arr[1]);

File t = new File(target);

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

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

/**

* 得到放大或者缩小后的比例

*

* @param W

* 图片原宽

* @param H

* 原高

* @param tarW

* 转换后的宽

* @param zoom

* 放大还是缩小

* @return 返回宽和高的数组

*/

private static int[] getImageWidthAndHeight(int orgW, int orgH, int avW,

int avH) {

int width = 0;

int height = 0;

if (orgW > 0 && orgH > 0) {

if (orgW / orgH >= avW / avH) {

if (orgW > avW) {

width = avW;

height = (orgH * avW) / orgW;

} else {

width = orgW;

height = orgH;

}

System.out.println("++Widht:" + width + " Height" + height);

} else {

if (orgH > avH) {

height = avH;

width = (orgW * avH) / orgH;

} else {

width = orgW;

height = orgH;

}

System.out.println("++Widht:" + width + " Height" + height);

}

}

int[] arr = new int[2];

arr[0] = width;

arr[1] = height;

// long start = System.currentTimeMillis();

// int width = 0;

// int height = 0;

// if ((W / tarW) >= (H / tarH)) {// 宽的缩小比例大于高的

// width = tarW;

// height = H * tarW / W;

// System.out.println(width + " " + height);

// } else {

// height = tarH;

// width = W * tarH / H;

// System.out.println(width + " " + height);

// }

// int[] arr = new int[2];

// arr[0] = width;

// arr[1] = height;

// long end = System.currentTimeMillis();

// System.out.println("宽高处理:" + (end - start));

return arr;

}

public void picscale(String source, String target, int w, int h) {

File f = new File(source);

int width = 0;

int height = 0;

try {

BufferedImage bi = ImageIO.read(f);

int[] arr = getImageWidthAndHeight(bi.getWidth(), bi.getHeight(),

w, h);

width = arr[0];

height = arr[1];

BufferedImage out = null;

ImageScale scal = new ImageScale();

out = scal.imageZoomOut(bi, width, height);

File t = new File(target);

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

} catch (IOException e) {

e.printStackTrace();

}

}

/**

*

*调用scale(源文件路径,保存路径,最大宽,最大高)

*

*

*/

public static void main(String[] args) {

ImageScale is = new ImageScale();

long start = System.currentTimeMillis();

is.scale("D:/nie.jpg", "D:/t6.jpg", 250, 194);

long end = System.currentTimeMillis();

System.out.println("时间:" + (end - start));

}

}

希望本文所述对大家java程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java生成缩略图的方法示例 https://www.kuaiidc.com/117895.html

相关文章

发表评论
暂无评论