Android相机捕获

时间:2013-06-01 08:37:00

标签: android android-camera

我从surfaceview拍照现在我想在另一个活动中显示保存在图像视图中的图片。如何将picutre移动到另一个活动,然后重新启动相机以拍摄新照片。

ShutterCallback myShutterCallback = new ShutterCallback(){

            @Override
            public void onShutter() {
                // TODO Auto-generated method stub

            }};

        PictureCallback myPictureCallback_RAW = new PictureCallback(){

            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub

            }};

        PictureCallback myPictureCallback_JPG = new PictureCallback(){

            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub
                Bitmap bitmapPicture 
                    = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
            }};

1 个答案:

答案 0 :(得分:0)

public File getOutputMediaFile() {
        File imagesFolder = new File(Environment
                .getExternalStorageDirectory() + "/Orecs");
        if (!imagesFolder.exists()) {
            imagesFolder.mkdirs();
        }

        random = new Random();
        seqNo = random.nextInt(1000000);


        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
        .format(new Date());

        photoName = "IMG_" + timeStamp;

        File mediaFile;
        mediaFile = new File(imagesFolder, "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

File pictureFile;

PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            Log.d("data: ", data+"");


                maindata = data;


                new PostTask().execute();






        }
    };


private class PostTask extends AsyncTask<String, Integer, Bitmap> {
           @Override
           protected void onPreExecute() {
              super.onPreExecute();

           }

           @Override
           protected Bitmap doInBackground(String... params) {

               Log.d("In background", "sfdsdfsd");
            //   pictureFile = getOutputMediaFile();


               Log.d("In background", "wrerwe");

               pathFile = pictureFile.getPath();

               File f = new File(pathFile);
               Matrix mat = new Matrix();
               Bitmap bmp = BitmapFactory.decodeByteArray(maindata, 0, maindata.length);
               Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);

               bmpPic = ShrinkBitmap(maindata, 800, 700);

               int rotateangle =90;
                mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);

                bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true); 


              return bmpPic1;
           }


           @Override
           protected void onPostExecute(Bitmap result) {
              super.onPostExecute(result);

              ivImg.setImageBitmap(result);
           }
    }




Intent i = new Intent(currentactivity.this, nextact.class);


            System.out.println("Main Data: " +pathFile);
            i.putExtra("bytedata", pathFile);
startActivity(i);

注意:在AsyncTask中,请参阅pathFile变量。将此发送给另一个有意图的活动。 并在该活动中获取并转换为位图:

Intent i = getIntent();
        pathFile = i.getStringExtra("bytedata");

 File f = new File(pathFile);
                         Matrix mat = new Matrix();
                         Bitmap bmp;
                        try {
                            bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);


                        Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);