sql >> Database teknologi >  >> RDS >> Mysql

Android-kamera til at tage flere billeder

Vis billede på skærmen, før du gemmer :

Brug min kode. Jeg tager et billede med kameraets hensigt, og før det gemmes i galleriet, vises det til brugeren med en Gem og Annuller-knap :- Ring til kameraets hensigt :-

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

implementer din færdige og annuller på kliklytter - hvad du vil gøre i dem. Min kode vil fange dit billede, rotere det i den rigtige retning uanset kamerarotation og vise det til dig i en dialog, før det gemmes

Denne kode gemmer dit billede i DB. Du skal bruge "blob" for at gemme billedet. Brug denne kode :-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() ;

}

Der er en alternativ hensigtshandling for enhedskameraet, der starter kameraet i stillbilledtilstand og ikke afsluttes, før brugeren er færdig med aktiviteten:

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

Brugt med en ContentObserver var dette præcis, hvad jeg havde brug for at opnå. eller Håndter dette i ActivityResult.

Bemærk:- hvis du er ny til Android, er dette for svært for dig at forstå nu. Læs venligst Android-dokumentationen først på google og læs tutorials. Lav grundlæggende apps. Lær først




  1. C# opkald oracle lagret funktion

  2. MySQL-indekser – hvor mange er nok?

  3. Hvordan gemmer man meget gamle datoer i databasen?

  4. Brug af setDate i PreparedStatement