Android调用相机程序和图片处理程序获得原图并且进行图片处理

    

        Android调用相机程序和图片处理程序获得原图并且进行图片处理             

        分类:             Android开发                   1376人阅读     评论(3)     收藏     举报    

这篇文章主要介绍的是调用相机程序获得图片,调用Android操作系统自带的图片处理程序处理图片然后返回到我们的程序。

先看看布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.       
  8.   
  9.     <Button  
  10.         android:id="@+id/button1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Button" />  
  14.   
  15.     <ImageView  
  16.         android:id="@+id/imageView1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content" />  
  19.   
  20. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

向布局中加入了一个Button和一个ImageView。

在Button的OnClick方法中启动相机程序,让用户选择使用哪个相机,代码如下:

  1. @Override  
  2. public void onClick(View v) {  
  3.     //doTakePhoto();  
  4.     File fos=null;  
  5.     try {  
  6.         fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  7.     } catch (Exception e) {  
  8.         e.printStackTrace();  
  9.     }  
  10.     Uri u=Uri.fromFile(fos);  
  11.     Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  12.     i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);  
  13.     i.putExtra(MediaStore.EXTRA_OUTPUT, u);  
  14.   
  15.     this.startActivityForResult(i, 9);  
  16. }   
	@Override
	public void onClick(View v) {
		//doTakePhoto();
		File fos=null;
		try {
			fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Uri u=Uri.fromFile(fos);
		Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		i.putExtra(MediaStore.EXTRA_OUTPUT, u);

		this.startActivityForResult(i, 9);
	} 

效果如下:

向Intent中放入一些信息,指定保存原图,额外的保存在我指定的地址。使用this.startActivityForresult();传入intent对象和请求的值。


覆盖父类的onActivityResult方法,当其他Activity结束之后返回到本Activity。

  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  2.          
  3.           
  4.         if(resultCode==RESULT_OK)  
  5.         {  
  6.             if(requestCode==9)  
  7.             {  
  8.                   
  9.                 File  bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  10.                   
  11.                 Intent i=new Intent("com.android.camera.action.CROP");  
  12.                 i.setType("image/*");  
  13.                   
  14.   
  15.                 //i.putExtra("data", bb);  
  16.                 i.setDataAndType(Uri.fromFile(bb), "image/jpeg");  
  17.   
  18.                 i.putExtra("crop""true");  
  19.   
  20.                 i.putExtra("aspectX"1);  
  21.   
  22.                 i.putExtra("aspectY"1);  
  23.   
  24.                 i.putExtra("outputX"500);  
  25.   
  26.                 i.putExtra("outputY"500);  
  27.   
  28.                 i.putExtra("return-data"true);  
  29.                   
  30.                 this.startActivityForResult(i, 7);  
  31.   
  32.   
  33.   
  34.   
  35.             }  
  36.               
  37.             if(requestCode==7)  
  38.             {  
  39.                 Bitmap bb=data.getParcelableExtra("data");  
  40.                 i.setImageBitmap(bb);  
  41.                   
  42.             }  
  43.         }  
  44.           
  45.           
  46.           
  47.         }  
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       
        
        if(resultCode==RESULT_OK)
        {
        	if(requestCode==9)
        	{
        		
        		File  bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
        		
        		Intent i=new Intent("com.android.camera.action.CROP");
        		i.setType("image/*");
        		

                //i.putExtra("data", bb);
        		i.setDataAndType(Uri.fromFile(bb), "image/jpeg");

                i.putExtra("crop", "true");

                i.putExtra("aspectX", 1);

                i.putExtra("aspectY", 1);

                i.putExtra("outputX", 500);

                i.putExtra("outputY", 500);

                i.putExtra("return-data", true);
                
                this.startActivityForResult(i, 7);




        	}
        	
        	if(requestCode==7)
        	{
        		Bitmap bb=data.getParcelableExtra("data");
        		i.setImageBitmap(bb);
        		
        	}
        }
        
        
        
        }

通过返回的请求值,我就知道是从哪个Activity返回来的,如果是从相机程序返回,那就获得拍照之后的数据,跳到图片处理程序。如果是从图片处理程序回来的,就获得返回的数据,显示处理之后的图片。


最后不要忘了在程序退出的时候删除在SDcard上没用的图片。

  1. @Override  
  2.     protected void onDestroy() {  
  3.         super.onDestroy();  
  4.         File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  5.         if(f.exists())  
  6.         {  
  7.             f.delete();  
  8.         }  
  9.           
  10.     }   
@Override
	protected void onDestroy() {
		super.onDestroy();
		File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
		if(f.exists())
		{
			f.delete();
		}
		
	} 

好了,就这样。

下面贴出运行效果图:





下面是本Activity的全部代码:

  1. package com.zhycheng.takephoto;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.net.Uri;  
  12. import android.os.Bundle;  
  13. import android.os.Environment;  
  14. import android.provider.MediaStore;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.ImageView;  
  19.   
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.     String localTempImgFileName;  
  22.     String localTempImgDir="TestCamera";  
  23.     Button b;  
  24.     ImageView i;  
  25.       
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         b=(Button) findViewById(R.id.button1);  
  31.         b.setOnClickListener(this);  
  32.         i=(ImageView) findViewById(R.id.imageView1);  
  33.     }  
  34.       
  35.   
  36.     @Override  
  37.     public void onClick(View v) {  
  38.         //doTakePhoto();  
  39.         File fos=null;  
  40.         try {  
  41.             fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  42.         } catch (Exception e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         Uri u=Uri.fromFile(fos);  
  46.         Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  47.         i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);  
  48.         i.putExtra(MediaStore.EXTRA_OUTPUT, u);  
  49.   
  50.         this.startActivityForResult(i, 9);  
  51.     }   
  52.       
  53.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  54.          
  55.           
  56.         if(resultCode==RESULT_OK)  
  57.         {  
  58.             if(requestCode==9)  
  59.             {  
  60.                   
  61.                 File  bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  62.                   
  63.                 Intent i=new Intent("com.android.camera.action.CROP");  
  64.                 i.setType("image/*");  
  65.                   
  66.   
  67.                 //i.putExtra("data", bb);  
  68.                 i.setDataAndType(Uri.fromFile(bb), "image/jpeg");  
  69.   
  70.                 i.putExtra("crop""true");  
  71.   
  72.                 i.putExtra("aspectX"1);  
  73.   
  74.                 i.putExtra("aspectY"1);  
  75.   
  76.                 i.putExtra("outputX"500);  
  77.   
  78.                 i.putExtra("outputY"500);  
  79.   
  80.                 i.putExtra("return-data"true);  
  81.                   
  82.                 this.startActivityForResult(i, 7);  
  83.   
  84.   
  85.   
  86.   
  87.             }  
  88.               
  89.             if(requestCode==7)  
  90.             {  
  91.                 Bitmap bb=data.getParcelableExtra("data");  
  92.                 i.setImageBitmap(bb);  
  93.                   
  94.             }  
  95.         }  
  96.           
  97.           
  98.           
  99.         }  
  100.   
  101.   
  102.     @Override  
  103.     protected void onDestroy() {  
  104.         super.onDestroy();   
  105.         File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");  
  106.         if(f.exists())  
  107.         {  
  108.             f.delete();  
  109.         }  
  110.           
  111.     }   
  112.       
  113.       
  114.       
  115.       
  116.       
  117.   
  118.   
  119.   
  120.   
  121.   
  122. }  
package com.zhycheng.takephoto;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
	String localTempImgFileName;
	String localTempImgDir="TestCamera";
	Button b;
	ImageView i;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button) findViewById(R.id.button1);
        b.setOnClickListener(this);
        i=(ImageView) findViewById(R.id.imageView1);
    }
    

	@Override
	public void onClick(View v) {
		//doTakePhoto();
		File fos=null;
		try {
			fos=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Uri u=Uri.fromFile(fos);
		Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		i.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		i.putExtra(MediaStore.EXTRA_OUTPUT, u);

		this.startActivityForResult(i, 9);
	} 
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       
        
        if(resultCode==RESULT_OK)
        {
        	if(requestCode==9)
        	{
        		
        		File  bb=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
        		
        		Intent i=new Intent("com.android.camera.action.CROP");
        		i.setType("image/*");
        		

                //i.putExtra("data", bb);
        		i.setDataAndType(Uri.fromFile(bb), "image/jpeg");

                i.putExtra("crop", "true");

                i.putExtra("aspectX", 1);

                i.putExtra("aspectY", 1);

                i.putExtra("outputX", 500);

                i.putExtra("outputY", 500);

                i.putExtra("return-data", true);
                
                this.startActivityForResult(i, 7);




        	}
        	
        	if(requestCode==7)
        	{
        		Bitmap bb=data.getParcelableExtra("data");
        		i.setImageBitmap(bb);
        		
        	}
        }
        
        
        
        }


	@Override
	protected void onDestroy() {
		super.onDestroy(); 
		File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"zhycheng.jpg");
		if(f.exists())
		{
			f.delete();
		}
		
	} 
	
	
	
	
	





}

工程代码下载: TakePhoto.zip

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值