【问题标题】:Android camera to take multiple photos [closed]Android相机拍摄多张照片[关闭]
【发布时间】:2013-11-22 01:23:10
【问题描述】:

问题类:

    public class problem extends Activity {

        ImageView iv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.problem);

    iv=(ImageView) findViewById(R.id.imageView1);

    Button b=(Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);

        }
    });
            }

         @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bm=(Bitmap) data.getExtras().get("data");
       iv.setImageBitmap(bm);
           }    
           }

这就是我想做的:

  • 拍摄多张照片

  • 在屏幕上显示它们

  • 将它们存储在 mysql 数据库中。

我是 android 新手,请告诉我该怎么做。我搜索了。但我找不到答案。 从这段代码中它只拍一张照片。

【问题讨论】:

    标签: java android mysql


    【解决方案1】:

    保存前在屏幕上显示图像:

    使用我的代码。我正在使用相机意图拍照,在将其保存到画廊之前,它会通过保存和取消按钮向用户显示:- 调用相机意图:-

    // This code is to call the camera intent. Basically it will start your camera. Put this code in a button or something                        
    String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator +CommonFunction.getDateTime()+".jpg"; // Get File Path
                            Intent takePictureFromCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            takePictureFromCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
                            startActivityForResult(takePictureFromCameraIntent, 123);
    

    onActivityResult : -

    // This function is called when you come back to your activity after the intent has finished. Do read android documentation on Google. It will Help
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT) 
        {
            if (resultCode == Activity.RESULT_OK) 
            {
                String galleryImatePath = SD_CARD_TEMP_DIR; // make SD_CARD_TEMP_DIR Global so that you can access it here from camera intent or pass it in put Extra method and retrieve it here
                File f = new File(galleryImatePath);
    
                try {//This code will rotate your image if you have taken the image by rotating the camera
                            Bitmap cameraBitmap = null;
                            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                            bmOptions.inJustDecodeBounds = false;
                            bmOptions.inPurgeable = true;
                            bmOptions.inBitmap = cameraBitmap; 
                            bmOptions.inMutable = true; 
    
    
                            cameraBitmap = BitmapFactory.decodeFile(galleryImatePath,bmOptions);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
    
                            //To Rotate image Code
                                ExifInterface exif = new ExifInterface(galleryImatePath);
                                float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
                                System.out.println(rotation);
    
                            float rotationInDegrees = exifToDegrees(rotation);
                            System.out.println(rotationInDegrees);
    
                            Matrix matrix = new Matrix();
                            matrix.postRotate(rotationInDegrees);
    
                            final Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
                            FileOutputStream fos=new FileOutputStream(galleryImatePath);
                            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
                            fos.write(bos.toByteArray());
                            cameraBitmap.recycle();
                            System.gc();
                            fos.flush();
                            fos.close();
    
    
                            // To set image in imageview in dialog. This code will set your image in a custon dialog box "captiondialog". It will contain a full width and height imageview and two textviews - done and cancel. It is upto u what you want to define in the textview's click listener. For example, you can pass the storing image in database in the "Done" textview and "Cancel" textview will dismiss your captiondialog and you app will return to your activity
                        Capdialog = new Dialog(AddToDo.this,android.R.style.Theme_NoTitleBar_Fullscreen);
                        Capdialog.setContentView(R.layout.captiondialog);
                        Capdialog.setCancelable(false);
                        TextView cancel = (TextView) Capdialog
                                .findViewById(R.id.cancel);
                        TextView done = (TextView) Capdialog.findViewById(R.id.done);
                                                    Capdialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                        ImageView img = (ImageView) Capdialog.findViewById(R.id.image);
                        img.setImageBitmap(rotatedBitmap);
                   }
                   catch(Exception e){}
          }
     }
    }
    

    在点击监听器上实现你的完成和取消 - 你想在其中做什么。我的代码将捕获您的图像,无论相机旋转如何,都将其旋转到正确的方向,并在保存之前在对话框中显示给您

    此代码会将您的图像存储在数据库中。您必须使用“blob”来存储图像。使用此代码:- public void insertImageInDb(int id , Bitmap img ) {

    byte[] data = bos.toByteArray(); // Use This or the code in comments below
    
    /*  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);       
        byte[] data = outputStream.toByteArray();*/
    
    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);
    
    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ;
    

    }

    对于设备摄像头有一个替代的意图操作,它以静止图像模式启动摄像头,并且在用户完成活动之前不会退出:

    Intent intent = new Intent(
        MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
    this.startActivity(intent);
    

    与 ContentObserver 一起使用,这正是我需要完成的。或在 ActivityResult 中处理。

    注意:- 如果您是 android 新手,那么您现在很难理解。请先在 google 上阅读 android 文档并阅读教程。制作基本应用程序。先学习

    【讨论】:

    • 你能详细解释一下上面的代码吗?很难理解,因为我是 android 新手。
    • @user2975407 - 我已经编辑了我的代码并添加了解释代码的详细信息 cmets。但是,我建议您先在 Android 上阅读 android 文档和教程
    • CommonFunction 是什么意思?
    • -无论如何感谢您的帮助。我会先阅读文档。
    • @Rahul-感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多