Problem in building my own camera in android

后端 未结 1 981
萌比男神i
萌比男神i 2021-01-07 09:11

I have tried starting the camera in android using an intent, something like this:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE) 
1条回答
  •  被撕碎了的回忆
    2021-01-07 09:46

    Tour activity EditPhoto.java working perfectly in my API level 7 AVD,

    package x.y;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.graphics.BitmapFactory;
    import android.hardware.Camera;
    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    
    public class EditPhoto extends Activity implements SurfaceHolder.Callback, OnClickListener {
        static final int FOTO_MODE = 0;
        private static final String TAG = "CameraTest";
        Camera mCamera;
        boolean mPreviewRunning = false;
        private Context mContext = this;
    
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            Log.e(TAG, "onCreate");
    
            Bundle extras = getIntent().getExtras();
    
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.main);
            mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            mSurfaceView.setOnClickListener(this);
            mSurfaceHolder = mSurfaceView.getHolder();
            mSurfaceHolder.addCallback(this);
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
        }
    
        Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){
            public void onPictureTaken(byte[] imageData, Camera c) {
    
                if (imageData != null) {
    
                    Intent mIntent = new Intent();
    
                    StoreByteImage(mContext, imageData, 50, "ImageName");
                    mCamera.startPreview();
    
                    Bundle b = new Bundle();
                    b.putByteArray("imageData", imageData);
                    Intent i = new Intent(mContext, ImageDisplayActivity.class);
                    i.putExtras(b);
                    startActivity(i);
    
                    setResult(FOTO_MODE, mIntent);
                    finish();
    
                }
            }
        };
    
        protected void onResume() {
            Log.e(TAG, "onResume");
            super.onResume();
        }
    
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        }
    
        protected void onStop() {
            Log.e(TAG, "onStop");
            super.onStop();
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
            Log.e(TAG, "surfaceCreated");
            mCamera = Camera.open();
    
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Log.e(TAG, "surfaceChanged");
    
            // XXX stopPreview() will crash if preview is not running
            if (mPreviewRunning) {
                mCamera.stopPreview();
            }
    
            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(w, h);
            mCamera.setParameters(p);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.e(TAG, "surfaceDestroyed");
            mCamera.stopPreview();
            mPreviewRunning = false;
            mCamera.release();
        }
    
        private SurfaceView mSurfaceView;
        private SurfaceHolder mSurfaceHolder;
    
        public void onClick(View arg0) {
    
            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    
        }
    
        public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {
    
            File sdImageMainDirectory = new File("/sdcard");
            FileOutputStream fileOutputStream = null;
            String nameFile;
            try {
    
                BitmapFactory.Options options=new BitmapFactory.Options();
                options.inSampleSize = 5;
                Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
                fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/image.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    
                myImage.compress(CompressFormat.JPEG, quality, bos);
    
                bos.flush();
                bos.close();
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return true;
        }
    
    }
    

    i am posting my Layout and Manifest here ...

    Layout editphoto.xml

    
    
    
    
    

    and Manifest file ...

    
    
        
            
                
                    
                    
                
            
    
        
    
    
    
    
    
     
    

    New Activity to display image ImageDisplayActivity.java (Image will be displayed when you will click on surface view)....

    package x.y;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class ImageDisplayActivity extends Activity {
    
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            Bundle extras = getIntent().getExtras();
            setContentView(R.layout.anotherlayout);
    
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;
            byte[] imageData = extras.getByteArray("imageData");
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);
    
    
            ImageView imageView = (ImageView) findViewById(R.id.myPic);
            imageView.setImageBitmap(myImage);
        }   
    }
    

    and it's layout otherlayout.xml ...

    
    
    
    
    

    0 讨论(0)
提交回复
热议问题