• Android Camera


    Android调用系统api使用照相机功能,实现拍照获取图片以及从照相机库中获取指定图片的功能。

    下面是演示样例代码:


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    	<ImageView android:layout_width="match_parent"
    	    android:layout_height="100dip"
    	    android:id="@+id/image"
    	    />
     	<Button 
     	    android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="testPopupWindow"
            android:layout_below="@id/image"
            />
    
    </RelativeLayout>
    


    /**
     * 实现Popup弹出窗体并实现调用系统照相机功能
     * @author dream
     *
     */
    public class TestPopupWindow extends Activity {
    	private SelectPopupWindow popupWindow;
    	private ImageView image;
    	private Button btn;
    	private Uri fileUri;     
    	
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main1);
    		init();
    	}
    	
    	private void init(){
    		image = (ImageView) findViewById(R.id.image);
    		btn = (Button) findViewById(R.id.btn);
    		btn.setOnClickListener(onClickListener);
    		fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")); //图片存放路径
    	}
    	
    	View.OnClickListener onClickListener = new View.OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			switch(v.getId()){
    			case R.id.btn:
    				test();
    				break;
    			case R.id.takephoto:
    				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    				intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    				startActivityForResult(intent, 100);
    				break;
    			case R.id.selectfromalbum:
    				Intent intent1 = new Intent(Intent.ACTION_PICK);
    				intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    				startActivityForResult(intent1, 200);
    				break;
    				
    			}
    		}
    	};
    	
    	
    	
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		// TODO Auto-generated method stub
    		super.onActivityResult(requestCode, resultCode, data);
    		if(requestCode == 100 && resultCode == RESULT_OK){
    			int width = image.getWidth();
    			int height = image.getHeight();
    			BitmapFactory.Options opts = new BitmapFactory.Options();
    			opts.inJustDecodeBounds = true;
    			BitmapFactory.decodeFile(fileUri.getPath(), opts);
    			int w = opts.outWidth;
    			int h = opts.outHeight;
    			int factor;
    			if(w>width && h>height){
    				factor = Math.min(w/width, h/height);  //依据ImageView的大小按一定比例缩小图片
    			}else {
    				factor = 1;
    			}
    			opts.inSampleSize = factor;
    			opts.inJustDecodeBounds = false;
    			Bitmap bm = BitmapFactory.decodeFile(fileUri.getPath(), opts);
    			image.setImageBitmap(bm);
    		} else if(requestCode == 200 && resultCode == RESULT_OK){
    			int width = image.getWidth();
    			int height = image.getHeight();
    			BitmapFactory.Options opts = new BitmapFactory.Options();
    			opts.inJustDecodeBounds = true;
    			Uri uri = data.getData();
    			try {
    				InputStream in = getContentResolver().openInputStream(uri);
    				BitmapFactory.decodeStream(in, null, opts);
    				int w = opts.outWidth;
    				int h = opts.outHeight;
    				int factor;
    				if(w>width && h>height){
    					factor = Math.min(w/width, h/height);  //依据ImageView的大小按一定比例缩小图片
    				}else {
    					factor = 1;
    				}
    				opts.inSampleSize = factor;
    				opts.inJustDecodeBounds = false;
    				in = getContentResolver().openInputStream(uri);   //须要再次获取,由于前面流已经改变了
    				Bitmap bm = BitmapFactory.decodeStream(in, null, opts);
    				image.setImageBitmap(bm);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			
    		}
    		
    	}
    
    	public void test(){
    		popupWindow = new SelectPopupWindow(this, onClickListener);
    		popupWindow.showAtLocation(findViewById(R.id.main), 
    				Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
    	}
    	
    }
    


    执行结果:



















  • 相关阅读:
    2014年终总结
    杭电2014——青年歌手大奖赛_评委会打分
    nyoj---t448(寻找最大数)
    nyoj_t218(Dinner)
    将string转换成char*
    nyoj71--独木舟上的旅行
    基于贪心算法的几类区间覆盖问题
    会场安排问题—NYOJ14
    南阳理工ACM——106背包问题
    南阳理工91——阶乘之和
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5364787.html
Copyright © 2020-2023  润新知