LAPTOP-SB56SG4Q\86185
3 years ago
8 changed files with 178 additions and 1 deletions
Binary file not shown.
@ -1,3 +1,6 @@ |
|||||||
# open-JSD-8472 |
# open-JSD-8472 |
||||||
|
|
||||||
JSD-8472 图片压缩函数 |
JSD-8472 图片压缩函数\ |
||||||
|
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||||
|
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||||
|
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,18 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||||
|
<id>com.fr.plugin.third.party.jsdiehc</id> |
||||||
|
<name><![CDATA[图片压缩函数]]></name> |
||||||
|
<active>yes</active> |
||||||
|
<version>0.2</version> |
||||||
|
<env-version>10.0</env-version> |
||||||
|
<jartime>2019-01-01</jartime> |
||||||
|
<vendor>fr.open</vendor> |
||||||
|
<description><![CDATA[ |
||||||
|
图片压缩函数 |
||||||
|
]]></description> |
||||||
|
<change-notes><![CDATA[ |
||||||
|
]]></change-notes> |
||||||
|
<extra-core> |
||||||
|
<FunctionDefineProvider class="com.fr.plugin.third.party.jsdiehc.function.ImageCompressionScaleFunction" name="IMAGE_COMPRESSION_SCALE" description="图片压缩函数。IMAGE_COMPRESSION_SCALE(image, [quality]),image:图片对象;quality:压缩质量率,值为0至1,1或0为不压缩"/> |
||||||
|
</extra-core> |
||||||
|
<function-recorder class="com.fr.plugin.third.party.jsdiehc.function.ImageCompressionScaleFunction"/> |
||||||
|
</plugin> |
@ -0,0 +1,43 @@ |
|||||||
|
package com.fr.plugin.third.party.jsdiehc.function; |
||||||
|
|
||||||
|
import com.fr.cache.Attachment; |
||||||
|
import com.fr.cache.AttachmentFileBase; |
||||||
|
import com.fr.cache.type.AttachmentScope; |
||||||
|
|
||||||
|
public class ImageAttachment extends Attachment { |
||||||
|
public ImageAttachment() |
||||||
|
{ |
||||||
|
super("", "", "", null); |
||||||
|
} |
||||||
|
|
||||||
|
private byte[] imageBytes; |
||||||
|
|
||||||
|
public ImageAttachment(String s, String s1, String s2, AttachmentFileBase attachmentFileBase, int i, int i1, AttachmentScope attachmentScope) { |
||||||
|
super(s, s1, s2, attachmentFileBase, i, i1, attachmentScope); |
||||||
|
} |
||||||
|
|
||||||
|
public ImageAttachment(String s, String s1, String s2, AttachmentFileBase attachmentFileBase, int i, int i1) { |
||||||
|
super(s, s1, s2, attachmentFileBase, i, i1); |
||||||
|
} |
||||||
|
|
||||||
|
public ImageAttachment(String s, String s1, String s2, AttachmentFileBase attachmentFileBase, AttachmentScope attachmentScope) { |
||||||
|
super(s, s1, s2, attachmentFileBase, attachmentScope); |
||||||
|
} |
||||||
|
|
||||||
|
public ImageAttachment(String s, String s1, String s2, AttachmentFileBase attachmentFileBase) { |
||||||
|
super(s, s1, s2, attachmentFileBase); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public byte[] getBytes() { |
||||||
|
return this.imageBytes; |
||||||
|
} |
||||||
|
|
||||||
|
public byte[] getImageBytes() { |
||||||
|
return imageBytes; |
||||||
|
} |
||||||
|
|
||||||
|
public void setImageBytes(byte[] bytes) { |
||||||
|
this.imageBytes = bytes; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,113 @@ |
|||||||
|
package com.fr.plugin.third.party.jsdiehc.function; |
||||||
|
|
||||||
|
import com.fanruan.api.log.LogKit; |
||||||
|
import com.fanruan.api.util.StringKit; |
||||||
|
import com.fr.cache.Attachment; |
||||||
|
import com.fr.general.FArray; |
||||||
|
import com.fr.intelli.record.Focus; |
||||||
|
import com.fr.intelli.record.Original; |
||||||
|
import com.fr.plugin.context.PluginContexts; |
||||||
|
import com.fr.record.analyzer.EnableMetrics; |
||||||
|
import com.fr.script.AbstractFunction; |
||||||
|
import com.fr.stable.Primitive; |
||||||
|
import com.fr.stable.fun.Authorize; |
||||||
|
import net.coobird.thumbnailator.Thumbnails; |
||||||
|
|
||||||
|
import java.awt.image.BufferedImage; |
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 图片压缩缩放函数<br/> |
||||||
|
* IMAGE_COMPRESSION_SCALE(image, [quality], [scale])<br/> |
||||||
|
* 参数:<br/> |
||||||
|
* image:图片对象<br/> |
||||||
|
* quality:压缩质量<br/> |
||||||
|
* scale:缩放比例 |
||||||
|
*/ |
||||||
|
@Authorize(callSignKey = "com.fr.plugin.third.party.jsdiehc") |
||||||
|
@EnableMetrics |
||||||
|
public class ImageCompressionScaleFunction extends AbstractFunction { |
||||||
|
|
||||||
|
@Focus(id = "com.fr.plugin.third.party.jsdiehc", text = "plugin-jsdiehc", source = Original.PLUGIN) |
||||||
|
public Object run(Object[] args) { |
||||||
|
if (!PluginContexts.currentContext().isAvailable()) { |
||||||
|
LogKit.error("图片压缩函数许可证过期"); |
||||||
|
return Primitive.ERROR_VALUE; |
||||||
|
} |
||||||
|
|
||||||
|
if ((args == null) || (args.length <= 0)) { |
||||||
|
return Primitive.ERROR_VALUE; |
||||||
|
} |
||||||
|
|
||||||
|
if (args.length <= 1) { |
||||||
|
return args; |
||||||
|
} |
||||||
|
Object obj = args[0]; |
||||||
|
if (obj == null) { |
||||||
|
return Primitive.ERROR_VALUE; |
||||||
|
} |
||||||
|
|
||||||
|
Object obj1 = args[1]; |
||||||
|
if (obj1 == null) { |
||||||
|
return Primitive.ERROR_VALUE; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
float quality = Float.valueOf(String.valueOf(obj1)); |
||||||
|
if (!(obj instanceof FArray)) { |
||||||
|
return compressImage(obj, quality); |
||||||
|
} |
||||||
|
|
||||||
|
FArray objArray = (FArray) obj; |
||||||
|
FArray newArray = new FArray(); |
||||||
|
if (objArray.length() <= 0) { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
Object tempObj, compressObj; |
||||||
|
for (int i = 0, max = objArray.length() - 1; i <= max; i++) { |
||||||
|
tempObj = objArray.elementAt(i); |
||||||
|
compressObj = compressImage(tempObj, quality); |
||||||
|
newArray.add(compressObj); |
||||||
|
} |
||||||
|
return newArray; |
||||||
|
} catch (Exception e) { |
||||||
|
LogKit.error("jsdiehc图片压缩函数出错," + e.getMessage(), e); |
||||||
|
return Primitive.ERROR_VALUE; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private Object compressImage(Object obj, float quality) throws IOException { |
||||||
|
if (obj == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
if ((quality < 0) || (quality >= 1)) { |
||||||
|
quality = 1; |
||||||
|
} |
||||||
|
|
||||||
|
if (obj instanceof BufferedImage) { |
||||||
|
BufferedImage inputBufferedImage = (BufferedImage) obj; |
||||||
|
BufferedImage outBufferedImage = Thumbnails.of(inputBufferedImage).scale(1).outputQuality(quality).asBufferedImage(); |
||||||
|
return outBufferedImage; |
||||||
|
} |
||||||
|
|
||||||
|
if (!(obj instanceof Attachment)) { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
Attachment attachment = (Attachment) obj; |
||||||
|
if (!StringKit.equalsIgnoreCase(attachment.getType(), "image")) { |
||||||
|
return obj; |
||||||
|
} |
||||||
|
|
||||||
|
InputStream inputStream = attachment.getInputStream(); |
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
||||||
|
Thumbnails.of(inputStream).scale(1).outputQuality(quality).toOutputStream(outputStream); |
||||||
|
ImageAttachment imageAttachment = new ImageAttachment(); |
||||||
|
imageAttachment.setImageBytes(outputStream.toByteArray()); |
||||||
|
return imageAttachment; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Binary file not shown.
Loading…
Reference in new issue