Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.cameraapp;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.hardware.Camera.PictureCallback;
- import android.hardware.Camera.ShutterCallback;
- import android.os.Bundle;
- import android.provider.MediaStore.Images;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.SurfaceHolder;
- import android.view.SurfaceHolder.Callback;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.Toast;
- public class CaMERAapp extends Activity implements SurfaceHolder.Callback {
- private static final String TAG = "cookbook.hardware";
- private LayoutInflater mInflater = null;
- Camera mCamera;
- byte[] tempdata;
- boolean mPreviewRunning = false;
- private SurfaceHolder mSurfaceHolder;
- private SurfaceView mSurfaceView;
- Button takepicture;
- protected ShutterCallback mShutterCallback;
- protected PictureCallback mPictureCallback;
- protected PictureCallback mjpeg;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_ca_meraapp);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.activity_ca_meraapp);
- //callt();
- /* mSurfaceView = (SurfaceView)findViewById(R.id.surface);
- mSurfaceHolder = mSurfaceView.getHolder();
- mSurfaceHolder.addCallback(this);
- mSurfaceHolder.setType(0);
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- */
- /* mInflater = LayoutInflater.from(this);
- View overView = mInflater.inflate(R.layout.activity_ca_meraapp, null);
- this.addContentView(overView,
- new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
- */
- takepicture = (Button) findViewById(R.id.button);
- takepicture.setOnClickListener(new OnClickListener(){
- public void onClick(View view){
- callt();
- mCamera.takePicture(mShutterCallback,mPictureCallback, mjpeg);
- }
- });
- }
- public void callt()
- {
- Toast.makeText(this,"Cpatured", Toast.LENGTH_LONG).show();
- }
- ShutterCallback mShutterCallback1 = new ShutterCallback(){
- @Override
- public void onShutter() {}
- };
- PictureCallback mPictureCallback1 = new PictureCallback() {
- public void onPictureTaken(byte[] data, Camera c) {} };
- PictureCallback mjpeg1 = new PictureCallback() {
- public void onPictureTaken(byte[] data, Camera c) {
- if(data !=null) {
- tempdata=data;
- done();
- }
- }
- };
- void done() {
- Bitmap bm = BitmapFactory.decodeByteArray(tempdata,
- 0, tempdata.length);
- String url = Images.Media.insertImage(getContentResolver(),
- bm, null, null);
- bm.recycle();
- Bundle bundle = new Bundle();
- if(url!=null) {
- bundle.putString("url", url);
- Intent mIntent = new Intent();
- mIntent.putExtras(bundle);
- setResult(RESULT_OK, mIntent);
- } else {
- Toast.makeText(this, "Picture can not be saved",
- Toast.LENGTH_SHORT).show();
- }
- finish();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_ca_meraapp, menu);
- return true;
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int w,int h) {
- // TODO Auto-generated method stub
- Log.e(TAG, "surfaceChanged");
- try {
- if (mPreviewRunning) {
- mCamera.stopPreview();
- mPreviewRunning = false;
- }
- Camera.Parameters p = mCamera.getParameters();
- p.setPreviewSize(w, h);
- mCamera.setParameters(p);
- mCamera.setPreviewDisplay(holder);
- mCamera.startPreview();
- mPreviewRunning = true;
- } catch(Exception e) {
- Log.d("",e.toString());
- }
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- Log.e(TAG, "surfaceCreated");
- mCamera = Camera.open();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- Log.e(TAG, "surfaceDestroyed");
- mCamera.stopPreview();
- mPreviewRunning = false;
- mCamera.release();
- mCamera=null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment