Android位图中getPixel和getPixel的更快替代方案?
我正在使用Android位图中getPixel和getPixel的更快替代方案?,android,performance,android-ndk,android-bitmap,renderscript,Android,Performance,Android Ndk,Android Bitmap,Renderscript,我正在使用getPixel和setPixel方法来制作我的Bitmap,速度太慢了(getPixels)。我想处理位图的每个像素,然后创建另一个位图。如何通过RealScript或使用C++访问像素?我认为他们更快,但我不知道怎么做 这就是我使用getPixel/setPixel所做的: bitmap.getPixels(colorArray, 0, width, 0, 0, width, height); for (int x = 0; x < width; x++) {
getPixel
和setPixel
方法来制作我的Bitmap
,速度太慢了(getPixels
)。我想处理位图的每个像素
,然后创建另一个位图
。如何通过RealScript或使用C++访问像素?我认为他们更快,但我不知道怎么做
这就是我使用getPixel
/setPixel
所做的:
bitmap.getPixels(colorArray, 0, width, 0, 0, width, height);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int g = Color.green(colorArray[y * width + x]);
int b = Color.blue(colorArray[y * width + x]);
int r = Color.red(colorArray[y * width + x]);
//some color changes ....
colorArray[y * width + x] = Color.rgb(r, g, b);
returnBitmap.setPixel(x, y, colorArray[y * width + x]);
}
}
bitmap.getPixels(colorArray,0,width,0,0,width,height);
对于(int x=0;x
谢谢 使用getPixels()
获取所有像素,修改字节[]
中的值,然后调用setPixels()
一次存储所有像素。在每个像素上调用setPixel()
的开销会降低性能
如果仍然不够快,可以将数组传递给NDK函数
如果您正在对相机图像进行实时处理,您可以获得。我找到了这个ColorMatrix示例
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.graphics;
import com.example.android.apis.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
public class ColorMatrixSample extends GraphicsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ColorMatrix mCM = new ColorMatrix();
private Bitmap mBitmap;
private float mSaturation;
private float mAngle;
public SampleView(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.balloons);
}
private static void setTranslate(ColorMatrix cm, float dr, float dg,
float db, float da) {
cm.set(new float[] {
2, 0, 0, 0, dr,
0, 2, 0, 0, dg,
0, 0, 2, 0, db,
0, 0, 0, 1, da });
}
private static void setContrast(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0 });
}
private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
1, 0, 0, 0, translate,
0, 1, 0, 0, translate,
0, 0, 1, 0, translate,
0, 0, 0, 1, 0 });
}
private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
scale, 0, 0, 0, 0,
0, scale, 0, 0, 0,
0, 0, scale, 0, 0,
0, 0, 0, 1, 0 });
}
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
float x = 20;
float y = 20;
canvas.drawColor(Color.WHITE);
paint.setColorFilter(null);
canvas.drawBitmap(mBitmap, x, y, paint);
ColorMatrix cm = new ColorMatrix();
mAngle += 2;
if (mAngle > 180) {
mAngle = 0;
}
//convert our animated angle [-180...180] to a contrast value of [-1..1]
float contrast = mAngle / 180.f;
setContrast(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
setContrastScaleOnly(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
setContrastTranslateOnly(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10),
paint);
invalidate();
}
}
}
取自
还有人在这里有一个很好的答案
您正在执行什么样的流程?你在旋转、缩放、改变颜色吗?这个过程是否需要以逐像素的方式应用?我更新了这个问题,我想更改颜色对于您的颜色更改,您最好使用颜色矩阵,而不是逐像素的双循环。颜色矩阵比每像素(双)循环快得多。@DerGolem如果您有时间整理示例代码,你应该把它写下来作为答案——我认为这是正确的方法。对不起,现在不行(我马上要完成一项工作)。我想在OP中留下了一些需要研究的东西:一个关键字,他们可以搜索并发现一个ColorMatrix可以允许多少事情(乌贼色调、灰度、色彩色调、负片等等)。以曲速前进@法登:谢谢你的回答。我正在尝试函数setPixels,但我不知道它是否足够。。如何使用NDK函数?(一些代码)DerGolem我已经使用ColorMatrix来表示饱和度、亮度等。但在这种情况下,我只需要从3个不同的位图中引入RGB,并将其高效地放入一个新的位图(如Harris效应)hi@DerGolem,现在我可以使用ColorMatrix从位图中提取RGB。但是如何覆盖这3个图像呢?在这个过程中,我需要将RGB矩阵放在每个像素中,并显示RGB不构成相同内容的像素中的颜色差异。我不知道它是否干净到可以理解。。你们可以看到搜索“哈里斯效应”,ColorMatrix真的很快,谢谢!现在我需要如何添加不同的频道。