我正在尝试使用GPUImage将图像转换为灰度图像。我写了一个扩展来完成我的工作。灰度的东西还可以。但输出图像的尺寸增加了一倍。在我的情况下,我需要图像的大小。有人可以帮我这个吗?任何帮助将受到高度赞赏。
这是我写的扩展
import UIKit
import GPUImage
extension UIImage {
public func grayscale() -> UIImage?{
var processedImage = self
print("1: "+"\(processedImage.size)")
let image = GPUImagePicture(image: processedImage)
let grayFilter = GPUImageGrayscaleFilter()
image?.addTarget(grayFilter)
grayFilter.useNextFrameForImageCapture()
image?.processImage()
processedImage = grayFilter.imageFromCurrentFramebuffer()
print("2: "+"\(processedImage.size)")
return processedImage
}
}
这是控制台中的输出
编辑:我知道稍后可以调整图片大小。但需要知道为什么会发生这种情况,并且有什么办法可以保持图像大小,就像使用GPUImage一样。
答案 0 :(得分:1)
尝试稍后缩放图像:
if let cgImage = processedImage.cgImage {
//The scale value 2.0 here should be replaced by the original image's scale.
let scaledImage = UIImage(cgImage: cgImage, scale: 2.0, orientation: processedImage.imageOrientation)
return scaledImage
}
答案 1 :(得分:0)
我不知道@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = {"com.arcvideo.cloudagent.repo", "com.arcvideo.cloudagent.customer"})
@PropertySource("classpath:database.properties")
public class DatabaseConfig {
@Autowired
private Environment env;
@Bean(destroyMethod = "close")
DataSource dataSource() {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setPoolName("springHikariCP");
dataSourceConfig.setConnectionTestQuery("SELECT 1");
dataSourceConfig.setDataSourceClassName(env.getProperty("hibernate.connection.driver_class"));
Properties dataSourceProperties = new Properties();
dataSourceProperties.put("url", env.getProperty("hibernate.connection.url"));
dataSourceProperties.put("user", env.getProperty("hibernate.connection.username"));
dataSourceProperties.put("password", env.getProperty("hibernate.connection.password"));
dataSourceConfig.setDataSourceProperties(dataSourceProperties);
return new HikariDataSource(dataSourceConfig);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource());
entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
Properties additionalProperties = new Properties();
additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
additionalProperties.put("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
additionalProperties.put("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
additionalProperties.put("hibernate.cache.region.factory_class", env.getProperty("hibernate.cache.region.factory_class"));
additionalProperties.put("hibernate.autoReconnect", env.getProperty("hibernate.autoReconnect"));
additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
additionalProperties.put("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
additionalProperties.put("javax.persistence.sharedCache.mode", env.getProperty("javax.persistence.sharedCache.mode"));
additionalProperties.put("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}
}
库如何将图像转换为灰度图像。我猜测他们的实现会扩大图像范围。您可以自己缩小它,或者我建议您使用以下代码将图像转换为灰度
GPUImage