Android Open Source - Smartlab Main Activity






From Project

Back to project page Smartlab.

License

The source code is released under:

Apache License

If you think the Android project Smartlab listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ir.smartlab.camera;
/*from w  ww.j  a va 2 s  .  c o m*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import com.example.cameraapplication.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MainActivity extends Activity implements SurfaceHolder.Callback,
    Camera.PictureCallback {

  SurfaceView surfaceView;
  SurfaceHolder surfaceHolder;
  Camera camera;
  List<Size> supportedSizes;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.addCallback(this);
  }

  public void captureImageOnClick(View v) {
    if (camera != null) {
      camera.takePicture(null, null, null, this);
    }
  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    try {
      camera = Camera.open();

      Camera.Parameters parameters = camera.getParameters();

      if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        parameters.set("orientation", "portrait");
        // For Android 2.2 and above
        camera.setDisplayOrientation(90);
        // Uncomment for Android 2.0 and above
         parameters.setRotation(90);
      } else {
        // This is an undocumented although widely known feature
        parameters.set("orientation", "landscape");
        // For Android 2.2 and above
        camera.setDisplayOrientation(0);
        // Uncomment for Android 2.0 and above
         parameters.setRotation(0);
      }

      // supportedSizes = parameters.getSupportedPictureSizes();
      // for( Size size : supportedSizes) {
      // Log.i("camera", "Width:" + size.width + " Height:" +
      // size.height);
      // }
      // parameters.setPictureSize(supportedSizes.get(supportedSizes.size()-1).width,
      // supportedSizes.get(supportedSizes.size()-1).height);

      parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

      parameters.setSceneMode(Camera.Parameters.SCENE_MODE_ACTION);

      List<String> colorEffects = parameters.getSupportedColorEffects();
      if (colorEffects != null
          && colorEffects.contains(Camera.Parameters.EFFECT_MONO)) {
        parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
      }

      camera.setParameters(parameters);
      camera.setPreviewDisplay(holder);
      camera.startPreview();
    } catch (Exception e) {
      camera.release();
      e.printStackTrace();
    }
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
  }

  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
    Uri imageFileUri = getContentResolver().insert(
        Media.EXTERNAL_CONTENT_URI, new ContentValues());
    try {
      OutputStream imageFileOS = getContentResolver().openOutputStream(
          imageFileUri);
      imageFileOS.write(data);
      imageFileOS.flush();
      imageFileOS.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    camera.startPreview();
  }
}




Java Source Code List

com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.SecondActivity.java
com.example.analyticstest.MainActivity.java
com.example.androidgraphic.MainActivity.java
com.example.animationexample.MainActivity.java
com.example.animationexample.SecondActivity.java
com.example.compassview.CompassView.java
com.example.compassview.MainActivity.java
com.example.databasetest.BookListActivity.java
com.example.databasetest.MainActivity.java
com.example.databasetest.data.Book.java
com.example.databasetest.data.DatabaseAdapter.java
com.example.fragmentexample.BookDetailsFragment.java
com.example.fragmentexample.BookListFragment.java
com.example.fragmentexample.MainActivity.java
com.example.fragmentexample.data.BookListAdapter.java
com.example.fragmentexample.data.Book.java
com.example.fragmentexample.data.DatabaseAdapter.java
com.example.listviewexample.MainActivity.java
com.example.listviewexample.data.BookListAdapter.java
com.example.listviewexample.data.Book.java
com.example.listviewexample.data.DatabaseAdapter.java
com.example.persiandatepicker.MainActivity.java
com.example.persiandatepicker.PersianDatePicker.java
com.example.preferencesexample.MainActivity.java
com.example.sqlitetestapp.MainActivity.java
com.example.sqlitetestapp.PersonListActivity.java
com.example.sqlitetestapp.data.DatabaseAdapter.java
com.example.sqlitetestapp.data.PersonListAdapter.java
com.example.sqlitetestapp.data.model.Person.java
com.example.viewpagerexample.MainActivity.java
ir.smartlab.android.broadcast.MainActivity.java
ir.smartlab.android.broadcast.WifiStateChangeReceiver.java
ir.smartlab.android.downloadasynctask.MainActivity.java
ir.smartlab.android.helloworld.MainActivity.java
ir.smartlab.audiorecord.MainActivity.java
ir.smartlab.audiotest.MainActivity.java
ir.smartlab.camera.MainActivity.java
ir.smartlab.captureimage.MainActivity.java
ir.smartlab.customaudiorecorder.MainActivity.java
ir.smartlab.java.ch04.firstcup.FisrtCup.java
ir.smartlab.java.ch05.circlesample.Circle.java
ir.smartlab.java.ch05.circlesample.Main.java
ir.smartlab.java.ch06.simplecalculator.SimpleCalculator.java
ir.smartlab.java.ch06.variablescope.VariableScopeTest.java
ir.smartlab.java.persiancalendar.Main.java
ir.smartlab.musicplayer.MainActivity.java
ir.smartlab.musicplayerservice.MainActivity.java
ir.smartlab.musicplayerservice.PlayerService.java
ir.smartlab.personlist.MainActivity.java
ir.smartlab.personlist.PersonListActivity.java
ir.smartlab.personlist.data.DatabaseAdapter.java
ir.smartlab.personlist.data.PersonListAdapter.java
ir.smartlab.personlist.data.Person.java