Capture Image from Camera and Display in Activity

问:

I want to write a module where on a click of a button the camera opens and I can click and capture an image.  If I don't like the image I can delete it and click one more image and then select the image and it should return back and display that image in the activity.

 

回答:

Here's an example activity that will launch the camera app and then retrieve the image and display 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) {   
            Bitmap photo = (Bitmap) data.getExtras().get("data");  
            imageView.setImageBitmap(photo); 
        }   
    }  
} 

Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.

Here is the layout that the above activity uses.  It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button> 
    <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView> 
 
</LinearLayout> 

And one final detail, be sure to add:

<uses-feature android:name="android.hardware.camera"></uses-feature>  

to your manifest.xml.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值