实现主流软件的调用系统相机拍照 取得照片 并进行加工

无意间看到@sdliuxiaofei的帖子
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=166200

由于前段时间做相机应用 自己写的相机 结果是各种机型不兼容(没有camera360的技术和时间),搞的头疼

看到楼主的帖子才知道这才是"王道",简单又实用.

不过@sdliuxiaofei的例子还是有点小问题的,这个例子一般拍下来的是相片的缩略图,压缩到内存卡不到20k的(中兴V880测试是10k.可到省流量.)这样的效果是不能让用户满意的

调查了腾讯的相机后发现了他们的都是很好质量的照片,还支持压缩功能,具体自己测试吧.于是做了这个程序,几乎和腾讯他们实现的方法类似了,腾讯拍照后处理了各种效果就不一一介绍了,

我只是加了一个加水印功能.

总之,感觉如果不是做camera360这样的应用,调用相机已经很够用了,腾讯他们也是这么做的.再次感谢@sdliuxiaofei

主Activity就一个按钮.
package com.zte;

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

import android.R.integer;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ZTEActivity extends Activity {
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zte_mainmenu);

		Button buttoncamera = (Button) findViewById(R.id.button1);
		buttoncamera.setText("拍照");
		buttoncamera.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(ZTEActivity.this,
						HandlePicActivity.class);
				startActivity(intent);

			}
		});
	}

}



拍照的Activity:

package com.zte;

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

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class HandlePicActivity extends Activity {
	private static boolean openCamera = true;
	private String localTempImgFileName = "temp";
	private final int CAPTURE_CODE = 100;
	private ImageView iv;
	private String TAG;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		if (openCamera) {
			File dir = new File(Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/DCIM/");// 设置临时文件的存放目录
			if (!dir.exists()) {
				dir.mkdir();
			}
			Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			File f = new File(dir, localTempImgFileName);
			Uri u = Uri.fromFile(f);
			intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
			startActivityForResult(intent, CAPTURE_CODE);
			openCamera = false;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!openCamera) {
		setContentView(R.layout.zte_mainmenu);
		iv = (ImageView) findViewById(R.id.picture);
		Button button = (Button) findViewById(R.id.button1);
		button.setText("加水印");
		button.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Thread thread = new Thread(new ThreadGetData());// 避免阻塞UI线程
				thread.start();

				finish();
			}

		 });
		}

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    openCamera=true;
		try {
			if (resultCode != RESULT_OK) {
				return;
			} else if (requestCode == CAPTURE_CODE && resultCode == RESULT_OK) {
				   
				File dir = new File(Environment.getExternalStorageDirectory()
						.getAbsolutePath() + "/DCIM/");// 设置存放目录
				File f = new File(dir.getAbsoluteFile(), localTempImgFileName);
				
				BitmapFactory.Options opts = new BitmapFactory.Options();//获取缩略图显示到屏幕 
				opts.inSampleSize = 5;
				Bitmap cbitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),opts);
				iv.setImageBitmap(cbitmap);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "选择图片错误,图片只能为jpg格式", Toast.LENGTH_SHORT)
					.show();
		}

		

	}

	class ThreadGetData implements Runnable {

		public void run() {

			try {

				File dir = new File(Environment.getExternalStorageDirectory()
						.getAbsolutePath() + "/DCIM/");// 设置存放目录
				// if (!dir.exists()) {
				// dir.mkdir();
				// }

				File f = new File(dir.getAbsoluteFile(), localTempImgFileName);
//				Uri u = Uri.parse(android.provider.MediaStore.Images.Media
//						.insertImage(getContentResolver(), f.getAbsolutePath(),
//								null, null));
//				Uri u = Uri.parse(f.getAbsolutePath());
//				Bitmap cbitmap = MediaStore.Images.Media.getBitmap(// 从URI读取图像到bitmap以处理
//						HandlePicActivity.this.getContentResolver(), u);
				Bitmap cbitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
				String picname = dir.getAbsolutePath() + "/" + "ZTEPIC_"
						+ System.currentTimeMillis() + ".jpg";
				File file = new File(picname);
				FileOutputStream outStream = new FileOutputStream(file);
				String zte = "ZTE";
				Bitmap zteBitmap = Bitmap.createBitmap(cbitmap.getWidth(),
						cbitmap.getHeight(), Bitmap.Config.ARGB_8888);// 做一个bitmap在上面加水印和原来的图片
				Canvas canvas = new Canvas(zteBitmap);
				Paint p = new Paint();
				p.setColor(Color.RED);
				p.setTextSize(80);
				canvas.drawBitmap(cbitmap, 0, 0, p);
				canvas.drawText(zte, cbitmap.getWidth() - 200,
						cbitmap.getHeight() - 60, p);
				zteBitmap.compress(CompressFormat.JPEG, 85, outStream);
				f.delete();
				Toast.makeText(HandlePicActivity.this, "存储完成", 100).show();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

你可能感兴趣的:(软件)