- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过线程化来加速我的应用程序。它是一个在位图上放置鱼眼效果的应用程序。它运行良好,但运行速度非常慢。我已经记录了各种方法的一些处理时间,并将其精确定位到过滤器类中的 barrel() 方法。这个方法需要一个位图并循环通过它扭曲像素,这个循环需要大约 40 秒才能完成!我试过 AsyncTask 但这也需要 40 多秒。最后,我尝试了 View.post 机制,但这会产生相同的结果。有什么办法可以加快这个应用程序的速度吗?任何想法将不胜感激,谢谢马特。
.
public class TouchView extends View{
private File tempFile;
private byte[] imageArray;
private Bitmap bgr;
private Bitmap bm;
private Bitmap bgr2 = null;;
private Paint pTouch;
private int centreX = 1;
private int centreY = 1;
private int radius = 50;
private int Progress = 1;
private static final String TAG = "*********TouchView";
private Filters f = null;
private boolean AsyncRunning = false;
// private MyTask mt = null;
public TouchView(Context context) {
super(context);
// TouchView(context, null);
}
public TouchView(Context context, AttributeSet attr) {
super(context,attr);
........... code to get bitmap from camera shot.........
bgr2 = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
f = new Filters();
}// end of touchView constructor
public void findCirclePixels(){
new Thread(new Runnable() {
public void run() {
float prog = (float)Progress/150000;
final Bitmap bgr3 = f.barrel(bgr,prog);
TouchView.this.post(new Runnable() {
public void run() {
TouchView.this.bgr2 = bgr3;
TouchView.this.invalidate();
}
});
}
}).start();
}// end of changePixel()
public void initSlider(final HorizontalSlider slider)
{
// Log.e(TAG, "******setting up slider*********** ");
slider.setOnProgressChangeListener(changeListener);
}
private OnProgressChangeListener changeListener = new OnProgressChangeListener() {
@Override
public void onProgressChanged(View v, int progress) {
// TODO Auto-generated method stub
setProgress(progress);
//TouchView.this.Progress = progress;
}
};
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(bgr2, 0, 0, null);
}//end of onDraw
protected void setProgress(int progress2) {
//Log.e(TAG, "***********in SETPROGRESS");
this.Progress = progress2;
findCirclePixels();
invalidate();
}
}
.
import android.graphics.Bitmap;
import android.util.Log;
class Filters{
float xscale;
float yscale;
float xshift;
float yshift;
int [] s;
private String TAG = "Filters";
long getRadXStart = 0;
long getRadXEnd = 0;
long startSample = 0;
long endSample = 0;
public Filters(){
Log.e(TAG, "***********inside filter constructor");
}
public Bitmap barrel (Bitmap input, float k){
//Log.e(TAG, "***********INSIDE BARREL METHOD ");
float centerX=input.getWidth()/2; //center of distortion
float centerY=input.getHeight()/2;
int width = input.getWidth(); //image bounds
int height = input.getHeight();
Bitmap dst = Bitmap.createBitmap(width, height,input.getConfig() ); //output pic
// Log.e(TAG, "***********dst bitmap created ");
xshift = calc_shift(0,centerX-1,centerX,k);
float newcenterX = width-centerX;
float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,k);
yshift = calc_shift(0,centerY-1,centerY,k);
float newcenterY = height-centerY;
float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,k);
xscale = (width-xshift-xshift_2)/width;
// Log.e(TAG, "***********xscale ="+xscale);
yscale = (height-yshift-yshift_2)/height;
// Log.e(TAG, "***********yscale ="+yscale);
// Log.e(TAG, "***********filter.barrel() about to loop through bm");
/*for(int j=0;j<dst.getHeight();j++){
for(int i=0;i<dst.getWidth();i++){
float x = getRadialX((float)i,(float)j,centerX,centerY,k);
float y = getRadialY((float)i,(float)j,centerX,centerY,k);
sampleImage(input,x,y);
int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
// System.out.print(i+" "+j+" \\");
dst.setPixel(i, j, color);
}
}*/
int origPixel;
long startLoop = System.currentTimeMillis();
for(int j=0;j<dst.getHeight();j++){
for(int i=0;i<dst.getWidth();i++){
origPixel= input.getPixel(i,j);
getRadXStart = System.currentTimeMillis();
float x = getRadialX((float)j,(float)i,centerX,centerY,k);
getRadXEnd= System.currentTimeMillis();
float y = getRadialY((float)j,(float)i,centerX,centerY,k);
sampleImage(input,x,y);
int color = ((s[1]&0x0ff)<<16)|((s[2]&0x0ff)<<8)|(s[3]&0x0ff);
// System.out.print(i+" "+j+" \\");
if( Math.sqrt( Math.pow(i - centerX, 2) + ( Math.pow(j - centerY, 2) ) ) <= 150 ){
dst.setPixel(i, j, color);
}else{
dst.setPixel(i,j,origPixel);
}
}
}
long endLoop = System.currentTimeMillis();
long loopDuration = endLoop - startLoop;
long radXDuration = getRadXEnd - getRadXStart;
long sampleDur = endSample - startSample;
Log.e(TAG, "sample method took "+sampleDur+"ms");
Log.e(TAG, "getRadialX took "+radXDuration+"ms");
Log.e(TAG, "loop took "+loopDuration+"ms");
// Log.e(TAG, "***********filter.barrel() looped through bm about to return dst bm");
return dst;
}
void sampleImage(Bitmap arr, float idx0, float idx1)
{
startSample = System.currentTimeMillis();
s = new int [4];
if(idx0<0 || idx1<0 || idx0>(arr.getHeight()-1) || idx1>(arr.getWidth()-1)){
s[0]=0;
s[1]=0;
s[2]=0;
s[3]=0;
return;
}
float idx0_fl=(float) Math.floor(idx0);
float idx0_cl=(float) Math.ceil(idx0);
float idx1_fl=(float) Math.floor(idx1);
float idx1_cl=(float) Math.ceil(idx1);
int [] s1 = getARGB(arr,(int)idx0_fl,(int)idx1_fl);
int [] s2 = getARGB(arr,(int)idx0_fl,(int)idx1_cl);
int [] s3 = getARGB(arr,(int)idx0_cl,(int)idx1_cl);
int [] s4 = getARGB(arr,(int)idx0_cl,(int)idx1_fl);
float x = idx0 - idx0_fl;
float y = idx1 - idx1_fl;
s[0]= (int) (s1[0]*(1-x)*(1-y) + s2[0]*(1-x)*y + s3[0]*x*y + s4[0]*x*(1-y));
s[1]= (int) (s1[1]*(1-x)*(1-y) + s2[1]*(1-x)*y + s3[1]*x*y + s4[1]*x*(1-y));
s[2]= (int) (s1[2]*(1-x)*(1-y) + s2[2]*(1-x)*y + s3[2]*x*y + s4[2]*x*(1-y));
s[3]= (int) (s1[3]*(1-x)*(1-y) + s2[3]*(1-x)*y + s3[3]*x*y + s4[3]*x*(1-y));
endSample = System.currentTimeMillis();
}
int [] getARGB(Bitmap buf,int x, int y){
int rgb = buf.getPixel(y, x); // Returns by default ARGB.
int [] scalar = new int[4];
scalar[0] = (rgb >>> 24) & 0xFF;
scalar[1] = (rgb >>> 16) & 0xFF;
scalar[2] = (rgb >>> 8) & 0xFF;
scalar[3] = (rgb >>> 0) & 0xFF;
return scalar;
}
float getRadialX(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float getRadialY(float x,float y,float cx,float cy,float k){
x = (x*xscale+xshift);
y = (y*yscale+yshift);
float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy)));
return res;
}
float thresh = 1;
float calc_shift(float x1,float x2,float cx,float k){
float x3 = (float)(x1+(x2-x1)*0.5);
float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));
if(res1>-thresh && res1 < thresh)
return x1;
if(res3<0){
return calc_shift(x3,x2,cx,k);
}
else{
return calc_shift(x1,x3,cx,k);
}
}
}// end of filters class
最佳答案
float 学一般来说很糟糕,几乎在每台计算机上都是如此。然而,它在缺乏浮点协处理器支持的设备上确实很糟糕,例如一半的 Android 设备。所以,摆脱 float 学的每一点,例如:
您不需要通过浮点ceil()
和floor()
方法运行整数
您不需要计算 sqrt()
来与 150
进行比较,而不是跳过 sqrt()
并与22500
您不需要浮点pow()
来计算某物的平方;相反,使用称为“乘法”的高级数学运算
我怀疑半像素在你的计算中会很重要,所以你可能不需要 centerX
和 centerY
为 float
值
等等
在紧要关头,您可能需要使用诸如适用于 Android 的 Google map 插件之类的技巧。通常,您会想到十进制度数的纬度和经度,但这意味着大量的 float 学运算。 Google map 使用以微度(10^6 度)为单位的纬度和经度,以便能够以定点数学进行所有计算,同时保持合理的准确性。
是否所有这些都会使性能下降到您认为合理的水平,我不能说。但是,在开始提示 Android 代码的质量之前,您应该确保您的代码质量合理,就像您在上一条评论中所做的那样。
关于android - View.post 没有加快图像处理速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6397452/
我只是想知道需要什么样的计算/编程语言/框架来生成图像,例如 http://www.erdas.com/ 中的图像。 ? 以编程方式,如何生成一般空间分析图像? ps:我大部分时间都在使用java。
我尝试在我的 grails 项目(Mac OS X 上的 1.1.1)中使用一些图像处理插件或 java 库:imageTools 插件、imageJ、awt 库等。每次我从路径打开/获取图像以启动进
我有一个项目,我必须以多种方式处理图像。我陷入了像素化的困境。 对于像素化,我必须采用一组 10x10 像素并返回一个单独平均 RGB 颜色的单元格。目前我在运行程序中得到的只是一个红色图像。谢谢您的
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这是一项作业,因为我是Python编程新手,所以我付出了很大的努力: 我正在运行以下函数,它接受图像和短语(空格将被删除,因此只有文本)作为参数,我已经获得了所有导入和预处理代码,我只需要实现这个函数
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我需要一种简单易学且快速的方法来从背景图像、文本生成图像,然后保存为 JPEG 格式。 您有什么建议?有关于此的任何图书馆或教程吗?重要的标准是简单。 最佳答案 在 .Net 3.5/4 中,您还可以
我正在构建一个夜视应用程序,但我没有找到任何有用的算法可以应用于黑暗图像以使其清晰。任何人请给我一些好的算法。 提前致谢 最佳答案 由于 iphone 镜头和传感器的尺寸,无论您做什么,都会有很多噪音
所以我为游戏制作了这个程序,需要帮助让它更自动化一些。 程序接收图像然后显示它。我正在对 OpenGL 中的纹理执行此操作。当我截取游戏截图时,它通常约为 700x400。我将高度和宽度输入到我的程序
我想更改图像中像素的值,为此我需要将图像存储为矩阵。我怎样才能完成这项工作?请指导。 最佳答案 BufferedImage image = ImageIO.read(..); image.setRGB
概述: 我正在做一个视频创作项目。我使用的技术有:imageMagick、php、ffmpeg。 当前状态: 目前,该项目能够使用图像和文本以及很少的基本过渡来创建视频。我这样做的方式是使用 imag
我正在创建 facebook 应用程序,其中我将用户图像作为背景图像,并有一个用户可以四处移动的默认大写图像。用户将叠加图像(一顶帽子)放在正确的位置后,他点击保存 这就是我感到震惊的地方,我想知道如
我正在尝试编写一个 JavaScript 程序,通过在图像上放置三个垂直条纹来修改图像。左边三分之一是红色条纹,中间是绿色条纹,右边三分之一是蓝色条纹。 这是我试图实现的算法:1. 从您要更改的图像开
目前,我正在尝试通过图像分割方法将面部和头发修剪在一起,然后将所有非彩色像素设置为透明,然后尝试使用Binary Threshold技术和Adaptive Threshold。但是我得到了不希望的结果
我必须使此图像Book Image To Process的页面标题为:“单元3:主动学习的秘诀”,使其成为图像中的唯一页面 为此,我需要删除也在图像中的其他页面的一部分 我需要编写一个通用代码,可以对
我正在研究一个问题,其中我缩小图像的尺寸,在缩小图像中找到类似于二进制图像的有趣点。现在我只想放大在缩小图像中找到的有趣点(即白色像素点),而不是放大整个图像然后找到有趣的点。哪种技术可以最好地用于此
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 上个月关闭。 I
嗨,我需要编写一个程序,从灰度图像中删除分界(图像中带有文本) 我阅读了有关阈值和模糊的信息,但我仍然不知道该怎么做。 我的图像是这样的希伯来文本图像: 我需要删除分界线(假设分界线是图像中的最小元素
我正在做一个带有深度图像的项目。但是我的深度相机有噪音和像素读取失败的问题。有一些点和轮廓(尤其是边缘)的值为零。如何忽略这个零值并将其与周围的值混合? 我试过 dilation和 erosion (
我正在做一个大学项目,我需要在java中处理图像。前段时间我在数学实验室工作,这很容易,所以我想知道是否存在任何可以让我玩像素值、颜色(按像素)、RGB 模型、灰度图像等的 java 库。 最佳答案
我是一名优秀的程序员,十分优秀!