如何使Android Camera Intent返回全尺寸图片 [英] How to make Android Camera Intent return full size picture

查看:15
本文介绍了如何使Android Camera Intent返回全尺寸图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这篇文章中的 jengelsma 代码从相机捕获图像并在 Activity 中显示以从相机中获取图像.

I am using the jengelsma code from this post Capture Image from Camera and Display in Activity to get an image from the camera.

当我得到返回的图像时,它只是很小,当我尝试扩大它时,我会立即失去图像质量.

When I get the returned image it is only small and when I try to expand it I instantly lose the image quality.

我假设这只是预览大小,但是我无法找到返回大图像,因为我需要它来操作它.

I assume that this is only the preview size however I have been unable to find out to return the large image as it is this I need in order to manipulate it.

供参考的代码是:

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 
}

非常感谢任何帮助.

推荐答案

你必须做一些额外的事情才能得到一张全尺寸的图片,它不会在 Intent 中传递它,而是将它存储在一个文件中.

You have to do a bit extra to get a fullsize picture, it doesn't pass it in the Intent, but stores it in a file.

要为此启动相机,请在 Intent 中添加额外内容:

To start the camera for this, add an extra to your Intent:

Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );

fileUri = getOutputMediaFileUri();
intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri );

getOutputMediaFileUri 看起来像:

getOutputMediaFileUri looks like:

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(){
  return Uri.fromFile(getOutputMediaFile());
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");

    return mediaFile;
}

当然,您可以修改创建文件的方法,但您喜欢修改命名.

Naturally you can modify the method that creates the file however you like to modify the naming.

然后在您的 onActivityResult 中,您只需在 Intent 上调用 getData() 即可获取图片的 URI.

Then in your onActivityResult you can get the URI to the picture by simply calling getData() on the Intent.

这篇关于如何使Android Camera Intent返回全尺寸图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆