java代码多线程批量处理文件内的图片去水印
1、上效果
2、上代码
package com.aaaa.yaojian.util;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/***
* Desc:
* @author lisong
*
*/
@Slf4j
public class ImageUtil {
// 开启10个线程
private static final int NUM_THREADS = 10;
public static void main(String[] args) {
File folder = new File("D:\\meituanpic\\meituanpic1\\xianfu");
File[] files = folder.listFiles();
if (files == null) {
return; // 文件夹为空或者读取失败
}
int numFiles = files.length;
int batchSize = numFiles / NUM_THREADS;
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++) {
final int start = i * batchSize;
final int end = (i == NUM_THREADS - 1) ? numFiles : (i + 1) * batchSize;
executor.submit(new FileProcessorTask(files, start, end));
}
executor.shutdown();
}
private static class FileProcessorTask implements Runnable {
private File[] files;
private int start;
private int end;
public FileProcessorTask(File[] files, int start, int end) {
this.files = files;
this.start = start;
this.end = end;
}
@Override
public void run() {
java.util.List<File> list1 = new ArrayList<>();
for (int i = start; i < end; i++) {
// File file = files[i];
getmain(files[i].getPath(),files[i].getName());
// 分批处理文件的逻辑
// list1.add(files[i]);
// if(list1.size() == 100){
// for(File file1 : list1){
// getmain(file1.getPath(),file1.getName());
// }
// list1.clear();
// }
}
// if(list1.size()>1){
// for(File file1 : list1){
// getmain(file1.getPath(),file1.getName());
// }
// list1.clear();
// }
}
}
public static void yidong( File sourceFile,String destinationDirPath){
File destinationDirectory = new File(destinationDirPath);
File destinationFile = new File(destinationDirectory, sourceFile.getName());
sourceFile.renameTo(destinationFile);
}
public static void getmain(String imgUrl,String imgName){
try{
File file = new File(imgUrl);
BufferedImage bi = ImageIO.read(file);
if(bi!=null){
int pointNum = 5;
double[][] xy = new double[2][5 * pointNum];
double[][] rgb = new double[3][5 * pointNum];
int widthBorder = 5;
double w = bi.getWidth()*0.35;
int width = (int)w;
// double widthStep = (double) (width - widthBorder * 2) / (pointNum - 1);
double widthStep = (double) (width - widthBorder * 2) / (pointNum - 1);
int heightBorder = 5;
double h = bi.getHeight()*0.20;
int height = (int)h;
// double heightStep = (double) (height - heightBorder * 2) / (pointNum - 1);
double heightStep = (double) (height - heightBorder * 2) / (pointNum - 1);
for (int i = 0; i < pointNum; i++) {
xy[0][i] = widthBorder;
xy[1][i] = (int) (heightBorder + heightStep * i);
Color oriColor = new Color(bi.getRGB((int) xy[0][i], (int) xy[1][i]));
rgb[0][i] = oriColor.getRed();
rgb[1][i] = oriColor.getGreen();
rgb[2][i] = oriColor.getBlue();
xy[0][pointNum + i] = bi.getWidth() - widthBorder;
xy[1][pointNum + i] = (int) (heightBorder + heightStep * i);
oriColor = new Color(bi.getRGB((int) xy[0][pointNum + i], (int) xy[1][pointNum + i]));
rgb[0][pointNum + i] = oriColor.getRed();
rgb[1][pointNum + i] = oriColor.getGreen();
rgb[2][pointNum + i] = oriColor.getBlue();
}
for (int i = 0; i < pointNum; i++) {
xy[0][2 * pointNum + i] = (int) (widthBorder + widthStep * i);
xy[1][2 * pointNum + i] = heightBorder;
Color oriColor = new Color(bi.getRGB((int) xy[0][2 * pointNum + i], (int) xy[1][2 * pointNum + i]));
rgb[0][2 * pointNum + i] = oriColor.getRed();
rgb[1][2 * pointNum + i] = oriColor.getGreen();
rgb[2][2 * pointNum + i] = oriColor.getBlue();
xy[0][3 * pointNum + i] = (int) (widthBorder + widthStep * i);
xy[1][3 * pointNum + i] = height - heightBorder;
oriColor = new Color(bi.getRGB((int) xy[0][3 * pointNum + i], (int) xy[1][3 * pointNum + i]));
rgb[0][3 * pointNum + i] = oriColor.getRed();
rgb[1][3 * pointNum + i] = oriColor.getGreen();
rgb[2][3 * pointNum + i] = oriColor.getBlue();
}
double[] ar = LinearRegressionTest.getRegressionCoefficient(xy, rgb[0]);
double[] ag = LinearRegressionTest.getRegressionCoefficient(xy, rgb[1]);
double[] ab = LinearRegressionTest.getRegressionCoefficient(xy, rgb[2]);
int colorDiffInit = 27;
for (int x = bi.getHeight() - height; x < bi.getHeight(); x++) {
for (int y = bi.getWidth() - width; y < bi.getWidth(); y++) {
int redStd = (int) (y * ar[0] + x * ar[1] + ar[2]);
redStd = redStd <= 255 ? redStd : 255;
int greedStd = (int) (y * ag[0] + x * ag[1] + ag[2]);
greedStd = greedStd <= 255 ? greedStd : 255;
int blueStd = (int) (y * ab[0] + x * ab[1] + ab[2]);
blueStd = blueStd <= 255 ? blueStd : 255;
Color oriColor = new Color(bi.getRGB(y, x));
int red = oriColor.getRed();
int greed = oriColor.getGreen();
int blue = oriColor.getBlue();
int colorDiffRed = redStd > red ? redStd - red : red - redStd;
int colorDiffGreed = greedStd > greed ? greedStd - greed : greed - greedStd;
int colorDiffBlue = blueStd > blue ? blueStd - blue : blue - blueStd;
if (colorDiffRed < colorDiffInit && colorDiffGreed < colorDiffInit && colorDiffBlue < colorDiffInit) {
Color wColor;
try {
wColor = new Color(redStd, greedStd, blueStd);
} catch (Exception ex) {
continue;
}
bi.setRGB(y, x, wColor.getRGB());
}
}
}
// 去完水印的图片保存
File output = new File("D:\\meituanpic\\meituanpic1\\去水印\\xianfu6\\"+imgName);
if (output.getName().toLowerCase().endsWith(".jpg")){
ImageIO.write(bi, "jpg", output);
} else if(output.getName().toLowerCase().endsWith(".png")){
ImageIO.write(bi, "png", output);
} else if(output.getName().toLowerCase().endsWith(".webp")){
ImageIO.write(bi, "webp", output);
}else {
ImageIO.write(bi, "jpeg", output);
}
// 原图移动到另一文件夹 防止找不到进行到哪个位置
yidong(file,"D:\\meituanpic\\meituanpic1\\完成\\xianfu6");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.aaa.yaojian.util;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LinearRegressionTest {
/**
* 多元线性回归分析
*
* @param x 每一列存放m个自变量的观察值
* @param y 存放随即变量y的n个观察值
* @param m 自变量的个数
* @param n 观察数据的组数
* @param a 返回回归系数a0,...,am
* @param dt dt[0]偏差平方和q,dt[1]平均标准偏差s,dt[2]复相关系数r,dt[3]回归平方和u
* @param v 返回m个自变量的偏相关系数
*/
public static void sqt2(double[][] x, double[] y, int m, int n, double[] a,
double[] dt, double[] v) {
int i, j, k, mm;
double q, e, u, p, yy, s, r, pp;
double[] b = new double[(m + 1) * (m + 1)];
mm = m + 1;
b[mm * mm - 1] = n;
for (j = 0; j <= m - 1; j++) {
p = 0.0;
for (i = 0; i <= n - 1; i++){
p = p + x[j][i];
}
b[m * mm + j] = p;
b[j * mm + m] = p;
}
for (i = 0; i <= m - 1; i++){
for (j = i; j <= m - 1; j++) {
p = 0.0;
for (k = 0; k <= n - 1; k++){
p = p + x[i][k] * x[j][k];
b[j * mm + i] = p;
b[i * mm + j] = p;
}
}
}
a[m] = 0.0;
for (i = 0; i <= n - 1; i++)
{
a[m] = a[m] + y[i];
}
for (i = 0; i <= m - 1; i++) {
a[i] = 0.0;
for (j = 0; j <= n - 1; j++){
a[i] = a[i] + x[i][j] * y[j];
}
}
chlk(b, mm, 1, a);
yy = 0.0;
for (i = 0; i <= n - 1; i++){
yy = yy + y[i] / n;
}
q = 0.0;
e = 0.0;
u = 0.0;
for (i = 0; i <= n - 1; i++) {
p = a[m];
for (j = 0; j <= m - 1; j++){
p = p + a[j] * x[j][i];
}
q = q + (y[i] - p) * (y[i] - p);
e = e + (y[i] - yy) * (y[i] - yy);
u = u + (yy - p) * (yy - p);
}
s = Math.sqrt(q / n);
r = Math.sqrt(1.0 - q / e);
for (j = 0; j <= m - 1; j++) {
p = 0.0;
for (i = 0; i <= n - 1; i++) {
pp = a[m];
for (k = 0; k <= m - 1; k++){
if (k != j){
pp = pp + a[k] * x[k][i];
}
}
p = p + (y[i] - pp) * (y[i] - pp);
}
v[j] = Math.sqrt(1.0 - q / p);
}
dt[0] = q;
dt[1] = s;
dt[2] = r;
dt[3] = u;
}
private static int chlk(double[] a, int n, int m, double[] d) {
int i, j, k, u, v;
if ((a[0] + 1.0 == 1.0) || (a[0] < 0.0)) {
return (-2);
}
a[0] = Math.sqrt(a[0]);
for (j = 1; j <= n - 1; j++){
a[j] = a[j] / a[0];
}
for (i = 1; i <= n - 1; i++) {
u = i * n + i;
for (j = 1; j <= i; j++) {
v = (j - 1) * n + i;
a[u] = a[u] - a[v] * a[v];
}
if ((a[u] + 1.0 == 1.0) || (a[u] < 0.0)) {
return (-2);
}
a[u] = Math.sqrt(a[u]);
if (i != (n - 1)) {
for (j = i + 1; j <= n - 1; j++) {
v = i * n + j;
for (k = 1; k <= i; k++){
a[v] = a[v] - a[(k - 1) * n + i] * a[(k - 1) * n + j];
}
a[v] = a[v] / a[u];
}
}
}
for (j = 0; j <= m - 1; j++) {
d[j] = d[j] / a[0];
for (i = 1; i <= n - 1; i++) {
u = i * n + i;
v = i * m + j;
for (k = 1; k <= i; k++){
d[v] = d[v] - a[(k - 1) * n + i] * d[(k - 1) * m + j];
}
d[v] = d[v] / a[u];
}
}
for (j = 0; j <= m - 1; j++) {
u = (n - 1) * m + j;
d[u] = d[u] / a[n * n - 1];
for (k = n - 1; k >= 1; k--) {
u = (k - 1) * m + j;
for (i = k; i <= n - 1; i++) {
v = (k - 1) * n + i;
d[u] = d[u] - a[v] * d[i * m + j];
}
v = (k - 1) * n + k - 1;
d[u] = d[u] / a[v];
}
}
return (2);
}
public static double[] getRegressionCoefficient(double[][] x, double[] y) {
int m = x.length; // 自变量的个数
int n = x[0].length; // 观察数据的组数
double[] a = new double[m + 1];
double[] v = new double[m];
double[] dt = new double[4];
sqt2(x, y, m, n, a, dt, v);
return a;
}
}
package com.wangyaow.yaojian.util;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LinearRegressionTest {
/**
* 多元线性回归分析
*
* @param x 每一列存放m个自变量的观察值
* @param y 存放随即变量y的n个观察值
* @param m 自变量的个数
* @param n 观察数据的组数
* @param a 返回回归系数a0,...,am
* @param dt dt[0]偏差平方和q,dt[1]平均标准偏差s,dt[2]复相关系数r,dt[3]回归平方和u
* @param v 返回m个自变量的偏相关系数
*/
public static void sqt2(double[][] x, double[] y, int m, int n, double[] a,
double[] dt, double[] v) {
int i, j, k, mm;
double q, e, u, p, yy, s, r, pp;
double[] b = new double[(m + 1) * (m + 1)];
mm = m + 1;
b[mm * mm - 1] = n;
for (j = 0; j <= m - 1; j++) {
p = 0.0;
for (i = 0; i <= n - 1; i++){
p = p + x[j][i];
}
b[m * mm + j] = p;
b[j * mm + m] = p;
}
for (i = 0; i <= m - 1; i++){
for (j = i; j <= m - 1; j++) {
p = 0.0;
for (k = 0; k <= n - 1; k++){
p = p + x[i][k] * x[j][k];
b[j * mm + i] = p;
b[i * mm + j] = p;
}
}
}
a[m] = 0.0;
for (i = 0; i <= n - 1; i++)
{
a[m] = a[m] + y[i];
}
for (i = 0; i <= m - 1; i++) {
a[i] = 0.0;
for (j = 0; j <= n - 1; j++){
a[i] = a[i] + x[i][j] * y[j];
}
}
chlk(b, mm, 1, a);
yy = 0.0;
for (i = 0; i <= n - 1; i++){
yy = yy + y[i] / n;
}
q = 0.0;
e = 0.0;
u = 0.0;
for (i = 0; i <= n - 1; i++) {
p = a[m];
for (j = 0; j <= m - 1; j++){
p = p + a[j] * x[j][i];
}
q = q + (y[i] - p) * (y[i] - p);
e = e + (y[i] - yy) * (y[i] - yy);
u = u + (yy - p) * (yy - p);
}
s = Math.sqrt(q / n);
r = Math.sqrt(1.0 - q / e);
for (j = 0; j <= m - 1; j++) {
p = 0.0;
for (i = 0; i <= n - 1; i++) {
pp = a[m];
for (k = 0; k <= m - 1; k++){
if (k != j){
pp = pp + a[k] * x[k][i];
}
}
p = p + (y[i] - pp) * (y[i] - pp);
}
v[j] = Math.sqrt(1.0 - q / p);
}
dt[0] = q;
dt[1] = s;
dt[2] = r;
dt[3] = u;
}
private static int chlk(double[] a, int n, int m, double[] d) {
int i, j, k, u, v;
if ((a[0] + 1.0 == 1.0) || (a[0] < 0.0)) {
return (-2);
}
a[0] = Math.sqrt(a[0]);
for (j = 1; j <= n - 1; j++){
a[j] = a[j] / a[0];
}
for (i = 1; i <= n - 1; i++) {
u = i * n + i;
for (j = 1; j <= i; j++) {
v = (j - 1) * n + i;
a[u] = a[u] - a[v] * a[v];
}
if ((a[u] + 1.0 == 1.0) || (a[u] < 0.0)) {
// logger.warn("Fail!");
return (-2);
}
a[u] = Math.sqrt(a[u]);
if (i != (n - 1)) {
for (j = i + 1; j <= n - 1; j++) {
v = i * n + j;
for (k = 1; k <= i; k++){
a[v] = a[v] - a[(k - 1) * n + i] * a[(k - 1) * n + j];
}
a[v] = a[v] / a[u];
}
}
}
for (j = 0; j <= m - 1; j++) {
d[j] = d[j] / a[0];
for (i = 1; i <= n - 1; i++) {
u = i * n + i;
v = i * m + j;
for (k = 1; k <= i; k++){
d[v] = d[v] - a[(k - 1) * n + i] * d[(k - 1) * m + j];
}
d[v] = d[v] / a[u];
}
}
for (j = 0; j <= m - 1; j++) {
u = (n - 1) * m + j;
d[u] = d[u] / a[n * n - 1];
for (k = n - 1; k >= 1; k--) {
u = (k - 1) * m + j;
for (i = k; i <= n - 1; i++) {
v = (k - 1) * n + i;
d[u] = d[u] - a[v] * d[i * m + j];
}
v = (k - 1) * n + k - 1;
d[u] = d[u] / a[v];
}
}
return (2);
}
public static double[] getRegressionCoefficient(double[][] x, double[] y) {
int m = x.length; // 自变量的个数
int n = x[0].length; // 观察数据的组数
double[] a = new double[m + 1];
double[] v = new double[m];
double[] dt = new double[4];
sqt2(x, y, m, n, a, dt, v);
return a;
}
}