2013-11-10 199 views
1

問題類:的Android相機拍攝多張照片

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數據庫中。

我是新來的機器人請告訴我該怎麼做。我搜查了。但我找不到答案。 從這段代碼它只採取一張照片。

回答

2

在保存前在屏幕上顯示圖像:

使用我的代碼。我使用的相機意圖拍攝照片並將其保存到畫廊之前,它展示給用戶提供了一個保存和取消按鈕: - 通話攝像頭意圖: -

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

實現你的完成並取消點擊監聽器 - 你想在他們做什麼。我的代碼會捕捉您的圖像,將其旋轉到正確的方向,而不管相機旋轉,並在保存之前在對話框中顯示給您。

此代碼會將圖像存儲在DB中。您必須使用「blob」來存儲圖像..使用此代碼: - 公共無效insertImageInDb(INT ID,位圖圖片){

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新手,這對您而言現在太難理解了。請先閱讀android文檔,然後閱讀教程。製作基本的應用。先學會

+0

你能解釋一下上面的代碼的細節嗎?這很難理解,因爲我是android新手。 – vish

+0

@ user2975407 - 我編輯了我的代碼並添加了解釋代碼的詳細註釋。不過,我建議你先閱讀Android文檔和教程Android –

+0

CommonFunction意味着什麼? – vish

相關問題