androidandroid-cameracommonsware-cwaccwac-camera

How to change flash setting while camera is open in cwac-cam2?


I ma using cwac-cam2 for creating a camera app. I am not able to set flash modes while running the camera window and the flash is on always. I also don't see a button for changing flashmode. Am i doing something wrong? Here is the code:

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

}
}

I will appreciate any help. Thanks.


Solution

  • I am not able to set flash modes while running the camera window

    Correct. Other than the values supplied when you start the activity (e.g., CameraActivity), you have no control over the behavior of that activity while it is in the foreground. If this is important to you, you will need to use some other library or work with the camera APIs directly.

    and the flash is on always

    That is what your code is asking for, by having ALWAYS as your top-priority flash mode.

    I also don't see a button for changing flashmode

    I have not implemented that yet. Please keep tabs on this issue.