下面列出了java.awt.Image#flush() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
*/
final void flush( )
{
if ( htCache.isEmpty( ) )
{
return;
}
Image img;
final int n = htCache.size( );
Enumeration<Image> eV = htCache.elements( );
while ( eV.hasMoreElements( ) )
{
img = eV.nextElement( );
img.flush( );
}
htCache.clear( );
logger.log( ILogger.INFORMATION,
Messages.getString( "SwingImageCache.info.flushed.swing.images",//$NON-NLS-1$
new Object[]{
Integer.valueOf( n )
},
idsSWING.getULocale( ) ) );
}
private static void draw(Shape clip, Shape to, Image vi, BufferedImage bi,
int scale) {
Graphics2D big = bi.createGraphics();
big.setComposite(AlphaComposite.Src);
big.setClip(clip);
Rectangle toBounds = to.getBounds();
int x1 = toBounds.x;
int y1 = toBounds.y;
int x2 = x1 + toBounds.width;
int y2 = y1 + toBounds.height;
big.drawImage(vi, x1, y1, x2, y2, 0, 0, toBounds.width / scale,
toBounds.height / scale, null);
big.dispose();
vi.flush();
}
private static void draw(Shape clip, Shape to, Image vi, BufferedImage bi,
int scale) {
Graphics2D big = bi.createGraphics();
big.setComposite(AlphaComposite.Src);
big.setClip(clip);
Rectangle toBounds = to.getBounds();
int x1 = toBounds.x;
int y1 = toBounds.y;
int x2 = x1 + toBounds.width;
int y2 = y1 + toBounds.height;
big.drawImage(vi, x1, y1, x2, y2, 0, 0, toBounds.width / scale,
toBounds.height / scale, null);
big.dispose();
vi.flush();
}
protected BufferedImage makeImage( final ImageProcessor ip, final FloatProcessor mask )
{
final BufferedImage transformedImage = new BufferedImage( ip.getWidth(), ip.getHeight(), null == mask ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB );
final Image img = ip.createImage();
transformedImage.createGraphics().drawImage( img, 0, 0, null );
img.flush();
if (null != mask) {
transformedImage.getAlphaRaster().setPixels( 0, 0, ip.getWidth(), ip.getHeight(), ( float[] )mask.getPixels() );
}
return transformedImage;
}
public static void clear() {
for (Image img : lazyImages.values()) {
img.flush();
img = null;
}
lazyImages.clear();
}
private static final BufferedImage toBufferedImage(Image img) {
// load the image
new javax.swing.ImageIcon(img);
java.awt.image.BufferedImage rep = createBufferedImage(img.getWidth(null), img.getHeight(null));
java.awt.Graphics g = rep.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
img.flush();
return rep;
}
private static final BufferedImage toBufferedImage(Image img) {
// load the image
new ImageIcon(img);
BufferedImage rep = createBufferedImage(img.getWidth(null), img.getHeight(null));
Graphics g = rep.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
img.flush();
return rep;
}
public void loadMaps(final ResourceLoader loader) {
final Image smallFromFile =
loadImage(loader, Constants.SMALL_MAP_FILENAME, Constants.SMALL_MAP_EXTENSIONS);
smallMapImage =
Util.newImage(smallFromFile.getWidth(null), smallFromFile.getHeight(null), false);
final Graphics g = smallMapImage.getGraphics();
g.drawImage(smallFromFile, 0, 0, null);
g.dispose();
smallFromFile.flush();
}
ActionResult<Wo> execute(EffectivePerson effectivePerson) throws Exception {
//Audit audit = logger.audit(effectivePerson);
ActionResult<Wo> result = new ActionResult<>();
Wo wo = new Wo();
String meta = StringTools.uniqueToken();
/** 二维码内容 */
String url = UriBuilder.fromUri(Config.collect().getAppUrl()).queryParam("meta", meta).build().toASCIIString();
int width = 200; // 二维码图片宽度
int height = 200; // 二维码图片高度
String format = "png";// 二维码的图片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// hints.put(EncodeHintType.CHARACTER_SET, DefaultCharset.name); //
// 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, "1");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q.toString());
// hints.put(EncodeHintType.QR_VERSION, "7");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
Graphics2D graphics = image.createGraphics();
Image logo = ImageIO.read(new ByteArrayInputStream(Config.bindLogo()));
graphics.drawImage(logo, 68, 68, null);
graphics.dispose();
logo.flush();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ImageIO.write(image, format, out);
wo.setImage(Base64.encodeBase64String(out.toByteArray()));
}
wo.setMeta(meta);
result.setData(wo);
return result;
}
/**
* Generate icon image.
*
* @param srcImage source image
* @param width image width
* @param height image height
* @param forceIntRGB set true when generate apple watch icon
* @throws Exception exception
*/
public BufferedImage generateIconImage(BufferedImage srcImage, int width, int height, boolean forceIntRGB) throws Exception {
BufferedImage buf = this.createBufferedImage(srcImage, width, height, forceIntRGB);
int hints = getScalingAlgorithm();
Image img = srcImage.getScaledInstance(width, height, hints);
if (forceIntRGB) {
Graphics g = buf.getGraphics();
g.setColor(ImageFile.getDefaultBackgroundColor(srcImage));
g.fillRect(0, 0, width, height);
}
buf.getGraphics().drawImage(img, 0, 0, MainFrame.this);
img.flush();
img = null;
return forceIntRGB ? buf : fixImageColor(buf, srcImage);
}
@Override
public void destroyBackBuffer(final Image backBuffer) {
if (backBuffer != null) {
backBuffer.flush();
}
}
/**
* Sets the cached image for the specified constraints.
*
* @param image The image to store in cache
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return true if the image could be cached or false if the image is too big
*/
public boolean setImage(Image image, GraphicsConfiguration config, int w, int h, Object... args) {
if (!isImageCachable(w, h)) return false;
int hash = hash(config, w, h, args);
lock.writeLock().lock();
try {
PixelCountSoftReference ref = map.get(hash);
// check if currently in map
if (ref != null && ref.get() == image) {
return true;
}
// clear out old
if (ref != null) {
currentPixelCount -= ref.pixelCount;
map.remove(hash);
}
// add new image to pixel count
int newPixelCount = image.getWidth(null) * image.getHeight(null);
currentPixelCount += newPixelCount;
// clean out lost references if not enough space
if (currentPixelCount > maxPixelCount) {
while ((ref = (PixelCountSoftReference)referenceQueue.poll()) != null){
//reference lost
map.remove(ref.hash);
currentPixelCount -= ref.pixelCount;
}
}
// remove old items till there is enough free space
if (currentPixelCount > maxPixelCount) {
Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
mapIter.remove();
Image img = entry.getValue().get();
if (img != null) img.flush();
currentPixelCount -= entry.getValue().pixelCount;
}
}
// finaly put new in map
map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount,hash, config, w, h, args));
return true;
} finally {
lock.writeLock().unlock();
}
}
@Override
public void destroyBackBuffer(final Image backBuffer) {
if (backBuffer != null) {
backBuffer.flush();
}
}
/**
* Sets the cached image for the specified constraints.
*
* @param image The image to store in cache
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return true if the image could be cached or false if the image is too big
*/
public boolean setImage(Image image, GraphicsConfiguration config, int w, int h, Object... args) {
if (!isImageCachable(w, h)) return false;
int hash = hash(config, w, h, args);
lock.writeLock().lock();
try {
PixelCountSoftReference ref = map.get(hash);
// check if currently in map
if (ref != null && ref.get() == image) {
return true;
}
// clear out old
if (ref != null) {
currentPixelCount -= ref.pixelCount;
map.remove(hash);
}
// add new image to pixel count
int newPixelCount = image.getWidth(null) * image.getHeight(null);
currentPixelCount += newPixelCount;
// clean out lost references if not enough space
if (currentPixelCount > maxPixelCount) {
while ((ref = (PixelCountSoftReference)referenceQueue.poll()) != null){
//reference lost
map.remove(ref.hash);
currentPixelCount -= ref.pixelCount;
}
}
// remove old items till there is enough free space
if (currentPixelCount > maxPixelCount) {
Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
mapIter.remove();
Image img = entry.getValue().get();
if (img != null) img.flush();
currentPixelCount -= entry.getValue().pixelCount;
}
}
// finaly put new in map
map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount,hash, config, w, h, args));
return true;
} finally {
lock.writeLock().unlock();
}
}
@Override
public void destroyBackBuffer(final Image backBuffer) {
if (backBuffer != null) {
backBuffer.flush();
}
}
/**
* Sets the cached image for the specified constraints.
*
* @param image The image to store in cache
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return true if the image could be cached or false if the image is too big
*/
public boolean setImage(Image image, GraphicsConfiguration config, int w, int h, Object... args) {
if (!isImageCachable(w, h)) return false;
int hash = hash(config, w, h, args);
lock.writeLock().lock();
try {
PixelCountSoftReference ref = map.get(hash);
// check if currently in map
if (ref != null && ref.get() == image) {
return true;
}
// clear out old
if (ref != null) {
currentPixelCount -= ref.pixelCount;
map.remove(hash);
}
// add new image to pixel count
int newPixelCount = image.getWidth(null) * image.getHeight(null);
currentPixelCount += newPixelCount;
// clean out lost references if not enough space
if (currentPixelCount > maxPixelCount) {
while ((ref = (PixelCountSoftReference)referenceQueue.poll()) != null){
//reference lost
map.remove(ref.hash);
currentPixelCount -= ref.pixelCount;
}
}
// remove old items till there is enough free space
if (currentPixelCount > maxPixelCount) {
Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
mapIter.remove();
Image img = entry.getValue().get();
if (img != null) img.flush();
currentPixelCount -= entry.getValue().pixelCount;
}
}
// finaly put new in map
map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount,hash, config, w, h, args));
return true;
} finally {
lock.writeLock().unlock();
}
}
@Override
public void destroyBackBuffer(final Image backBuffer) {
if (backBuffer != null) {
backBuffer.flush();
}
}
ActionResult<Wo> execute(EffectivePerson effectivePerson, String meetingId) throws Exception {
Audit audit = logger.audit(effectivePerson);
ActionResult<Wo> result = new ActionResult<>();
Wo wo = new Wo();
String meta = StringTools.uniqueToken();
ApplicationServer applicationServer = Config.currentNode().getApplication();
Boolean sslEnable = applicationServer.getSslEnable();
String host = applicationServer.getProxyHost();
int port = applicationServer.getProxyPort();
String applicationUrl = getApplicationUrl(sslEnable, host, port) + "/x_meeting_assemble_control/jaxrs/meeting/" + meetingId + "/checkin";
/** 二维码内容 */
String url = UriBuilder.fromUri(applicationUrl).build().toASCIIString();
int width = 200; // 二维码图片宽度
int height = 200; // 二维码图片高度
String format = "png";// 二维码的图片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, "1");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q.toString());
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
Graphics2D graphics = image.createGraphics();
Image logo = ImageIO.read(new ByteArrayInputStream(Config.bindLogo()));
graphics.drawImage(logo, 68, 68, null);
graphics.dispose();
logo.flush();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ImageIO.write(image, format, out);
wo.setImage(Base64.encodeBase64String(out.toByteArray()));
}
wo.setMeta(meta);
result.setData(wo);
audit.log(effectivePerson.getDistinguishedName(), "认证码发送.");
return result;
}
ActionResult<Wo> execute(EffectivePerson effectivePerson, String meetingId) throws Exception {
Audit audit = logger.audit(effectivePerson);
ActionResult<Wo> result = new ActionResult<>();
Wo wo = new Wo();
String meta = StringTools.uniqueToken();
ApplicationServer applicationServer = Config.currentNode().getApplication();
Boolean sslEnable = applicationServer.getSslEnable();
String host = applicationServer.getProxyHost();
int port = applicationServer.getProxyPort();
String applicationUrl = getApplicationUrl(sslEnable, host, port) + "/x_meeting_assemble_control/jaxrs/meeting/" + meetingId + "/checkin";
/** 二维码内容 */
String url = UriBuilder.fromUri(applicationUrl).build().toASCIIString();
int width = 200; // 二维码图片宽度
int height = 200; // 二维码图片高度
String format = "png";// 二维码的图片格式
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
// 内容所使用字符集编码
hints.put(EncodeHintType.MARGIN, "1");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q.toString());
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
Graphics2D graphics = image.createGraphics();
Image logo = ImageIO.read(new ByteArrayInputStream(Config.bindLogo()));
graphics.drawImage(logo, 68, 68, null);
graphics.dispose();
logo.flush();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ImageIO.write(image, format, out);
wo.setImage(Base64.encodeBase64String(out.toByteArray()));
}
wo.setMeta(meta);
result.setData(wo);
audit.log(effectivePerson.getDistinguishedName(), "认证码发送.");
return result;
}
@Override
public void destroyBackBuffer(final Image backBuffer) {
if (backBuffer != null) {
backBuffer.flush();
}
}