我正在使用这篇文章Capture Image from Camera and Display in Activity中的jengelsma代码从相机中获取图像。
当我得到返回的图像时,它只是小的,当我试图扩展它时,我立即失去了图像质量。
我假设这只是预览大小,但我一直无法找到返回大图像,因为它是我需要的,以便操纵它。
供参考的代码为:
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);
}
}
}非常感谢您的帮助。
发布于 2012-10-22 02:23:56
你必须做一点额外的工作才能得到一个完整的图片,它不会在Intent中传递它,而是将它存储在一个文件中。
要为此启动相机,请为您的意图添加额外的内容:
Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
fileUri = getOutputMediaFileUri();
intent.putExtra( MediaStore.EXTRA_OUTPUT, fileUri );getOutputMediaFileUri看起来像这样:
/** 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;
}当然,您可以修改创建文件的方法,但您可以随意修改命名。
然后,在您的onActivityResult中,您可以通过简单地对Intent调用getData()来获取图片的URI。
发布于 2015-05-08 20:26:55
相机的开始意图
private void takeFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File exportDir = new File(Environment.getExternalStorageDirectory(), "TempFolder");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
String fileName = "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
mTempCameraPhotoFile = new File(exportDir,fileName);
Log.d(LOG_TAG, fileName);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempCameraPhotoFile));
startActivityForResult(takePictureIntent, REQUEST_CAMERA);
}
}onActivityResult格式的捕获结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
if (requestCode == REQUEST_CAMERA) {
String filePath = mTempCameraPhotoFile.getPath();
Log.d(LOG_TAG, filePath);
/*
// extracted file path can be load into imageview for example with Picasso loader
Uri uri = Uri.fromFile(new File(mReportImageList.get(position).getUrl()));
Picasso.with(mContext)
.load(uri)
.fit()
.centerCrop()
.into(ivPhoto);*/
}
}
}
} 在清单中
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />https://stackoverflow.com/questions/13000651
复制相似问题