在不使用意图的情况下捕获图像

时间:2014-04-22 14:05:14

标签: android android-intent parameters surfaceview

我想在触摸时捕捉图像。我正在使用表面视图,我已经应用了动作和动作向下参数。现在我想拍照而不使用任何意图。我已经制作了相机对象并尝试调用拍照或拍照参数,但这些参数不存在。有人可以帮我解决这个问题吗?

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
surfaceView.setOnTouchListener(new View.OnTouchListener() {

          public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
              Log.d("TouchTest", "Touch down");
            } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                Intent i =
                         new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                if (hasImageCaptureBug()) {
                    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(new File("/sdcard/tmp")));
                } else {
                    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,  
                       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                }
                startActivityForResult(i,0);
              Log.d("TouchTest", "Touch up");
            }
            return true;
          }

1 个答案:

答案 0 :(得分:0)

public class CameraLibrary extends Activity {

    private MyPreview mPreview;
    private final String LOG_TAG = "Camera Lib : CameraLibrary";
    private Camera.Parameters cam_params;
    private CameraOptions camOptions;
    private String Flashmode = "auto";
    private int Flashmodevalue = 0;
    private int zoomMode = 0;
    LinearLayout parentPreview;
    private ImageView imgCapture;
    private ImageView imgFlash;
    private ImageView switchCam;
    public static Bitmap bmp;
    public static Bitmap bitmap;
    String TEMP_PHOTO_FILE = "temporary_holder.jpg"; 
    int REQ_FILE_CODE=3123;

    Vibrator v1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try 
        {
            mPreview = new MyPreview(this);
            bmp=null;
            v1=(Vibrator)getSystemService(VIBRATOR_SERVICE);
            camOptions = CameraOptions.getInstance(this);
            if (camOptions.getActionClick()== 0 || camOptions.getActionClick() >3)
                throw new RuntimeException("Camera Options is not defined");
            CreateMyView();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void CreateMyView(){

          try {
              setContentView(R.layout.camera_preview);
              parentPreview=(LinearLayout)findViewById(R.id.preview);
              imgCapture=(ImageView)findViewById(R.id.imageView1);
              parentPreview.addView(mPreview);
              [YOUR_SURFACE_VIEW_BUT_FIRST_TRY_WITH_A_BUTTON_INSTEAD].setOnClickListener( new OnClickListener() {
                @Override
                public void onClick(View v) {

                    v1.vibrate(35);
                    cam_params = mPreview.mCamera.getParameters();
                    switch(camOptions.getActionClick()){
                    case 1:
                        try{
                            mPreview.mCamera.takePicture(camOptions.getShutterCallback(),
                                    camOptions.getRaw_PictureCallback(), 
                                    camOptions.getJPEG_PictureCallback());
                        }catch (Exception e) {
                            e.printStackTrace();
                            throw new RuntimeException("Take Picture: " +e.getMessage());
                        }
                        break;

                    case 2:
                        try{
                            mPreview.mCamera.takePicture(camOptions.getShutterCallback(),
                                    camOptions.getRaw_PictureCallback(), 
                                    camOptions.getJPEG_PictureCallback());
                        }catch (Exception e) {
                            e.printStackTrace();
                            throw new RuntimeException("Take Picture: " +e.getMessage());
                        }
                        break;

                    case 3:
                        try{
                            mPreview.mCamera.takePicture(camOptions.getShutterCallback(),
                                    camOptions.getRaw_PictureCallback(), 
                                    camOptions.getPostView_PictureCallback(),
                                    camOptions.getJPEG_PictureCallback());
                        }catch (Exception e) {
                            e.printStackTrace();
                            throw new RuntimeException("Take Picture: " +e.getMessage());
                        }
                        break;

                    default:
                        throw new RuntimeException("Take Picture Undefined");

                    }
                }
            });





        } catch (Exception e) {
            e.printStackTrace();
        }



    }




       @Override
        protected void onDestroy() {
            super.onDestroy();

            try 
            {
                mPreview.mCamera.stopPreview(); 
                mPreview.mCamera.setPreviewCallback(null);
                mPreview.mCamera.release();
                mPreview.mCamera = null;
            } catch (Exception e) {}
        }




}

我的代码中编辑过的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:visibility="visible">

        <LinearLayout
            android:id="@+id/preview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:visibility="visible" >
        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@id/menurl"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="#4ccc"
        android:padding="3dp"
        android:orientation="vertical"
        android:visibility="visible" >

         <ImageView
             android:id="@+id/imageView1"
             android:layout_width="45dp"
             android:layout_height="45dp"
             android:scaleType="fitXY"
             android:layout_alignParentLeft="true"
             android:layout_centerVertical="true"
             android:src="@drawable/camerabutton" />
    </RelativeLayout>
</RelativeLayout>

public class CameraOptions {
    private int ActionClick = 0;
    private static Activity activity;
    private int ResultCode=1;

    BitmapFactory.Options bitmapOptions;

    private static Context context;
    private final String LOG_TAG = "Camera Lib : CameraOptions";
    private static CameraOptions options; 
    public String PicPath;
    private ShutterCallback mShutterCallback;
    private PictureCallback mRaw_PictureCallback,
                            mPostView_PictureCallback,
                            mJPEG_PictureCallback;

    private CameraOptions(){

    }

    public static CameraOptions getInstance(Activity _this){
        context = _this.getApplicationContext();
        activity = _this;

        if (options == null)
            options = new CameraOptions();
        return options;
    }

    //Getters
    public ShutterCallback getShutterCallback(){
        return this.mShutterCallback;
    }
    public PictureCallback getRaw_PictureCallback(){
        return this.mRaw_PictureCallback;
    }
    public PictureCallback getPostView_PictureCallback(){
        return this.mPostView_PictureCallback;
    }
    public PictureCallback getJPEG_PictureCallback(){
        return this.mJPEG_PictureCallback;
    }

    //Setters
    public void setShutterCallback(ShutterCallback shutterCallback){
        this.mShutterCallback = shutterCallback;
    }
    public void setRaw_PictureCallback(PictureCallback Raw_PictureCallback){
        this.mRaw_PictureCallback = Raw_PictureCallback;
    }
    public void setPostView_PictureCallback(PictureCallback PostView_PictureCallback){
        this.mPostView_PictureCallback = PostView_PictureCallback;
    }
    public void setJPEG_PictureCallback(PictureCallback JPEG_PictureCallback){
        this.mRaw_PictureCallback = JPEG_PictureCallback;
    }

    public void takePicture(){

        ShutterCallback shutter = new ShutterCallback(){public void onShutter(){}};
        PictureCallback raw_pic = new PictureCallback(){public void onPictureTaken(byte[] data, Camera camera) {}};
        PictureCallback postview_pic = new PictureCallback(){public void onPictureTaken(byte[] data, Camera camera) {}};
        PictureCallback jpeg_pic = new PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream fos ;
                try 
                {
                    File f = new File(context.getCacheDir() + "/temp.jpg");
                    fos = new FileOutputStream(f);
                    fos.write(data);
                    fos.close();
                    bitmapOptions=new BitmapFactory.Options();
                    bitmapOptions.inJustDecodeBounds = false;
                    bitmapOptions.inPreferredConfig = Config.RGB_565;
                    bitmapOptions.inDither = true;

                    CameraLibrary.bmp=null;
                    CameraLibrary.bmp=BitmapFactory.decodeFile(context.getCacheDir() + "/temp.jpg",bitmapOptions);

                    try 
                    {
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    int nh = (int) ( CameraLibrary.bmp.getHeight() * (512.0 / CameraLibrary.bmp.getWidth()) );
                    CameraLibrary.bmp = Bitmap.createScaledBitmap(CameraLibrary.bmp, 512, nh, true);
                    PicPath = f.getAbsolutePath();

                    if(CameraLibrary.bmp!=null)
                    {
                        Intent i = new Intent(activity,AnimateBmp.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        activity.startActivity(i);
                        activity.overridePendingTransition(R.anim.fade_in_act, R.anim.fade_out_act);
                    }
                    else
                    {
                        activity.finish();
                        activity.overridePendingTransition(R.anim.fade_in_act, R.anim.fade_out_act);
                    }
                    f.delete();

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        };

        this.mShutterCallback = shutter;
        this.mRaw_PictureCallback = raw_pic;
        this.mPostView_PictureCallback = postview_pic;
        this.mJPEG_PictureCallback = jpeg_pic;
        ActionClick = 1;
    }

    public void takePicture(ShutterCallback shutterCallback ,
                            PictureCallback Raw_PictureCallback,
                            PictureCallback JPEG_PictureCallback){
        this.mShutterCallback = shutterCallback;
        this.mRaw_PictureCallback = Raw_PictureCallback;
        this.mJPEG_PictureCallback = JPEG_PictureCallback;
        ActionClick = 2;
    }

    public void takePicture(ShutterCallback shutterCallback ,
            PictureCallback Raw_PictureCallback,
            PictureCallback postView_PictureCallback,
            PictureCallback JPEG_PictureCallback){
        this.mShutterCallback = shutterCallback;
        this.mRaw_PictureCallback = Raw_PictureCallback;
        this.mPostView_PictureCallback = postView_PictureCallback;
        this.mJPEG_PictureCallback = JPEG_PictureCallback;
        ActionClick = 3;
    }

    public int getActionClick(){
        return ActionClick;
    }

    public int getRequesCode(){
        return ResultCode;
    }

    public void setRequesCode(int Request_Code){
        ResultCode = Request_Code;
    } 




    public String getFilePath(){
        return PicPath;
    }

    public void setFilePath(String path){
        this.PicPath= path;
    }

}

public class MyPreview extends SurfaceView implements SurfaceHolder.Callback{

    private SurfaceHolder mHolder;
    public Camera mCamera;
    private Context context;


    public MyPreview(Context context){
        super(context);
        this.context = context;
        mHolder = getHolder();
        mHolder.addCallback(this);
        // Added for devices before 3.0 else it crashes 
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder){
        try
        {
            if(mCamera == null)
                mCamera = android.hardware.Camera.open(SectionListActivity.currentCameraId);
        }catch(Exception ex){
            Toast.makeText(getContext(), "Unable To Detect Camera" , 50).show();
            System.exit(0);
        }

        try{
            mCamera.setPreviewDisplay(mHolder);

            mCamera.setPreviewCallback(new PreviewCallback() {

                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    FileOutputStream fos = null;
                    try
                    {
                        fos = new FileOutputStream(context.getCacheDir()+"/temp.jpg");
                        fos.write(data);
                        fos.close();
                    }catch(Exception ex){
                        Toast.makeText(context, ex.getMessage(), 35).show();
                        ex.printStackTrace();
                        return;
                    }
                    MyPreview.this.invalidate();
                }
            });

        }catch (Exception e) {
            Toast.makeText(context, e.getMessage(), 35).show();
            e.printStackTrace();    
            return;
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder){
        if(mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w , int h){
        Camera.Parameters params = mCamera.getParameters();
        mCamera.setParameters(params);
        mCamera.startPreview();
    }

    public void clear(){
        if(mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }

    public void getParametersInfo(){

    }

}

看看这是否有帮助,如果你遇到任何问题,我会尝试回复...... thx