2014-01-20 61 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>