2014年8月1日 星期五

android調用framework函數的用法。


最近剛好要用到android安裝apk所產生的permission granting view 如下


那其實這個view是在framework中產生的,我看光產這個view的class就四千多行,其中相關連的class更是不可能整個移植過來,上網google一下才發現(應該說是忘了)在一般app也能調用framework函數,只是你要確定安裝的device有著正規的framework,否則class會not found。

直接貼出我使用的function。這樣一來就能抓到這個view。

public static View getPermissionView(final Context context) {
    try {
    // android.widget.AppSecurityPermissions
    // AppSecurityPermissions asp = new AppSecurityPermissions(this,
    // packageName);
    // asp.getPermissionsView()
    // 原本在framework的使用方法
  
    // 使用reflection得到class
    Class clazz = Class.forName("android.widget.AppSecurityPermissions");
    Constructor[] constructors = clazz.getConstructors();
        for (Constructor c : constructors) {
        Class[] parameterTypes = c.getParameterTypes();
            for (Class cls : parameterTypes) {
                System.out.println(cls.getName());
        }
    }
    // loop找出constructor結構

    Constructor c = constructors[1];
    Object obj = c.newInstance(new Object[] { context, context.getPackageName() });
    Method method = clazz.getMethod("getPermissionsView");
    View view = (View) method.invoke(obj);
    // ll_list_permission.addView(view);

    ScrollView scrollview = new ScrollView(context);
    ScrollView.LayoutParams params = new ScrollView.LayoutParams(
        ScrollView.LayoutParams.MATCH_PARENT,
        ScrollView.LayoutParams.MATCH_PARENT);
    scrollview.setLayoutParams(params);

    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams paramsLinear = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
    linearLayout.setLayoutParams(paramsLinear);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.addView(view);
    scrollview.addView(linearLayout);

    return scrollview;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

使用的機制是java的refection,然後抓出constructor來建構這個class,而後invoke他的method,這文章對於reflection寫的很詳盡。

http://godleon.blogspot.tw/2007/09/class-class-java-class-class-jvm-class.html

最後就能傳出漂亮的permission view了。


2014年1月24日 星期五

android view draggable , rotatable , scaleable by android-gesture-detectors framework

nice work by this https://github.com/Almeros/android-gesture-detectors

Basically you just imply this framework and make some flexible modified to a ImageView . there is a sample of his project .

The change i did is made the ImageView to not match the parent to whole screen instead just wrap the content . the hardest part should be rotation . you have to modify the width and height to wrap the dynamic content . it took me some hours to figure out but turned out it was just junior high school math .. bummer .













2013年11月18日 星期一

zxing barcode qrcode in android 使用方法

1. 先下載https://code.google.com/p/zxing/

2. 解壓縮後要編core.jar 進入core中打mvn -DskipTests -Dgpg.skip=true install 基本上要作修改的要編整個core , 如果只想使用的可以用migration 

3. 在eclipse中import整個android project

4. properties -> java build path -> add external jar 將剛剛打包好的jar丟入即可

2013年10月24日 星期四

GLImageProcessing image effects in opengl

將圖檔存入opengl texture memory
首先要先看懂texture environment的用法 http://www.khronos.org/opengles/sdk/1.1/docs/man/glTexEnv.xml 




brightness
//告訴目前貼圖環境為GL_COMBINE
//告訴目前貼圖環境為GL_COMBINE
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
if (t > 1.0f)
{
//對於GL_COMBINE_RGB參數為GL_ADD 也就是相加
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_ADD);
//顏色ARRAY
glColor4f(t-1, t-1, t-1, t-1);
}
else
{
//對於GL_COMBINE_RGB參數為GL_SUBTRACT 也就是相減
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_SUBTRACT);
//again顏色ARRAY
glColor4f(1-t, 1-t, 1-t, 1-t);
}
//對於RGB的第一個resource為材質
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB,         GL_TEXTURE);
//對於RGB的第二個resource為顏色array
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB,         GL_PRIMARY_COLOR);
//對於Alpha的方式為GL_REPLACE
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,    GL_REPLACE);
//第一個resource為材質
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA,       GL_TEXTURE);

before drawarrays , order is not important .
基本上0.5 1.5左右就全黑全白了 以general color來說




constract


// One pass using two units:
// contrast < 1.0 interpolates towards grey
// contrast > 1.0 extrapolates away from grey
//
// Here, the general extrapolation 2*(Src*t + Dst*(0.5-t))
// can be simplified, because Dst is a constant (grey).
// That results in: 2*(Src*t + 0.25 - 0.5*t)
//
// Unit0 calculates Src*t
// Unit1 adds 0.25 - 0.5*t
// Since 0.5*t will be in [0..0.5], it can be biased up and the addition done in signed space.
此為extrapolate(外推)公式 取X_0 = 0 X_1 = 0.5 X_2 = t                        Y_0 = degen  Y_1 = src  便可算出Y_2

GLfloat h = t*0.5f;
告訴目前貼圖環境為GL_COMBINE
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
//GL_MODULATE  Arg0 × Arg1

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_MODULATE); 
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB,         GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB,         GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,    GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA,       GL_TEXTURE);
//使用第二層紋理 是個灰階圖
GLubyte half[4] = { 0x80, 0x80, 0x80, 0x80 };

glActiveTexture(GL_TEXTURE1);
//這裡只對目前actived texture做enable
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
//GL_ADD_SIGNED Arg0 + Arg1 - 0.5
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,      GL_ADD_SIGNED);
//C f represents the primary color of the incoming fragment, and C p represent the color computed from the previous texture stage or C f if processing texture stage 0
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB,         GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB,         GL_PRIMARY_COLOR);
//Arg1使用GL_SRC_ALPHA = A f
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,     GL_SRC_ALPHA);
//RGB x 2
glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE,        2);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,    GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA,       GL_PREVIOUS);
glColor4f(h, h, h, 0.75 - 0.5 * h); // 2x extrapolation


簡單來說就是達成上面算好的公式 第一層用GL_MODULATE 第二層用GL_ADD_SIGNED去混合這些紋理 第二層比較tricky

saturation

這特效使用一個灰階的texture當作基底來合成

首先要產生灰階的texture







saturation


GLfloat lerp[4] = { 1.0, 1.0, 1.0, 0.5 };

GLfloat avrg[4] = { .667, .667, .667, 0.5 }; // average
GLfloat prcp[4] = { .646, .794, .557, 0.5 }; // perceptual NTSC
    //NTSC, named for the National Television System Committee,[1] is the analog television system that is used in most of North America, parts of South America (except Brazil, Argentina, Uruguay, and French Guiana), Myanmar, South Korea, Taiwan, Japan, the Philippines, and some Pacific island nations and territories (see map).
 GLfloat dot3[4] = { prcp[0]*t+avrg[0]*(1-t), prcp[1]*t+avrg[1]*(1-t), prcp[2]*t+avrg[2]*(1-t), 0.5 };
 
 // One pass using two units:
 // Unit 0 scales and biases into [0.5..1.0]
 // Unit 1 dot products with perceptual weights

lerp是第一張texture的constant , avrg為灰階平均像素資料 , prcp是電視顏色基準

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); //Arg0 x Arg2 + Arg1 x (1-Arg2) glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); //Constant means Color of GL_TEXTURE_ENV_COLOR glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT); glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_CONSTANT); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE); glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR, lerp); // Note: we prefer to dot product with primary color, because // the constant color is stored in limited precision on MBX glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); //GL_DOT3_RGB or GL_DOT3_RGBA 4*((Arg0r - 0.5) * (Arg1r - 0.5) + (Arg0g - 0.5) * (Arg1g - 0.5) + (Arg0b - 0.5) * (Arg1b - 0.5)) glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB); //refers to the color computed from the previous texture stage (or the incoming fragment if processing texture stage 0), glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS); glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS); glColor4f(dot3[0], dot3[1], dot3[2], dot3[3]); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

第一張貼圖使用interpolate看似很威 實際上的constant是 1 1 1 0.5 也就是說除了alpha RGB還是使用bind的texture , 實際上alpha算出來也是1 , 而第二章用DOT3_RGB可以轉換成灰階 , alpha不變 也就能產出灰階圖片了 接著的外推 透過extrapolate算出後在使用 去作rgb scale

2013年10月17日 星期四

image effects

此篇未完

剛好找到一個直接對image作處理的project讀的一些心得

RGB處理

作色偏處理 

將RGB[0 , 255] 作 mapping 對應到 [ n, m ]  , n m  ∈    [0 , 255] . 即可達到色偏的效果 .

Gaussian Blur

將一個像素取周遭的像素來作平均 該矩陣由normal distribution 2 dimensional取得,邊緣失去的像素我assume是由中間去遞補, 事實上不會是一個很難的filter if you have the conception of all these stuffs .

Sepia Filter
Sepia toning is a specialized treatment to give a black-and-white photographic print a warmer tone and to enhance its archival qualities. Chemicals are used to convert the metallic silver in the print to a sulfidecompound, which is much more resistant to the effects of environmental pollutants such as atmospheric sulfur compounds. Silver sulfide is at least 50% more stable than silver.

基本上就是套用以下formula
outputRed = (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189)
outputGreen = (inputRed * .349) + (inputGreen *.686) + (inputBlue * .168)
outputBlue = (inputRed * .272) + (inputGreen *.534) + (inputBlue * .131)





2013年9月26日 星期四

background removal algogo !

關於這點是研究不完的,能用的演算法之多,符合怎樣的場景使用哪種等,我想這邊不該是講這些的地方,應該留給研究生去研究。
那麼主要講的是基本的圖形演算法的幾個東西 dilation erosion .
我用的演算法非常簡單,基本上就是設定個threshold ,讓顏色能夠透過threshold去作擴張。並且隨著距離作linear decay。
首先透過手在圖片上畫過的路徑取點,作分群演算法取出最大群組的顏色量,以那幾點當作基準點去作Dilation
Dilation
http://www.cs.princeton.edu/~pshilane/class/mosaic/images/dilation.png
不囉說直接看圖大概就知道Dilation在幹嘛,基本上我全用十字的參考去作dilate
http://ostermiller.org/dilate_and_erode.html
這網站對於Dilation有詳盡的implement可以參考。
但是對於程式判斷出來的區塊,用這麼簡易的threshold去作判別出來的區塊是非常殘破的,這時候才是Dilation and Erosion發揮的地方。
首先對自己判斷出來的區塊作fixed times Dilation,之後再對已存在的區塊作the same times Erosion
最後出現的區塊就會非常完整,Dilation and Erosion那步驟吃掉了破碎的像素
整個spirit就如同上述所敘的,很容易吧。。
。。實際上運作起來會死人的
首先假設一張很普通的圖,say 800x600好了,那實際上就有480000個像素,所需要的bitmask就要有480000個,不要太囂張用bitwise那還得在寫一些macro去實作,用最小的uint8好了,也會有480000bytes,那也要將近0.5mb的記憶體,這已經是很小張的圖片以及最保守的bitmask了。
再來就是計算點位的問題,假設你第一步的Dilation不要太多,兩百pixel就夠了,200pixel在800x600的圖來看只吃掉了1/4個距離,也就是說每一步你只能擴張1/4左右的圖片大小,所需要掃描的次數為800x600x200次...恩,我本來跑在simulator覺得沒什麼,還是一樣快,真正跑在ipad3才發現,跑完一次要五六秒。。這還只是average。。
直接講我目前版本的三個步驟final的作法好了
對於那些點存進兩個set container,輪流作reset,將第一次dilate的點存進另一個container,這樣不斷的來回作你想要dilate的次數,這樣可以避免每次都要掃600x800次,但這部份的速率感覺上並沒有比600x800實作良好的continue快許多,畢竟物件也是有效率的問題。
最後的dilation我取20好了,也就是作800x600x20次,速度還是在0.2秒內完成。
最慘的是erosion,最近才發現erosion做了兩秒多,並且這部份似乎好像沒有簡化的方法,直到目前我才想到一個好方法!就是在上述的public dilation中,取出最後一次dilation的點存進container,而直接用那些點去作dilate就夠了,直到這部份的完成,目前整個作一次消背景的動作average time可以壓在一秒左右,並且跑出的效果還ok

如此複雜背景的圖片,多花點步驟還是可以弄出不錯的成果。


我目前覺得值得做的further
1. 回復上一步
2. 手動畫筆清除
3. 手動畫筆復原
4. 自動手動切換
5. 全部重作
6. threshold的調整
7. dilate width的調整
甚至能想到的further應該都有辦法作,目前就先如此了。