用相机拍照并在图像视图中查看
Posted
技术标签:
【中文标题】用相机拍照并在图像视图中查看【英文标题】:Taking a Picture With Camera and Viewing in Image View 【发布时间】:2016-11-19 21:41:50 【问题描述】:我不知道为什么我的代码刚刚运行并突然停止了?我想通过我的图像按钮拍照,然后将其显示在图像视图中。这是我的代码,不会显示在图像视图中:
public class UserInterface extends AppCompatActivity
private static final int REQUEST_IMAGE_CAPTURE= 1;
private ImageView imageView;
ImageButton cap;
Bundle extras;
Bitmap imageBitmap;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_interface);
imageView = (ImageView)this.findViewById(R.id.imageView1);
cap = (ImageButton) this.findViewById(R.id.cap);
cap.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
【问题讨论】:
代码对我来说看起来不错。有错误吗?或者相机打开但捕获的图像没有进入图像视图?还是什么都没有发生? 捕获的图像不会进入图像视图。相机出现,并将图像保存在我的手机上,但它没有出现在图像视图中。 你确定 imageView id 是 imageView1 吗? 是的,我刚刚检查过了。 是否调用了 onActivityResult() 方法?放置一些日志消息并检查。 【参考方案1】:Uri imageUri;
在您的按钮点击监听器上:
cap.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
);
关于活动结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
imageView.setImageURI(imageUri);
【讨论】:
【参考方案2】:我之前遇到过这个问题,这是因为图像在完全保存到设备之前就显示出来了,所以尝试将其包装在延迟处理程序中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
new Handler().postDelayed(new Runnable()
@Override
public void run()
extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
, 750);
如果它也不起作用,请尝试先将其保存到 sdcard 并直接从那里获取:
cap.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
);
然后得到它:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap image = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
// BEST QUALITY MATCH
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight)
inSampleSize = Math.round((float)height / (float)reqHeight);
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth)
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
【讨论】:
以上是关于用相机拍照并在图像视图中查看的主要内容,如果未能解决你的问题,请参考以下文章