大家好,欢迎来到IT知识分享网。
说道我们的智能手机,相机是必不可少的组件了,今天我们就以一个简单地例子来介绍我们的相机操作,直接上干货
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.下面是运行结果
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/157490.html