如何在cwac-cam2中打开相机时更改闪光灯设置?

时间:2016-12-12 15:11:26

标签: android android-camera commonsware-cwac cwac-camera

我使用cwac-cam2来创建相机应用程序。我无法在运行相机窗口时设置闪光模式,并且闪光灯始终打开。我也没有看到改变flashmode的按钮。难道我做错了什么?这是代码:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


private static final FlashMode[] FLASH_MODES={
        FlashMode.ALWAYS,
        FlashMode.AUTO,
        FlashMode.OFF
};


private static final int REQUEST_PORTRAIT_RFC=1337;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!Environment.MEDIA_MOUNTED
            .equals(Environment.getExternalStorageState())) {
        Toast
                .makeText(this, "Cannot access external storage!",
                        Toast.LENGTH_LONG)
                .show();
        finish();
    }

    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            capturePortraitFFC();

        }

    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


private void capturePortraitFFC() {

    Intent i;


        i=new CameraActivity.IntentBuilder(this)
                .skipConfirm()
                .facing(Facing.BACK)
                .facingExactMatch()
                .to(new File(getExternalFilesDir(null),  (new SimpleDateFormat("yyyyMMdd'-'HHmmss").format(new Date())).replaceAll(" ", "_") + ".jpg"))
                .updateMediaStore()
                .flashModes(FLASH_MODES)
                .zoomStyle(ZoomStyle.SEEKBAR)
                .debugSavePreviewFrame()
                .debug()
                .build();


    startActivityForResult(i, REQUEST_PORTRAIT_RFC);

}
}

我将不胜感激任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

  

我无法在运行相机窗口时设置闪光模式

正确。除了启动活动时提供的值(例如CameraActivity)之外,您无法控制该活动在前台时的行为。如果这对您很重要,您将需要使用其他一些库或直接使用相机API。

  

并且闪光灯始终处于

状态

这是您的代码所要求的,将ALWAYS作为您的最高优先级闪存模式。

  

我也没有看到改变flashmode的按钮

我还没有实现。请密切关注this issue