2014-01-20 85 views
0

我有點新的Android應用程序開發,並努力做一個簡單的應用程序,它,的Android捕捉和顯示圖像的應用程序

  1. 使用系統相機應用程序通過將攝像機的意圖進行拍照。
  2. 將圖像存儲在文件系統中的公用文件夾中。
  3. 然後將圖像從其絕對路徑轉換爲位圖,並通過在圖像視圖中執行一些縮放來顯示它。

這裏是我的代碼....

static final int REQUEST_IMAGE_CAPTURE = 1; 
String mCurrentPhotoPath; 
Button captureImageButton; 
static ImageView imageCapturedView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_take_image); 




    captureImageButton = (Button) findViewById(R.id.button); 

    OnClickListener captureImageListener = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent captureImageIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      if(captureImageIntent.resolveActivity(getPackageManager())!=null) 
      { 
       File photoFile=null; 

       try{ 
        photoFile = createImageFile(); 

       }catch(IOException e){}; 

       if(photoFile != null) 
       { 

        captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
        startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE); 
       } 

      } 

     } 
    }; 

    captureImageButton.setOnClickListener(captureImageListener); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    imageCapturedView = (ImageView)findViewById(R.id.ImageView); 

    if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK) 
    { 
     Bundle extras = data.getExtras(); 
     Bitmap imageBitmap= (Bitmap) extras.get("data"); 

     imageCapturedView.setImageBitmap(imageBitmap); 
     galleryAddPic(); 
     setPic(); 
    } 
} 

private File createImageFile() throws IOException 
{ 

    String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date()); 
    String ImageFile = "JPEG_" + TimeStamp + "_"; 
    File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

    File image = File.createTempFile(ImageFile, ".jpg", StorageDir); 

    mCurrentPhotoPath = image.getAbsolutePath(); 

    return image; 

} 

private void galleryAddPic() 
{ 
    Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f= new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScan.setData(contentUri); 
    this.sendBroadcast(mediaScan); 

} 


private void setPic() throws ArithmeticException{ 


    int scaleFactor; 
    Bitmap bitmap; 
    // Get the dimensions of the View 
    int targetW = imageCapturedView.getWidth(); 
    int targetH = imageCapturedView.getHeight(); 


    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    //bmOptions.inSampleSize = 4; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    // Determine how much to scale down the image 
     scaleFactor= Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 



    bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 

    Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show(); 
    imageCapturedView.setImageBitmap(bitmap); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.take_image, menu); 
    return true; 
} 

}

這裏的問題在於,根據我的setPic功能,但不知道它是什麼,因爲我剛纔跟着教程給開發者.android

我能夠捕捉圖像並將其存儲在SD卡中,但無法顯示它在圖像視圖。根據圖像視圖屬性縮放位圖時發生錯誤。

這是我收到

01-20 14:32:34.736: E/AndroidRuntime(2513): FATAL EXCEPTION: main 
01-20 14:32:34.736: E/AndroidRuntime(2513): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/JPEG_2014012020_143223_-1472164486.jpg typ=image/jpeg (has extras) }} to activity {yogesh.atArxxus.ocr_trial_application/yogesh.atArxxus.ocr_trial_application.Take_image}: java.lang.ArithmeticException: divide by zero 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3161) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3204) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread.access$1100(ActivityThread.java:137) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.os.Looper.loop(Looper.java:213) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread.main(ActivityThread.java:4793) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at dalvik.system.NativeStart.main(Native Method) 
01-20 14:32:34.736: E/AndroidRuntime(2513): Caused by: java.lang.ArithmeticException: divide by zero 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at yogesh.atArxxus.ocr_trial_application.Take_image.setPic(Take_image.java:136) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at yogesh.atArxxus.ocr_trial_application.Take_image.onActivityResult(Take_image.java:87) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.Activity.dispatchActivityResult(Activity.java:5192) 
01-20 14:32:34.736: E/AndroidRuntime(2513):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3157) 

謝謝

+0

如果我刪除縮放代碼,只是試圖顯示在圖像視圖圖像那麼它正在工作。不知道什麼是錯的一些我如何獲得targetW targetH這個值在某個地方爲零。 –

+0

我也有類似的問題。所以請檢查下面的鏈接,它可能對你有用。 http://stackoverflow.com/questions/21136311/picking-a-photo-from-gallery-and-show-in-a-image-view – Biplab

+0

@Biplab所以,你使用圖像選擇器庫...? –

回答

1

在線 「比例因子= Math.min(photoW/targetW,photoH/targetH);」 錯誤,可以targetW或targetH或兩者都爲零。只需在該行之前包含一個像這樣的簡單驗證。

if(targetH == 0) targetH = 1; 
if(targetW == 0) targetW = 1; 

你的下一步應該是在調試爲什麼其中的一個包含零。

+0

是的,這實際上是我的問題...我調試程序,並知道其中一個值變成零,所以公式用於縮放將分母設置爲0,應用程序失敗....但不知道爲什麼值變成0 ..?實際上沒有理由..?我只是得到我的imageView的heaight和寬度.. –

+0

在xml中,ImageView(R.id.ImageView)具有用於佈局寬度和佈局高度的固定尺寸? – Srikanth

+1

明白了。請參考這些答案 - http://stackoverflow.com/questions/4680499/android-get-height-width-of-imageview和http://stackoverflow.com/questions/10411975/how-to-get-the-width Android中的圖像高度和視圖高度。實際上,更好的方法是將尺寸存儲在dimension-values xml中,並使用該尺寸將imageview的尺寸設置爲xml佈局文件,並使用相同的尺寸(來自值xml)以在setPic()方法中縮放圖像 – Srikanth

0

試試這個,但它不是正確測試

public class MainActivity extends Activity { 

    // Activity request codes 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    public static final int MEDIA_TYPE_VIDEO = 2; 
    public static int RESULT_LOAD_IMAGE = 1; 

    // directory name to store captured images and videos 
    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera"; 

    private Uri fileUri; // file url to store image/video 

    private ImageView imgPreview; 
    private VideoView videoPreview; 
    private Button btnCapturePicture, btnRecordVideo; 
    private ImageButton captureImage,browsImage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     imgPreview = (ImageView) findViewById(R.id.imgPreview); 
     videoPreview = (VideoView) findViewById(R.id.videoPreview); 
     btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture); 
     btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo); 
     captureImage = (ImageButton) findViewById(R.id.captureImage); 
     browsImage = (ImageButton) findViewById(R.id.browsImage); 


     /* 
     * Capture image button click event 
     */ 
     captureImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // capture picture 
       captureImage(); 
      } 
     }); 


     browsImage.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // capture pictur 
       Intent i = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore 
           .Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(i, RESULT_LOAD_IMAGE); 
       } 
     }); 
     /* 
     * Record video button click event 
     */ 
     btnRecordVideo.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // record video 
       recordVideo(); 
      } 
     }); 

     // Checking camera availability 
     if (!isDeviceSupportCamera()) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! Your device doesn't support camera", 
        Toast.LENGTH_LONG).show(); 
      // will close the app if the device does't have camera 
      finish(); 
     } 
    } 

    /** 
    * Checking device has camera hardware or not 
    * */ 
    private boolean isDeviceSupportCamera() { 
     if (getApplicationContext().getPackageManager().hasSystemFeature(
       PackageManager.FEATURE_CAMERA)) { 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

    /* 
    * Capturing Camera Image will lauch camera app requrest image capture 
    */ 
    private void captureImage() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

     // start the image capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
    } 

    /* 
    * Here we store the file url as it will be null after returning from camera 
    * app 
    */ 
    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // save file url in bundle as it will be null on scren orientation 
     // changes 
     outState.putParcelable("file_uri", fileUri); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 

     // get the file url 
     fileUri = savedInstanceState.getParcelable("file_uri"); 
    } 

    /* 
    * Recording video 
    */ 
    private void recordVideo() { 
     Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 

     // set video quality 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file 
                  // name 

     // start the video capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
    } 

    /** 
    * Receiving activity result method will be called after closing the camera 
    * */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 

     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // successfully captured the image 
       // display it in image view 
       previewCapturedImage(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // video successfully recorded 
       // preview the recorded video 
       previewVideo(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled recording 
       Toast.makeText(getApplicationContext(), 
         "User cancelled video recording", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to record video 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to record video", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
     else if (requestCode == RESULT_LOAD_IMAGE) { 

      super.onActivityResult(requestCode, resultCode, data); 

      if (resultCode == RESULT_OK && null != data) { 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String picturePath = cursor.getString(columnIndex); 
       cursor.close(); 
       Bitmap bm=BitmapFactory 
         .decodeFile(picturePath); 
       Bitmap pic=ShrinkBitmap(picturePath,100,100); 

       imgPreview = (ImageView) findViewById(R.id.imgPreview); 
       imgPreview.setVisibility(View.VISIBLE); 
       imgPreview.setImageBitmap(pic); 


      } 
     } 
    } 

    /* 
    * Display image from a path to ImageView 
    */ 
    private void previewCapturedImage() { 
     try { 
      // hide video preview 
      videoPreview.setVisibility(View.GONE); 

      imgPreview.setVisibility(View.VISIBLE); 

      // bimatp factory 
      BitmapFactory.Options options = new BitmapFactory.Options(); 

      // downsizing image as it throws OutOfMemory Exception for larger 
      // images 
      options.inSampleSize = 6; 



      final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
        options); 

      try { 
      File file = new File (fileUri.getPath()); 
      FileOutputStream out = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out); 
      imgPreview.setImageBitmap(bitmap); 
      out.flush(); 
       out.close(); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    /* 
    * Previewing recorded video 
    */ 
    private void previewVideo() { 
     try { 
      // hide image preview 
      imgPreview.setVisibility(View.GONE); 

      videoPreview.setVisibility(View.VISIBLE); 
      videoPreview.setVideoPath(fileUri.getPath()); 
      // start playing 
      videoPreview.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * ------------ Helper Methods ---------------------- 
    * */ 

    /* 
    * Creating file uri to store image/video 
    */ 
    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /* 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 

    Bitmap ShrinkBitmap(String file, int width, int height){ 

     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

      int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
      int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

      if (heightRatio > 1 || widthRatio > 1) 
      { 
      if (heightRatio > widthRatio) 
      { 
       bmpFactoryOptions.inSampleSize = heightRatio; 
      } else { 
       bmpFactoryOptions.inSampleSize = widthRatio; 
      } 
      } 

      bmpFactoryOptions.inJustDecodeBounds = false; 
      bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
     return bitmap; 
     } 

} 

* * XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:baselineAligned="false" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <!-- Capture picture button --> 

    <ImageButton 
     android:id="@+id/captureImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/camera" /> 

    <ImageButton 
     android:id="@+id/browsImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/brows" /> 

    <Button 
     android:id="@+id/btnCapturePicture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Take a Picture" 
     android:visibility="gone" 
     android:layout_marginBottom="10dp"/> 

    <!-- Record video button --> 
    <Button 
     android:id="@+id/btnRecordVideo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:text="Record a Video" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:padding="10dp"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Preview" 
     android:padding="10dp" 
     android:textSize="15dp"/> 

    <!-- To display picture taken --> 
    <ImageView 
     android:id="@+id/imgPreview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <!-- To preview video recorded --> 
    <VideoView 
     android:id="@+id/videoPreview" 
     android:layout_width="wrap_content" 
     android:layout_height="400dp" 
     android:visibility="gone" /> 
</LinearLayout>