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

<?php

        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"));
        $group->showAll();
        ?>

output:

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 
lnmcc 
wangsijie

Resolve Conflicts

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

        <?php
        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();
        $test->run();
        $test->Command1Run();
        ?>

output:

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();

        display.getRealMetrics(realSize);
        display.getMetrics(windowSize);

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

getRealMetrics()getRealSize()的功能几乎一样。

see also: get physical screen size

PHP 中 extract 同名覆盖

extract()方法会根据一个Array创建相应的变量,并且会覆盖同名变量的值。幸运的是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.

所以解决同名覆盖的一个方法就是使用EXTRA_PREFEX_ALL标志,这样extract()在创建Array对应变量的时候会自动加上第三个参数设置的前缀字符串。eg:

        $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

为什么要分这些文件夹呢?

为了能够自动适配不同的屏幕,使他们看上去大小(基本)一致。

我们可以使用一张分辨率为500×500的图片(test.jpg)做一些测试。

测试代码片段如下:

public class MainActivity extends Activity {

	TextView tv;
	ImageView img;
	Drawable drawable;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		img = (ImageView)findViewById(R.id.img);
		tv = (TextView)findViewById(R.id.tv);
		drawable = getResources().getDrawable(R.drawable.test);
		String text = "IntrinsicHeight: " + drawable.getIntrinsicHeight() + " IntrinsicWidth: " + drawable.getIntrinsicWidth();
		tv.setText(text);
		img.setImageDrawable(drawable);
	}
}

先把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)