VirtualBox 使用摄像头

OS: OSX 10.10.1

VirtualBOX: 4.3.20

近期在OSX上使用VirtualBox安装了一个Win7用来测试一个摄像头程序,但是发现VirtualBox本身是不支持使用HOST机的摄像头设备的,需要VirtualBox Extension Pack的支持才可以。 可以在这里下载对应ViretualBox版本的ExtensionPack,点击安装成功后,可以看到如下界面: Alt none

进入系统后即可在Devices -> Webcams中找到你的视频设备

PHP Trait & Resolve Conflict


        trait Logger {

            public function log($logString) {
                $className = __CLASS__;
                echo date("Y-m-d:h:i:s", time()) . ": [{$className}] {$logString} <br />";


        class User {

            use Logger;

            function __construct($name = '') {
                $this->name = $name;
                $this->log("Create user '{$this->name}'");

            function __toString() {
                return $this->name;


        Class UserGroup {

            use Logger;

            private $users = array();

            public function addUser(User $user) {
                if(!$this->includesUser($user)) {
                    $this->users[] = $user;
                    $this->log("Added user '{$user}' to group");

            private function includesUser(User $user) {
                return array_search($user, $this->users, true);
            public function showAll() {
                foreach($this->users as $name) {
                    echo "{$name} <br />";
        $group = new UserGroup;
        $group->addUser(new User("lnmcc"));
        $group->addUser(new User("wangsijie"));


2014-11-29:04:49:23: [User] Create user 'lnmcc' 
2014-11-29:04:49:23: [UserGroup] Added user 'lnmcc' to group 
2014-11-29:04:49:23: [User] Create user 'wangsijie' 
2014-11-29:04:49:23: [UserGroup] Added user 'wangsijie' to group 

Resolve Conflicts

Use the as keyword to alias a trait’s method name.

        trait Command1 {
            function run() {
                echo "executing command1 ... <br />";
        trait Command2 {
            function run() {
                echo "executing command2 ... <br />";
        class TestCommand {
            use Command1, Command2 {
                Command1::run as Command1Run;
                Command2::run insteadof Command1;
        $test = new TestCommand();


executing command2 ... 
executing command1 ... 

Android 中一种判断 Navigation Bar 位置的方法

  public boolean isNavigationBottom() {
        WindowManager wm;
        Display display;
        DisplayMetrics realSize;
        DisplayMetrics windowSize;

        realSize = new DisplayMetrics();
        windowSize = new DisplayMetrics();
        wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        display = wm.getDefaultDisplay();


        if (windowSize.heightPixels != realSize.heightPixels)
            return true;
            return false;


see also: get physical screen size

PHP 中 extract 同名覆盖


EXTR_OVERWRITE If there is a collision, overwrite the existing variable.

EXTR_SKIP If there is a collision, don’t overwrite the existing variable.

EXTR_PREFIX_SAME If there is a collision, prefix the variable name with prefix.

EXTR_PREFIX_ALL Prefix all variable names with prefix.

EXTR_PREFIX_INVALID Only prefix invalid/numeric variable names with prefix.

EXTR_IF_EXISTS Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example.

EXTR_PREFIX_IF_EXISTS Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.

EXTR_REFS Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of the array parameter. You can use this flag on its own or combine it with any other flag by OR’ing the flags.

If flags is not specified, it is assumed to be EXTR_OVERWRITE.


        $name = "wangsijie";
        $person = array('name' => "lnmcc", 'age' => 100);
        extract($person, EXTR_PREFIX_ALL, "person");
        echo "Name: {$name}, Person Name: {$person_name}, Person Age: {$person_age}";  

关于图片 Drawable 的宽高

在Android项目的res目录下有一些对应不同屏幕DPI的子文件夹: Alt none 分别对应的DPI为:

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi





public class MainActivity extends Activity {

	TextView tv;
	ImageView img;
	Drawable drawable;
	protected void onCreate(Bundle savedInstanceState) {
		img = (ImageView)findViewById(;
		tv = (TextView)findViewById(;
		drawable = getResources().getDrawable(R.drawable.test);
		String text = "IntrinsicHeight: " + drawable.getIntrinsicHeight() + " IntrinsicWidth: " + drawable.getIntrinsicWidth();

先把test.jpg拷贝到xxhdpi下,在Nexus 5 (480DPI)下显示的IntrinsicHeight和IntrinsicWidth为500×500; 而在Nexus 7 (320DPI)下显示的IntrinsicHeight和IntrinsicWidth为333×333 。

接着我们把test.jpg换到xhdpi下,在Nexus 5(480DPI)下显示的IntrinsicHeight和IntrinsicWidth为750×750; 而在Nexus 7 (320DPI)下显示的IntrinsicHeight和IntrinsicWidth为500×500 。


答案是跟你设备的DPI有关。Android为了显示效果不依赖设备像素(pix),特别捣鼓出了一个设备独立像素(dp)。具体可参考Supporting Multiple Screens


Drawable的宽高 = 实际图片的宽高 * (设备的DPI / 文件夹对应的DPI)