GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-util/HdCommonPictureUtil.java at master · lianyj/java-util

Expand file tree

/

HdCommonPictureUtil.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
632 lines (569 loc) · 18.2 KB
/

HdCommonPictureUtil.java

Copy path

File metadata and controls

632 lines (569 loc) · 18.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/**************************************************************************
* Copyright (c) 2006-2015 Zhejiang TaChao Network Technology Co.,Ltd.
* All rights reserved.
*
* 项目名称:浙江踏潮-汇道体育
* 版权说明:本软件属浙江踏潮网络科技有限公司所有,在未获得浙江踏潮网络科技有限公司正式授权
* 情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知
* 识产权保护的内容。
***************************************************************************/
package com.zjtachao.hd.common.util.tools;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 图片处理工具类
*
* @author <a href="mailto:[email protected]">yourname</a>
* @version $Id$
* @since 2.0
*/
public class HdCommonPictureUtil {
/** 日志 **/
private static Logger logger = LoggerFactory.getLogger(HdCommonPictureUtil.class);
/**
* 纵向合图的x坐标像素
*/
private final static int y_width = 645;
/**
* 标准图片的y坐标像素,920,是一般照片,1099是邮票照片
*/
private final static int y_height = 920;
/**
* 裁剪x坐标缩进像素
*/
private final static int x_retract = 50;
/**
* 裁剪y坐标缩进像素
*/
private final static int y_retract = 50;
/**
* 系统默认图片边框为20
*/
public final static int BORDER = 20;
/**
* 横向合成图片
*/
public static void xPic(String first, String second, String out) {
try {
/* 1 读取第一张图片 */
File fileOne = new File(first);
BufferedImage imageFirst = ImageIO.read(fileOne);
int width = imageFirst.getWidth();// 图片宽度
int height = imageFirst.getHeight();// 图片高度
int[] imageArrayFirst = new int[width * height];// 从图片中读取RGB
imageArrayFirst = imageFirst.getRGB(0, 0, width, height,
imageArrayFirst, 0, width);
/* 1 对第二张图片做相同的处理 */
File fileTwo = new File(second);
BufferedImage imageSecond = ImageIO.read(fileTwo);
int widthTwo = imageSecond.getWidth();// 图片宽度
int heightTwo = imageSecond.getHeight();// 图片高度
int[] imageArraySecond = new int[widthTwo * heightTwo];
imageArraySecond = imageSecond.getRGB(0, 0, widthTwo, heightTwo,
imageArraySecond, 0, widthTwo);
int h = height;
if (height < heightTwo) {
h = heightTwo;
}
// 生成新图片
BufferedImage imageResult = new BufferedImage(width + widthTwo, h,
BufferedImage.TYPE_INT_RGB);
imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// 设置左半部分的RGB
imageResult.setRGB(width, 0, widthTwo, heightTwo, imageArraySecond,
0, widthTwo);// 设置右半部分的RGB
File outFile = new File(out);
ImageIO.write(imageResult, "jpg", outFile);// 写图片
} catch (Exception e) {
logger.error("横向合成图片出错....", e);
}
}
/**
* 纵向合成图片
*
* @param first
* 放上面的图片路径
* @param second
* 放下面的图片路径
* @param out
* 文件输出目录
* @param border
* 图片预留边框
*/
public static boolean yPic(String first, String second, String out,
int border) {
boolean isOk = true;
try {
/* 1 读取第一张图片 */
File fileOne = new File(first);
BufferedImage imageFirst = ImageIO.read(fileOne);
int width = imageFirst.getWidth();// 图片宽度
int height = imageFirst.getHeight();// 图片高度
/* 2对第二张图片做相同的处理 */
File fileTwo = new File(second);
BufferedImage imageSecond = ImageIO.read(fileTwo);
int widthTwo = imageSecond.getWidth();// 图片宽度
int heightTwo = imageSecond.getHeight();// 图片高度
/* 1 读取第一张图片begin */
int t_height = y_height - heightTwo;
// 图片是横图,逆时针旋转90度再等比缩放
if (width > height) {
imageFirst = rotateImageLeft90(imageFirst);
}
// 等比缩放
imageFirst = resize(imageFirst, y_width, t_height);
// 缩放后图片的大小
width = imageFirst.getWidth();// 图片宽度
height = imageFirst.getHeight();// 图片高度
// 等比缩放后,图片还是太大,裁剪图片
boolean a_w, a_h = false;
if ((a_w = (width > y_width)) || (a_h = (height > t_height))) {
// 起始位置x,y坐标
int s_w = 0, s_h = 0;
// 裁剪x坐标时,缩进属性x_retract
if (a_w) {
int temp = width - y_width;
if (temp > x_retract) {
temp = x_retract;
} else {
temp = 0;
}
s_w = s_w + temp;
}
// 裁剪y坐标时,缩进属性y_retract
if (a_h) {
int temp = height - t_height;
if (temp > y_retract) {
temp = y_retract;
} else {
temp = 0;
}
s_h = s_h + temp;
}
imageFirst = crop(imageFirst, s_w, s_h, y_width, t_height);
width = imageFirst.getWidth();
height = imageFirst.getHeight();
}
int[] imageArrayFirst = new int[(width - border) * height];// 从图片中读取RGB
imageArrayFirst = imageFirst.getRGB(border, 0, (width - border),
height, imageArrayFirst, 0, (width - border));
/* 2对第二张图片做相同的处理begin */
int[] imageArraySecond = new int[widthTwo * heightTwo];
imageArraySecond = imageSecond.getRGB(0, 0, widthTwo, heightTwo,
imageArraySecond, 0, widthTwo);
int w = width;
if (width < widthTwo) {
w = widthTwo;
}
// 图片高度
int h = height + heightTwo;
// 生成新图片
BufferedImage imageResult = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
// 解决黑色背景,默认的TYPE_INT_RGB都是0,都是黑色的
Graphics2D g = (Graphics2D) imageResult.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);// 填充整个屏幕
g.dispose();
// 留边框
imageResult.setRGB(border, 0, (width - border * 2), height,
imageArrayFirst, 0, (width - border));// 设置左半部分的RGB
imageResult.setRGB(0, height, widthTwo, heightTwo,
imageArraySecond, 0, widthTwo);// 设置右半部分的RGB
File outFile = new File(out);
ImageIO.write(imageResult, "jpg", outFile);// 写图片
} catch (Exception e) {
logger.error("纵向合成图片失败....", e);
isOk = false;
}
return isOk;
}
/**
* 全图打印,图片缩放、旋转处理
*
* @param source
* 待处理的图片
* @param out
* 处理后文件输出目录
* @param border
* 图片预留边框
*/
public static boolean maigaoPic(String source, String out, int border) {
boolean isOk = true;
try {
/* 1 读取第一张图片 */
File fileOne = new File(source);
BufferedImage imageFirst = ImageIO.read(fileOne);
int width = imageFirst.getWidth();// 图片宽度
int height = imageFirst.getHeight();// 图片高度
// 图片是横图,逆时针旋转90度再等比缩放
if (width > height) {
imageFirst = rotateImageLeft90(imageFirst);
}
// 等比缩放
imageFirst = resize(imageFirst, y_width, y_height);
// 缩放后图片的大小
width = imageFirst.getWidth();// 图片宽度
height = imageFirst.getHeight();// 图片高度
// 等比缩放后,图片还是太大,裁剪图片
boolean a_w, a_h = false;
if ((a_w = (width > y_width)) || (a_h = (height > y_height))) {
// 起始位置x,y坐标
int s_w = 0, s_h = 0;
// 裁剪x坐标时,缩进属性x_retract
if (a_w) {
int temp = width - y_width;
if (temp > x_retract) {
temp = x_retract;
} else {
temp = 0;
}
s_w = s_w + temp;
}
// 裁剪y坐标时,缩进属性y_retract
if (a_h) {
int temp = height - y_height;
if (temp > y_retract) {
temp = y_retract;
} else {
temp = 0;
}
s_h = s_h + temp;
}
imageFirst = crop(imageFirst, s_w, s_h, y_width, y_height);
width = imageFirst.getWidth();
height = imageFirst.getHeight();
}
int[] imageArrayFirst = new int[(width - border) * height];// 从图片中读取RGB
imageArrayFirst = imageFirst.getRGB(border, 0, (width - border),
height, imageArrayFirst, 0, (width - border));
// 生成新图片
BufferedImage imageResult = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 解决黑色背景,默认的TYPE_INT_RGB都是0,都是黑色的
Graphics2D g = (Graphics2D) imageResult.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);// 填充整个屏幕
g.dispose();
// 留边框
imageResult.setRGB(border, 0, (width - border * 2), height,
imageArrayFirst, 0, (width - border));// 设置左半部分的RGB
File outFile = new File(out);
ImageIO.write(imageResult, "jpg", outFile);// 写图片
} catch (IOException e) {
logger.error("全图打印,图片缩放、旋转处理失败....", e);
isOk = false;
}
return isOk;
}
/**
* 实现图像的等比缩放
*
* @param source
* 待处理的图片流
* @param targetW
* 宽度
* @param targetH
* 高度
* @return
*/
private static BufferedImage resize(BufferedImage source, int targetW,
int targetH) {
//int width = source.getWidth();// 图片宽度
//int height = source.getHeight();// 图片高度
return zoomInImage(source, targetW, targetH);
// 图片宽高都太小时,强制放大图片
/*
* if (width < targetW && height < targetH) { return zoomInImage(source,
* targetW, targetH); } else if ((width < targetW && width == height) ||
* (height < targetH && width == height)) { return zoomInImage(source,
* targetW, targetH); } return null;
*/
}
/**
* 实现图像的等比缩放
*
* @param source
* 待处理的图片流
* @param targetW
* 宽度
* @param targetH
* 高度
* @return
*/
public static boolean resize(String sourcePath, int targetW,
int targetH) {
boolean flag = false;
try{
File file = new File(sourcePath);
BufferedImage sourceImg = ImageIO.read(file);
// int width = sourceImg.getWidth();
// int height = sourceImg.getHeight();
// if(width > height){
// double rate = Double.valueOf(width)/Double.valueOf(height);
// Double newH = targetH /rate;
// targetH = newH.intValue();
// }else if(height > width){
// Double newW = targetW*1000 / Math.round(height/width)/1000.0;
// targetW = newW.intValue();
// }
BufferedImage outImg = resize(sourceImg, targetW, targetH);
//outImg = rotateImage(outImg,90);
String format = sourcePath.substring(sourcePath.lastIndexOf(".")+1);
String targetPath = sourcePath.substring(0,sourcePath.lastIndexOf("."))+"_s."+format;
File outFile = new File(targetPath);
ImageIO.write(outImg, format, outFile);// 写图片
flag = true;
}catch(Exception ex){
logger.error("图片缩放失败!" , ex);
}
return flag;
}
/*
* 图片缩放,w,h为缩放的目标宽度和高度
* src为源文件目录,dest为缩放后保存目录
*/
public static boolean zoomImage(String src,int w,int h) {
boolean flag = false;
try {
double wr=0,hr=0;
File srcFile = new File(src);
String format = src.substring(src.lastIndexOf(".")+1);
String targetPath = src.substring(0,src.lastIndexOf("."))+"_s."+format;
BufferedImage bufImg = ImageIO.read(srcFile); //读取图片
Image Itemp = bufImg.getScaledInstance(w, h, Image.SCALE_SMOOTH);//设置缩放目标图片模板
wr=w*1.0/bufImg.getWidth(); //获取缩放比例
hr=h*1.0/bufImg.getHeight();
AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
Itemp = ato.filter(bufImg, null);
File outFile = new File(targetPath);
ImageIO.write((BufferedImage) Itemp,format, outFile); //写入缩减后的图片
flag = true;
} catch (Exception ex) {
logger.error("图片缩放失败!" , ex);
}
return flag;
}
/**
* 按比例裁剪图片
*
* @param source
* 待处理的图片流
* @param startX
* 开始x坐标
* @param startY
* 开始y坐标
* @param endX
* 结束x坐标
* @param endY
* 结束y坐标
* @return
*/
public static BufferedImage crop(BufferedImage source, int startX,
int startY, int endX, int endY) {
int width = source.getWidth();
int height = source.getHeight();
if (startX <= -1) {
startX = 0;
}
if (startY <= -1) {
startY = 0;
}
if (endX <= -1) {
endX = width - 1;
}
if (endY <= -1) {
endY = height - 1;
}
BufferedImage result = new BufferedImage(endX, endY, source.getType());
for (int y = startY; y < endY + startY; y++) {
for (int x = startX; x < endX + startX; x++) {
int rgb = source.getRGB(x, y);
result.setRGB(x - startX, y - startY, rgb);
}
}
return result;
}
/**
* 旋转图片为指定角度
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2
+ (w > h ? (w - h) / 2 : (h - w) / 2));
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 图片左转90度
*
* @param bufferedimage
* @return
*/
public static BufferedImage rotateImageLeft90(BufferedImage bufferedimage) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w - h) / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 图片右转90度
*
* @param bufferedimage
* @return
*/
public static BufferedImage rotateImageRight90(BufferedImage bufferedimage) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(90), w / 2 - (w - h) / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
// 对转
public File rotateImageOppo(File file) throws Exception {
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(180), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
ImageIO.write(img, "jpg", file);
return file;
}
/***
* 图片镜像处理
*
* @param file
* @param FX
* 0 为上下反转 1 为左右反转
* @return
*/
public void imageMisro(File file, int FX) {
try {
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int[][] datas = new int[w][h];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
datas[j][i] = bufferedimage.getRGB(j, i);
}
}
int[][] tmps = new int[w][h];
if (FX == 0) {
for (int i = 0, a = h - 1; i < h; i++, a--) {
for (int j = 0; j < w; j++) {
tmps[j][a] = datas[j][i];
}
}
} else if (FX == 1) {
for (int i = 0; i < h; i++) {
for (int j = 0, b = w - 1; j < w; j++, b--) {
tmps[b][i] = datas[j][i];
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
bufferedimage.setRGB(j, i, tmps[j][i]);
}
}
ImageIO.write(bufferedimage, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 对图片进行强制放大或缩小
*
* @param originalImage
* 原始图片
* @return
*/
public static BufferedImage zoomInImage(BufferedImage originalImage,
int width, int height) {
BufferedImage newImage = new BufferedImage(width, height,
originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return newImage;
}
/**
* 简易图片识别原理
*
* @param img
* 图片路径
*/
public static void discernImg(String img) {
try {
File fileOne = new File(img);
BufferedImage bi = ImageIO.read(fileOne);
// 获取图像的宽度和高度
int width = bi.getWidth();
int height = bi.getHeight();
// 扫描图片
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {// 行扫描
int dip = bi.getRGB(j, i);
if (dip == -1)
System.out.print(" ");
else
System.out.print("♦");
}
System.out.println();// 换行
}
} catch (Exception e) {
logger.error("图片识别出错", e);
}
}
public static void main(String[] args) throws IOException {
zoomImage("/Users/duhao/Desktop/aaa.gif", 300, 300);
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.