- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何删除覆盖有相同颜色的特定像素和像素区域的颜色。我可以使用下面的快速代码获得特定的像素颜色,但我无法获得具有相同颜色像素的周围区域并且无法擦掉它。
import UIKit
class ColorOfImage: UIImageView {
var lastColor:UIColor? = nil
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.isHidden == true {
self.next?.touchesEnded(touches, with: event)
return
}
let touch: UITouch = touches.first!
var point:CGPoint = touch.location(in: self)
self.lastColor = self.getPixelColorAtLocation(point:point)
}
public func createARGBBitmapContext(inImage: CGImage) -> CGContext {
var bitmapByteCount = 0
var bitmapBytesPerRow = 0
//Get image width, height
let pixelsWide = inImage.width
let pixelsHigh = inImage.height
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = Int(pixelsWide) * 4
bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
// Use the generic RGB color space.
let colorSpace = CGColorSpaceCreateDeviceRGB()
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
let bitmapData = malloc(bitmapByteCount)
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
let context = CGContext(data: bitmapData, width: pixelsWide, height: pixelsHigh, bitsPerComponent: 8, bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
// Make sure and release colorspace before returning
return context!
}
public func getPixelColorAtLocation( point:CGPoint) -> UIColor {
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
var point = point
var context:CGContext? = nil
context = self.createARGBBitmapContext(inImage: (self.image?.cgImage)!)
if context == nil {
return UIColor.white
}
var pixelsWide = (self.image?.cgImage)!.width
var pixelsHigh = (self.image?.cgImage)!.height
var rect = CGRect(x:0, y:0, width:Int(pixelsWide), height:Int(pixelsHigh))
var xScale:CGFloat = CGFloat(pixelsWide)/self.frame.size.width
var yScale:CGFloat = CGFloat(pixelsHigh)/self.frame.size.height
point.x = point.x * xScale
point.y = point.y * yScale
var x:CGFloat = 1.0
if (self.image?.responds(to: #selector(getter: self.image?.scale)))! {
x = ( self.image!.scale)
}
//Clear the context
context?.clear(rect)
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
context?.draw((self.image?.cgImage)!, in: rect)
// Now we can get a pointer to the image data associated with the bitmap
// context.
let data = context?.data
// let dataType = UnsafePointer<UInt8>(data)
var color:UIColor? = nil
if data != nil {
let dataType = data?.assumingMemoryBound(to: UInt8.self)
let offset = 4*((Int(pixelsWide) * Int(point.y)) + Int(point.x))
let alpha = dataType?[offset]
let red = dataType?[offset+1]
let green = dataType?[offset+2]
let blue = dataType?[offset+3]
color = UIColor(red: CGFloat(red!)/255.0, green: CGFloat(green!)/255.0, blue: CGFloat(blue!)/255.0, alpha: CGFloat(alpha!)/255.0)
}
else
{
}
// Free image data memory for the context
free(data)
return color!;
}
}
例如,如果我触摸 UIImage 和特定坐标,那么它必须获取颜色并删除具有相同颜色的像素和周围像素,所有这些像素都必须删除。有人可以帮我解决这个问题吗。
最佳答案
一个月前,我开发了一个应用程序,在该应用程序中,您可以获取图像并且必须交换颜色……我不知道它是否对您有用。我为它做了一个 UIImage 的类别。
这里是 Objective-C 中的代码:
#import <UIKit/UIKit.h>
@interface UIImage (YPKInterface)
+ (UIImage *)swapImage:(UIImage *)image color:(UIColor *)originalColor withColor:(UIColor *)swappedColor andThreshold:(float)threshold;
@end
#import "UIImage+YPKInterface.h"
@implementation UIImage (YPKInterface)
+ (UIImage *)swapImage:(UIImage *)image color:(UIColor *)originalColor withColor:(UIColor *)swappedColor andThreshold:(float)threshold {
int count = image.size.width*image.size.height;
// Convert image in raw data
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Scan all the pixels
NSUInteger byteIndex = 0;
for (int i = 0 ; i < count ; ++i)
{
CGFloat alpha = ((CGFloat) rawData[byteIndex + 3] ) / 255.0f;
CGFloat red = ((CGFloat) rawData[byteIndex] ) / alpha;
CGFloat green = ((CGFloat) rawData[byteIndex + 1] ) / alpha;
CGFloat blue = ((CGFloat) rawData[byteIndex + 2] ) / alpha;
// Change the color
// RGBA of the colors
float origR = CGColorGetComponents(originalColor.CGColor)[0] * 255;
float origG = CGColorGetComponents(originalColor.CGColor)[1] * 255;
float origB = CGColorGetComponents(originalColor.CGColor)[2] * 255;
//float origA = CGColorGetComponents(originalColor.CGColor)[3];
float swapR = CGColorGetComponents(swappedColor.CGColor)[0] * 255;
float swapG = CGColorGetComponents(swappedColor.CGColor)[1] * 255;
float swapB = CGColorGetComponents(swappedColor.CGColor)[2] * 255;
//float swapA = CGColorGetComponents(swappedColor.CGColor)[3];
if (red >= origR - threshold && red <= origR + threshold &&
green >= origG - threshold && green <= origG + threshold &&
blue >= origB - threshold && blue <= origB + threshold) {
rawData[byteIndex + 3] = alpha * 255;
rawData[byteIndex ] = swapR * alpha;
rawData[byteIndex + 1] = swapG * alpha;
rawData[byteIndex + 2] = swapB * alpha;
}
byteIndex += bytesPerPixel;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rawData, width*height*4, NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo =
kCGBitmapByteOrderDefault | kCGImageAlphaLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef2 = CGImageCreate(width, height, 8, 32, 4*width,colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
UIImage *newImage = [UIImage imageWithCGImage:imageRef2];
return newImage;
}
@end
关于ios - 用相同像素颜色覆盖的区域并更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712718/
在使用 GDI 进行图形处理的传统 Windows 程序中,您必须担心只绘制需要重绘的窗口区域;这是“更新矩形”,可以通过 PAINTSTRUCT.rcPaint 或通过调用 GetUpdateRec
我对 TFS 中的所有内容感到困惑。有人可以解释一下所有这些是如何组合在一起的吗? 团队项目合集 团队项目 团队 地区 迭代 来自 this page,我认为一个(团队)项目集合可以包含多个(团队)项
这对我来说根本没有意义。创建新的 API 网关时,您可以指定它是区域优化的还是边缘优化的。但话又说回来,当您为 API Gateway 创建自定义域名时,您可以在两者之间进行选择。 最糟糕的是,您可以
我有一个工作表,其中包含我想循环遍历的许多不同区域。我将进行一些计算,并认为它比在列中逐个单元格地更快/更有效。 我尝试了一些不同的东西,但不知道如何跳到下一个区域。我已经发表了一些评论,最有希望的是
最近迁移到 TFS 2010 后,我想知道对于区域而言,最佳或最广泛接受的定义或配置是什么? 我能在网上找到的唯一有用的文章是 this one并且是我认为是正确的。然而,这让我开始思考是否以下任何一
我在“北欧”有一个存储帐户,现在我想使用生命周期管理将冷 blob 移动到存档层。 但是我每次尝试都会遇到以下失败:无法为存储帐户“myStorageAccount”添加生命周期管理规则。错误:管理策
我正在开发一个项目,您在扫描仪中输入州的缩写,然后程序会告诉您该州位于美国的哪个地区。我认为我的大部分代码都是正确的,但我有以前从未使用过 switch。 我已经将我的工作改进为一个错误,即我的第一个
我正在尝试学习 AngularJS,我想我有一个架构问题。 如果我想开始开发一个应用程序,比方说,比如 youtube,我如何在“区域”(比如 Marionette)中组织页面?我的意思是:顶部导航栏
我正在制作一个动态内存分配器,当我释放其中的一部分时,我需要检查我传递给函数的指针实际上是否在该区域内。我有一个指向 malloc 区域开头的指针 typedef unsigned char byte
有时我想看看 到底在哪里页面上图片上的标签。 在 Javascript 中有没有办法改变颜色或隐藏与区域标签对应的图像部分? 最佳答案 也许您正在寻找类似 mapper.js 的内容.它允许您在鼠标
我有一个使用 D3 js 创建的时间序列图表。我想为特定时间间隔添加高亮区域,以显示在该特定时间发生的特定事件(会有不同类型的事件,因此每个高亮标记将根据其类型具有不同的颜色)。我希望这个突出显示区域
我正在尝试创建网站的密码保护区。我想通过检查 MySql 表中的用户名和密码来允许访问,然后启动一个 session 并允许在 session 处于事件状态时访问多个页面。如果有人尝试直接访问这些页面
在 ScrollView 中我添加了几个按钮。正如您在图片中看到的,2 个按钮完全可见,第三个按钮半可见。当我向右滚动看到第三个时;1-如果滚动条很短,则它会滚动回到第一个位置。2- 如果滚动足够则显
我正在开发我的第一个 spritekit 应用程序,并且有一个关于如何处理我想到的事情的快速问题。我正在制作一个我想要的棋盘游戏原型(prototype),并希望在 iOS map 中进行一些集思广益
在我的 C# 程序中,我收到这样的日期和时间:DateTime.Now 我得到:19/09/2010 20:10:30 因为我的地区是:希伯来语(以色列) 但是如果我将我的程序安装在区域为 Engli
有时我在 Visual Studio 的源文件中运行(在我的例子中是 2010),我看到很多区域,我必须点击 + 号才能一个一个地打开它们! 是否有任何快捷方式或菜单选项可以使这对我来说更容易,并且对
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
我正在寻找二维数据中的峰值区域(如果您愿意,灰度图像或二维景观,通过霍夫变换创建)。 峰值区域是指局部最大峰值,但不是单个点而是周围的一部分strong>contributing region 随之而
我希望能够将任何字符或字符串转换为形状或区域,以便我可以按照我喜欢的任何大小、样式、效果等来绘制该字符。 更具体地说,我将使用视差绘制它,以便它仅在特定角度清晰定义(这就是为什么我不能使用 html
我非常喜欢数学(或者你们大多数人会说的“数学”!),但我还没有达到知道这个问题答案的程度。我有一个主圆,它可以在显示器上的任何 x 和 y 处有一个中心点。其他圆圈将随意在显示器周围移动,但在任何给定
我是一名优秀的程序员,十分优秀!