Android的相机拍下多张照片拍下、多张、相机、照片

2023-09-05 10:48:53 作者:这年头真爱全是非卖品゜

在问题类:

    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);
           }    
           }

这就是我想要做的:

取多张照片

Take multiple photos

显示它们在屏幕

将它们存储在MySQL DATABSE。

store them in the mysql databse.

我是新到Android,请告诉我该怎么做that.I searched.But我找不到答案。 从这个code IT只需要一张照片。

I am new to android please tell me how to do that.I searched.But I could not find a answer. From this code IT TAKE ONLY ONE PHOTO.

推荐答案

显示屏幕上的影像之前保存:​​

Show Image on Screen Before Saving :

使用我的code。我拍照用摄像头的意图,并将其保存到画廊之前,它展示给用户提供了一个保存和取消按钮: - 通话摄像​​头意图: -

Use my Code. I am taking a picture using camera intent and before saving it to gallery , it is showed to the user with a Save and Cancel Button :- Call Camera Intent :-

// 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: -

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){}
      }
 }
}

实现你做,并取消对点击听众 - 你想做的事在他们什么。我的code将捕获你的形象,将其旋转方向是正确的,不论摄像头转动,并在保存前展示给你一个对话框,它

implement your done and cancel on click listener - what you want to do in them. My code will capture your image, rotate it in the right direction irrespective of camera rotation and show it to you in a dialog before saving it

这code将存储图像的DB.You都用一滴存储图像。使用本code: - 公共无效insertImageInDb(INT ID,位图图片){

This code will store your image in DB.You have to use "blob" to store image.. Use This Code :- 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() ;

}

有用于设备照相机的替代意图动作,会启动相机在静止图像模式和直到用户完成与该活动不退出:

There's an alternative intent action for the device camera that launches the camera in still image mode and does not exit until the user is finished with the activity:

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

用于与ContentObserver这正是我需要完成。或处理这ActivityResult。

Used with a ContentObserver this was exactly what I needed to accomplish. or Handle this in ActivityResult.

注: - 如果你是新的机器人,这是太辛苦了,你现在明白了。请先阅读文档的Andr​​oid对谷歌和阅读教程。进行基本的应用程序。先学习

Note :- if you are new to android, this is too hard for you to understand now. Please read the android documentation first on google and read tutorials. Make basic apps. Learn first