下面列出了java.awt.image.BufferedImage#getPropertyNames() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static BufferedImage clone(BufferedImage source) {
if (source == null) {
return null;
}
try {
ColorModel cm = source.getColorModel();
Hashtable<String, Object> properties = null;
String[] keys = source.getPropertyNames();
if (keys != null) {
properties = new Hashtable<>();
for (String key : keys) {
properties.put(key, source.getProperty(key));
}
}
return new BufferedImage(cm, source.copyData(null), cm.isAlphaPremultiplied(), properties)
.getSubimage(0, 0, source.getWidth(), source.getHeight());
} catch (Exception e) {
logger.error(e.toString());
return null;
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
private static void validate(final BufferedImage bi, final int expected) {
final String[] names = bi.getPropertyNames();
if (names.length != expected) {
throw new RuntimeException("Wrong number of names");
}
for (final String name : names) {
final Object property = bi.getProperty(name);
if (property == Image.UndefinedProperty || property == null) {
throw new RuntimeException("Unexpected property");
}
}
}
public void dumpBIProps(final String prefix, final BufferedImage src) {
final String[] keys = src.getPropertyNames();
if (keys == null) {
LOGGER.fine(prefix + ": no props");
return;
}
for (final String key : keys) {
LOGGER.fine(prefix + ": " + key + ": "
+ src.getProperty(key));
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}
public static void main(final String[] args) {
// default result is null
if (defaultProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for null properties result is null
final BufferedImage emptyProps = getBufferedImage(null);
if (emptyProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for empty properties result is null
final BufferedImage nullProps = getBufferedImage(new Properties());
if (nullProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for non-string keys result is null
final Properties properties = new Properties();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
final BufferedImage nonStringProps = getBufferedImage(properties);
if (nonStringProps.getPropertyNames() != null) {
throw new RuntimeException("PropertyNames should be null");
}
// for string keys result is not null
properties.clear();
properties.setProperty("1", "1");
properties.setProperty("2", "2");
validate(getBufferedImage(properties), 2);
// for the mix of strings and objects result is not null
properties.clear();
properties.put(1, 1);
properties.put(2, 2);
properties.put(3, 3);
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
final BufferedImage mixProps = getBufferedImage(properties);
validate(mixProps, 2);
if (!"value1".equals(mixProps.getProperty("key1"))
|| !"value2".equals(mixProps.getProperty("key2"))) {
throw new RuntimeException("Wrong key-value pair");
}
}