我正在尝试使用以下方法比较两个图像:
[DllImport("msvcrt.dll")]
private static extern int memcmp(IntPtr b1, IntPtr b2, long count);
public static bool CompareMemCmp(Bitmap b1, Bitmap b2)
{
if ((b1 == null) != (b2 == null)) return false;
if (b1.Size != b2.Size) return false;
var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
try
{
IntPtr bd1scan0 = bd1.Scan0;
IntPtr bd2scan0 = bd2.Scan0;
int stride = bd1.Stride;
int len = stride * b1.Height;
return memcmp(bd1scan0, bd2scan0, len) …Run Code Online (Sandbox Code Playgroud) 可能的重复:
使用类似于检查的 GDI+(或 GDI)在屏幕上绘制
我正在尝试编写一个没有窗口的蛇游戏,但冻结了前景并将蛇画在它上面。当游戏结束时,前景应该解冻。
我写了一些测试代码,应该在前景上绘制一个正方形,但它似乎只是冻结桌面一秒钟,然后冻结前景中的窗口,直到我最小化、最大化、关闭它或带来另一个窗口进入前景,它不会绘制任何东西。在代码中,我尝试存储桌面的位图,以便我基本上可以将其重置为原始状态并在不同的位置绘制正方形。任何人都可以发现我的代码的问题吗?
//Handle to the desktop window
HWND hDesktopWindow = GetDesktopWindow();
//Lock the window to prevent other applications drawing on it
if(!LockWindowUpdate(hDesktopWindow)){
return 1;
}
//Calling GetDC with argument NULL retrieves the desktop's DC
HDC hdcDesktop = GetDCEx(hDesktopWindow, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
//Create a compatible DC to allow us to store a bitmap of the desktop
HDC hdcCompatible;
if((hdcCompatible = CreateCompatibleDC(hdcDesktop)) == NULL){
return 1;
}
//Create a compatible bitmap with the same dimensions as the …Run Code Online (Sandbox Code Playgroud) 我有位图
uint64_t bitmap[10000]
Run Code Online (Sandbox Code Playgroud)
跟踪系统中分配的资源。现在的问题是如何有效地找到此位图中的第一个未设置(零)位?
我知道ffsll(unsigned long long)glibc 中有用于查找第一个设置位,我假设它使用硬件指令来完成。
要在我的情况下使用此函数,首先我需要初始化数组以将每一位设置为 1,然后当我进行资源分配时,我必须线性搜索数组以查找第一个非零字。然后使用 ffsll() 找到第一个设置位。
我怎样才能做得更快?
更新:我在 x86-64 cpu 上。
我正在寻找将图像(png、jpeg 等)转换为 1bit/px 位图图像的命令和选项(类似于在 photoshop 中将其转换为位图时发生的情况)。请参阅下面的示例
我想从 Rails 应用程序(例如 RMagick 或通过 ImageMagick)中以编程方式执行此操作

以下工作:
convert yo.jpg -remap pattern:gray30 mono.gif
Run Code Online (Sandbox Code Playgroud)
但与 phosotsohp 在那种情况下会做什么相比,我想更好地了解正在发生的事情;考虑到这里解释的许多可能性 (@roger_rowland thx 再次提供链接)
最后我想要做的是有这种“效果”,并放大图像而不会产生模糊效果
我正在尝试将普通彩色图像转换为灰度图像。代码非常简单,但不知道为什么会出现错误。我只是逐个像素地更改颜色值,然后将其存储在新的位图中。错误即将到来我正在尝试将像素设置为新位图。
Bitmap c=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.jpg");
int width=c.getWidth();
int height=c.getHeight();
int A,B,R,G;
int pixel;
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
// get one pixel color
// pixel = c.getPixel(x, y);
// retrieve color of all channels
// A = Color.alpha(c.getPixel(x, y));
R = Color.red(c.getPixel(x, y));
G = Color.green(c.getPixel(x, y));
B = Color.blue(c.getPixel(x, y));
// take conversion up to one single value
R = G = B = (int)(0.299 * …Run Code Online (Sandbox Code Playgroud) 我通过这样做得到我的 ARGB_8888 位图的像素数据:
public void getImagePixels(byte[] pixels, Bitmap image) {
// calculate how many bytes our image consists of
int bytes = image.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
pixels = buffer.array(); // Get the underlying array containing the data.
}
Run Code Online (Sandbox Code Playgroud)
但是,我想将每个像素存储在四个字节 (ARGB) 上的数据转换为每个像素存储在 3 个字节 ( BGR ) 上的位置。
任何帮助表示赞赏!
我需要创建一个自定义视图,然后将其保存到 png 文件到 sdcard 中。现在我在 sdcard 中得到黑色图像。我无法在代码中找出问题。任何人都可以帮助我。
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
</LinearLyout>
Run Code Online (Sandbox Code Playgroud)
在我的 java 文件中,我膨胀了线性布局并将数据设置为 textviews,然后我调用:
private Bitmap convertViewToBitmap(LinearLayout layout) {
layout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();
//Create the bitmap
Bitmap bitmap = Bitmap.createBitmap(width,
height,
Bitmap.Config.ARGB_8888);
//Create a canvas with the specified bitmap to draw into
Canvas c = new Canvas(bitmap);
//Render this view (and all of …Run Code Online (Sandbox Code Playgroud) 快速提问:
我有这个1000 x 1000位图图像:
我使用这个例程来加载它:
private BitmapSource initialBitmap = new BitmapImage(new Uri("C:\\Users\\...\\Desktop\\Original.bmp"));
Run Code Online (Sandbox Code Playgroud)
为什么在我加载它之后,并在我跨过上面的行之后,我看到它是800 x 800?
PS 我希望它是 1000 x 1000 并且不使用任何调整大小功能。它正在工作,突然变成了 800*800 !
我正在尝试使用 viewpager 为具有 4 张图像的应用程序创建介绍之旅。从上一张图片滑到下一张图片需要很长时间。
我在滑动时登录 logcat
I/Choreographer: Skipped 100 frames! The application may be doing too much work on its main thread.
I/Choreographer: Skipped 94 frames! The application may be doing too much work on its main thread.
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
1 setImageResource() 在 UI 线程上执行 Bitmap 读取和解码,这可能会导致延迟打嗝。如果这是一个问题,请考虑使用 2.setImageDrawable(android.graphics.drawable.Drawable) 或 3.setImageBitmap(android.graphics.Bitmap) 和 BitmapFactory。图像的大小和分辨率也可能是导致滚动缓慢的原因,请尝试创建大图像的缩略图并使用。(我将可绘制的图像大小从 100kb 减少到 30-50kb)
还尝试了下面的堆栈溢出答案:
静止图像在视图寻呼机中从上一个图像到下一个图像花费很多时间。我在不同的设备上测试过。在 moto x 上播放速度最慢,而其他设备,如三星、moto E、联想,速度稍快,但仍达不到标准。
我在 drawable 文件夹中有 4 张图像,并使用下面的代码尝试滑动图像。
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, …Run Code Online (Sandbox Code Playgroud)