2015-09-12 80 views
0

我想拍摄一张照片并将其放入一张imageView中。我设法打开相机,每当我拍摄照片时,都会让我拍摄一张照片。 这是我的onCreate方法:拍摄一张照片,然后将其显示到ImageView

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 
    File photo = dispatchTakePictureIntent(); 
    ImageView imgView = (ImageView) findViewById(R.id.DisplayImageView); 
    if(photo.exists()){ 
     Bitmap myBitmap = BitmapFactory.decodeFile(photo.getAbsolutePath()); 
     imgView.setImageBitmap(myBitmap); 
    } 
} 

这是我的拍照方法:

private File dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File photoFile = null; 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
      Toast.makeText(this, "Failed to create the image file!", Toast.LENGTH_SHORT).show(); 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 

     } 
    } 
    return photoFile; 
} 

我需要一个后照片要回从相机的活动,并查看它。我怎样才能做到这一点?

+0

覆盖'用于拍照后得到结果返回onActivityResult'方法 –

回答

1

您需要重写onActivityResult()你的活动得到了ImageView的替换mImageView图像并将其显示在ImageView中。您应该根据自己的需要对图像进行采样,并在ImageView中显示时防止图像旋转,因此您需要查找Exif参数。

这里是onActivityResult()代码。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_TAKE_PHOTO) { 

// in case of taking pic from camera, you have to define filepath already and image get saved in that filepath you specified at the time when you started camera intent.So in your case it will be following 

      String filePath=photoFile.getAbsolutePath(); //photofile is the same file you passed while starting camera intent. 
      if (filePath != null) { 
       int orientation = 0; 
       private Bitmap imageBitmap; 
       try { 
        ExifInterface exif = new ExifInterface(filePath); 
        orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 1); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        final BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(filePath, options); 
        options.inSampleSize = calculateInSampleSize(
          options, reqWidth, rewHeight); 
        options.inJustDecodeBounds = false; 
        imageBitmap = BitmapFactory.decodeFile(filePath, 
          options); 
        if (orientation == 6) { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(90); 
         imageBitmap = Bitmap.createBitmap(imageBitmap, 
           0, 0, imageBitmap.getWidth(), 
           imageBitmap.getHeight(), matrix, true); 
        } else if (orientation == 8) { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(270); 
         imageBitmap = Bitmap.createBitmap(imageBitmap, 
           0, 0, imageBitmap.getWidth(), 
           imageBitmap.getHeight(), matrix, true); 
        } else if (orientation == 3) { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(180); 
         imageBitmap = Bitmap.createBitmap(imageBitmap, 
           0, 0, imageBitmap.getWidth(), 
           imageBitmap.getHeight(), matrix, true); 
        } 
       } catch (OutOfMemoryError e) { 
        imageBitmap = null; 
        e.printStackTrace(); 
       } catch (Exception e) { 
        imageBitmap = null; 
        e.printStackTrace(); 
       } 
      } 
      if (imageBitmap != null) { 
       // set this imageBitmap to your ImageView 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

,这是采样函数

public static int calculateInSampleSize(BitmapFactory.Options options, 
             int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 
    if (height > reqHeight || width > reqWidth) { 
     final int halfHeight = height/2; 
     final int halfWidth = width/2; 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 
    return inSampleSize; 
} 
0
在你的活动,做startActivityForResult重写以下方法

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap imageBitmap = (Bitmap) extras.get("data"); 
     mImageView.setImageBitmap(imageBitmap); 
    } 
} 

你想展示你的形象

相关问题