001package gu.dtalk; 002 003import com.google.common.base.Throwables; 004 005import net.gdface.image.ImageErrorException; 006import net.gdface.image.BaseLazyImage; 007import net.gdface.utils.FaceUtilits; 008import net.gdface.utils.Judge; 009 010/** 011 * 图像类型选项 012 * @author guyadong 013 * 014 */ 015public class ImageOption extends BaseBinary { 016 private volatile boolean updated = false; 017 private BaseLazyImage image; 018 public ImageOption() { 019 } 020 021 @Override 022 public OptionType getType() { 023 return OptionType.IMAGE; 024 } 025 @Override 026 public ImageOption setValue(byte[] value) { 027 synchronized(this){ 028 super.setValue(value); 029 updated = false; 030 } 031 return this; 032 } 033 034 /** 035 * 036 * @return 返回图像对象,如果值为空则返回{@code null} 037 * @throws ImageErrorException 038 */ 039 public BaseLazyImage imageObj() throws ImageErrorException{ 040 if(Judge.isEmpty(getValue())){ 041 return null; 042 } 043 if(!updated){ 044 synchronized (this) { 045 if(!updated){ 046 image = BaseLazyImage.getLazyImageFactory().create(getValue()); 047 updated = true; 048 } 049 } 050 } 051 052 return image; 053 } 054 /** 055 * 与{@link #imageObj()}类似,只是所有的异常都被封装到{@link RuntimeException} 056 * @return 返回图像对象,如果值为空则返回{@code null} 057 */ 058 public BaseLazyImage imageObjUncheck(){ 059 try { 060 return imageObj(); 061 } catch (Exception e) { 062 Throwables.throwIfUnchecked(e); 063 throw new RuntimeException(e); 064 } 065 } 066 public int getWidth(){ 067 try{ 068 return imageObj().getWidth(); 069 } catch (Exception e) { 070 return 0; 071 } 072 } 073 public int getHeight(){ 074 try{ 075 return imageObj().getHeight(); 076 } catch (Exception e) { 077 return 0; 078 } 079 } 080 public String getSuffix() throws ImageErrorException{ 081 try{ 082 return imageObj().getSuffix(); 083 } catch (Exception e) { 084 return null; 085 } 086 } 087 /** 088 * 从input(Base64格式)中解码为byte[]调用{@link #setDefaultValue(Object)} 089 * @see gu.dtalk.BaseOption#asValue(java.lang.String) 090 */ 091 @Override 092 public ImageOption asValue(String input) { 093 try { 094 setValue(FaceUtilits.getBytes(input)); 095 return this; 096 } catch (Exception e) { 097 Throwables.throwIfUnchecked(e); 098 throw new RuntimeException(e); 099 } 100 } 101 /** 102 * 从input中读取字节流转为byte[]调用{@link #setValue(byte[])} 103 * @param <T> 参见 {@link FaceUtilits#getBytes(Object)} 104 */ 105 public <T>ImageOption asValue(T input) { 106 super.asDefaultValue(input); 107 return this; 108 } 109 /** 110 * 从input(Base64格式)中解码为byte[]调用{@link #setDefaultValue(Object)} 111 * @see gu.dtalk.BaseOption#asDefaultValue(java.lang.String) 112 */ 113 @Override 114 public ImageOption asDefaultValue(String input) { 115 try { 116 setDefaultValue(FaceUtilits.getBytes(input)); 117 return this; 118 } catch (Exception e) { 119 Throwables.throwIfUnchecked(e); 120 throw new RuntimeException(e); 121 } 122 } 123 /** 124 * 从input中读取字节流转为byte[]调用{@link #setValue(byte[])} 125 * @param <T> 参见 {@link FaceUtilits#getBytes(Object)} 126 */ 127 public <T>ImageOption asDefaultValue(T input) { 128 super.asDefaultValue(input); 129 return this; 130 } 131 /** 132 * 从input中读取字节流转为byte[]调用{@link #setDefaultValue(Object)} 133 * @param <T> 参见 {@link FaceUtilits#getBytes(Object)} 134 */ 135 public <T>ImageOption asValue(BaseLazyImage input) { 136 setValue(input.wirteJPEGBytes()); 137 this.image = input; 138 return this; 139 } 140 /** 141 * 从input中读取字节流转为byte[]调用{@link #setDefaultValue(Object)} 142 * @param <T> 参见 {@link FaceUtilits#getBytes(Object)} 143 */ 144 public <T>ImageOption asDefaultValue(BaseLazyImage input) { 145 setDefaultValue(input.wirteJPEGBytes()); 146 this.image = input; 147 return this; 148 } 149 150 @Override 151 public String contentOfValue() { 152 try { 153 BaseLazyImage img = imageObj(); 154 if(img != null){ 155 return String.format("%s(%dx%d),size=%d", img.getSuffix(),img.getWidth(),img.getHeight(),getValue().length); 156 } 157 } catch (ImageErrorException e) { 158 return e.getMessage(); 159 } 160 161 return super.contentOfValue(); 162 } 163}