Commit a9331864 authored by yanzg's avatar yanzg

SQL层级处理的支持

parent 2c507875
package com.yanzuoguang.util;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 图片处理程序
*
* @author 颜佐光
*/
public class ImageHelper {
public static final float DEFAULT_SIZE = 1f;
public static final float DEFAULT_QUALITY = 1f;
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPicBySystem(String srcFilePath, String descFilePath,
float quality, float size) throws IOException {
// 设置默认值
if (size == 0) {
size = 1;
}
if (quality == 0) {
quality = 1;
}
File input = new File(srcFilePath);
BufferedImage image = ImageIO.read(input);
// 压缩图片大小
if (size < 1) {
int toWidth = (int) (image.getWidth() * size);
int toHeight = (int) (image.getHeight() * size);
BufferedImage buffImage = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
buffImage.getGraphics().drawImage(image.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH),
0, 0, null);
image = buffImage;
}
// 先指定Output,才能调用writer.write方法
File output = new File(descFilePath);
// 指定写图片的方式为 jpg
ImageWriter writer = null;
OutputStream out = null;
ImageOutputStream ios = null;
try {
writer = ImageIO.getImageWritersByFormatName("jpg").next();
out = new FileOutputStream(output);
ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param.setCompressionQuality(quality);
}
// 调用write方法,向输入流写图片
writer.write(null, new IIOImage(image, null, null), param);
} finally {
if (out != null) {
out.close();
}
if (ios != null) {
ios.close();
}
if (writer != null) {
writer.dispose();
}
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath) throws IOException {
compressPic(srcFilePath, descFilePath, DEFAULT_QUALITY, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality) throws IOException {
compressPic(srcFilePath, descFilePath, quality, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality, float size) throws IOException {
Thumbnails
// 来源目录
.of(srcFilePath)
// 按比例缩小
.scale(size)
// 质量压缩
.outputQuality(quality)
// 目标文件
.toFile(descFilePath);
}
}
...@@ -2,23 +2,15 @@ package com.yanzuoguang.util; ...@@ -2,23 +2,15 @@ package com.yanzuoguang.util;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import it.sauronsoftware.jave.*; import it.sauronsoftware.jave.*;
import net.coobird.thumbnailator.Thumbnails;
import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter; import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
/** /**
* 视频帮助类 * 视频帮助类
...@@ -27,12 +19,10 @@ import java.io.OutputStream; ...@@ -27,12 +19,10 @@ import java.io.OutputStream;
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public class MediaHelper { public class MediaHelper extends ImageHelper {
public static final int FIRST_FRAME = 5; public static final int FIRST_FRAME = 5;
public static final int DEFAULT_BIT_RATE = 372 * 1000; public static final int DEFAULT_BIT_RATE = 372 * 1000;
public static final float DEFAULT_SIZE = 1f;
public static final float DEFAULT_QUALITY = 1f;
public static final String FORMAT_EMPTY = ""; public static final String FORMAT_EMPTY = "";
public static final String FORMAT_FLV = "flv"; public static final String FORMAT_FLV = "flv";
...@@ -365,116 +355,4 @@ public class MediaHelper { ...@@ -365,116 +355,4 @@ public class MediaHelper {
return bufferedImage; return bufferedImage;
} }
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPicBySystem(String srcFilePath, String descFilePath,
float quality, float size) throws IOException {
// 设置默认值
if (size == 0) {
size = 1;
}
if (quality == 0) {
quality = 1;
}
File input = new File(srcFilePath);
BufferedImage image = ImageIO.read(input);
// 压缩图片大小
if (size < 1) {
int toWidth = (int) (image.getWidth() * size);
int toHeight = (int) (image.getHeight() * size);
BufferedImage buffImage = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
buffImage.getGraphics().drawImage(image.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH),
0, 0, null);
image = buffImage;
}
// 先指定Output,才能调用writer.write方法
File output = new File(descFilePath);
// 指定写图片的方式为 jpg
ImageWriter writer = null;
OutputStream out = null;
ImageOutputStream ios = null;
try {
writer = ImageIO.getImageWritersByFormatName("jpg").next();
out = new FileOutputStream(output);
ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param.setCompressionQuality(quality);
}
// 调用write方法,向输入流写图片
writer.write(null, new IIOImage(image, null, null), param);
} finally {
if (out != null) {
out.close();
}
if (ios != null) {
ios.close();
}
if (writer != null) {
writer.dispose();
}
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath) throws IOException {
compressPic(srcFilePath, descFilePath, DEFAULT_QUALITY, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality) throws IOException {
compressPic(srcFilePath, descFilePath, quality, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath  目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality, float size) throws IOException {
Thumbnails
// 来源目录
.of(srcFilePath)
// 按比例缩小
.scale(size)
// 质量压缩
.outputQuality(quality)
// 目标文件
.toFile(descFilePath);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment