View.post不能加快图像处理速度吗?
|
我正在尝试通过对应用程序进行线程化来加快速度。它是一款将鱼眼效果置于位图上的应用程序。它运行良好,但运行缓慢。我已经注销了各种方法的一些处理时间,并已将其精确定位到过滤器类中的bucket()方法。此方法需要一个位图并循环遍历它,从而使像素失真,此循环大约需要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
没有找到相关结果
已邀请:
1 个回复
妊辽剁茧
和
方法运行整数 您无需计算
即可与
进行比较,而无需跳过
并与
进行比较 您不需要浮点数8来计算事物的平方;而是使用称为“乘法”的高级数学运算 我怀疑半像素在您的计算中是否会很重要,因此您可能不需要
和
作为
值 等等 一时之间,您可能需要使用一些技巧,例如适用于Android的Google Maps插件。通常,您会想到以十进制表示的纬度和经度,但这意味着很多浮点数学运算。 Google Maps使用微度(10 ^ 6倍)的经度和纬度,以便能够以定点数学进行所有计算,同时保持合理的准确性。 我不能说这是否会使性能降低到您认为合理的程度。但是,在开始抱怨Android代码的质量之前,您应该确保您的代码质量合理,就像在上一次评论中所做的那样。