Android中的Carema(相机操作)

说道我们的智能手机,相机是必不可少的组件了,今天我们就以一个简单地例子来介绍我们的相机操作,直接上干货

1.首先打开相机,我们需要一定的权限,所以首先我们要声明一下权限.


2.下面我们进行了一下简单的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.caremadck.MainActivity" >

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/ss"
android:layout_weight="1"
android:id="@+id/buttonGet"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/cc"
android:layout_weight="1"
android:id="@+id/buttonForm"/>
</LinearLayout>

</LinearLayout>

3.下面是主程序的代码

package com.android.caremadck;

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

public class MainActivity extends Activity {
	//注意全局变量与局部变量的区别
	private Button buttonGet;
	private Button buttonFrom;
	private ImageView imageView;
	private final int OPEN_REQUES=1;
	private final int GET_REQUES=2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        buttonGet=(Button)findViewById(R.id.buttonGet);
        buttonFrom=(Button)findViewById(R.id.buttonForm);
        imageView=(ImageView)findViewById(R.id.imageView);
        
        buttonGet.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(android.provider.
						MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent,OPEN_REQUES);
			}
		});
        
        buttonFrom.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(intent,GET_REQUES);
				
			}
		});
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	super.onActivityResult(requestCode, resultCode, data);
    	switch (requestCode) {
		case OPEN_REQUES:{
			if(resultCode==RESULT_OK){
			Bundle bundle=data.getExtras();
			Bitmap bitmap=(Bitmap) bundle.get("data");
			imageView.setImageBitmap(bitmap);
			}
			
			break;
		}
		case GET_REQUES:{
			if(resultCode==RESULT_OK){
				Uri uri=data.getData();
				imageView.setImageURI(uri);
			}
			break;
		}

		default:
			break;
		}
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

该demo可以实现两个功能,可以拍照片,也可以从相册里面进行选取

最后将其显示到屏幕上

4.下面是运行结果



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值