TakePhoto

package com.camera;

import java.io.IOException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	PickView pickview;
	Camera camera;
	Camera.Parameters parameters;
	Button save_pic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		surfaceView = (SurfaceView) findViewById(R.id.camera_preview);
		save_pic = (Button) findViewById(R.id.save_pic);
		pickview = (PickView) findViewById(R.id.pickview);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.setKeepScreenOn(true);
		surfaceView.setFocusable(true);
		surfaceView.setBackgroundColor(TRIM_MEMORY_BACKGROUND);
		surfaceHolder.addCallback(new Callback() {
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				camera.stopPreview();
				camera.release();
				camera = null;

			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				if (null == camera) {
					camera = Camera.open();
					try {
						camera.setPreviewDisplay(surfaceHolder);
						initCamera();
						camera.startPreview();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
				// 实现自动对焦
				camera.autoFocus(new AutoFocusCallback() {
					@Override
					public void onAutoFocus(boolean success, Camera camera) {
						if (success) {
							initCamera();// 实现相机的参数初始化
							camera.cancelAutoFocus();// 只有加上了这一句,才会自动对焦。
						}
					}

				});
			}

		});
		save_pic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bitmap bitmap = pickview.getBitmap();
				Log.v("", "width:" + bitmap.getWidth());
				Log.v("", "height:" + bitmap.getHeight());
			}
		});
	}

	// 相机参数的初始化设置
	private void initCamera() {
		parameters = camera.getParameters();
		// parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		parameters
				.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦
		setDispaly(parameters, camera);
		camera.setParameters(parameters);
		camera.startPreview();
		camera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上

	}

	// 控制图像的正确显示方向
	private void setDispaly(Camera.Parameters parameters, Camera camera) {
		if (Build.VERSION.SDK_INT >= 8) {
			setDisplayOrientation(camera, 90);
		} else {
			parameters.setRotation(90);
		}

	}

	// 实现的图像的正确显示
	private void setDisplayOrientation(Camera camera, int i) {
		Method downPolymorphic;
		try {
			downPolymorphic = camera.getClass().getMethod(
					"setDisplayOrientation", new Class[] { int.class });
			if (downPolymorphic != null) {
				downPolymorphic.invoke(camera, new Object[] { i });
			}
		} catch (Exception e) {
			Log.e("Came_e", "图像出错");
		}
	}

}

  

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class PickView extends View {
	private static final int PADDING = 10;
	private static final int SIZE = 100;
	private static final int STROKEWIDTH=2;
	private Paint paintBg;
	private Paint paint;
	private RectF ChooseArea = null;
	private RectF UnChooseAreaTop = null;
	private RectF UnChooseAreaBottom = null;
	private RectF UnChooseAreaRight = null;
	private RectF UnChooseAreaLeft = null;
	private int RectRadio = 0;
	private float ScreenWidth;
	private float ScreenHeight;

	public PickView(Context context, AttributeSet attrs) {
		super(context, attrs);

		ScreenWidth = getResources().getDisplayMetrics().widthPixels;
		ScreenHeight = getResources().getDisplayMetrics().heightPixels;
		RectRadio = (int) (getResources().getDisplayMetrics().density * SIZE);
		ChooseArea = new RectF();
		UnChooseAreaTop = new RectF();
		UnChooseAreaBottom = new RectF();
		UnChooseAreaRight = new RectF();
		UnChooseAreaLeft = new RectF();

		paintBg = new Paint();
		paintBg.setColor(Color.BLACK);
		paintBg.setAlpha(150);
		paintBg.setStyle(Style.FILL);

		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStrokeWidth(STROKEWIDTH);
		paint.setStyle(Style.STROKE);

		moveChooseArea(ScreenWidth / 2, ScreenHeight / 2);
		this.invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawRect(ChooseArea, paint);
		canvas.drawRect(UnChooseAreaTop, paintBg);
		canvas.drawRect(UnChooseAreaBottom, paintBg);
		canvas.drawRect(UnChooseAreaRight, paintBg);
		canvas.drawRect(UnChooseAreaLeft, paintBg);
	}

	public void moveChooseArea(float move_x, float move_y) {

		if (move_x > RectRadio + PADDING && move_y > RectRadio + PADDING
				&& move_x < (ScreenWidth - RectRadio - PADDING)
				&& move_y < (ScreenHeight - RectRadio - PADDING)) {
			float left = move_x - RectRadio;
			float top = move_y - RectRadio;
			float right = move_x + RectRadio;
			float bottom = move_y + RectRadio;
			ChooseArea.set(move_x - RectRadio, move_y - RectRadio, move_x
					+ RectRadio, move_y + RectRadio);

			// top
			UnChooseAreaTop.set((float) 0, (float) 0, ScreenWidth,
					(float) (top - STROKEWIDTH));
			// bottom
			UnChooseAreaBottom.set(0, bottom + STROKEWIDTH, ScreenWidth, ScreenHeight);
			// left
			UnChooseAreaRight.set(0, top - STROKEWIDTH, left - STROKEWIDTH, bottom + STROKEWIDTH);
			// right
			UnChooseAreaLeft.set(right + STROKEWIDTH, top - STROKEWIDTH, ScreenWidth, bottom + STROKEWIDTH);

			this.invalidate();
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			moveChooseArea(event.getX(), event.getY());
		}
		return true;
	}

	public Bitmap getBitmap() {
		int left = (int) ChooseArea.left;
		int right = (int) ChooseArea.right;
		int top = (int) ChooseArea.top;
		int bottom = (int) ChooseArea.bottom;
		return Bitmap.createBitmap(getDrawingCache(), left, top, right - left,
				bottom - top);
	}
}

  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <android.view.SurfaceView
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <com.camera.PickView
        android:id="@+id/pickview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:background="#00000000" />

    <Button
        android:id="@+id/save_pic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="save" />

</RelativeLayout>

  

时间: 2024-10-20 04:57:26

TakePhoto的相关文章

RDVECore来自锐动的无UI,高度抽象化API的视频编辑SDK--IOS版

1 编写目的 预期读者: 有视频编辑开发经验或者无经验的,打算或者正在使用"锐动IOS版RDVECore"的相关工程师. iOS软件工程师. 产品经理. QA 2 名词解释 分辨率:用于计算机视频处理的图像,以水平和垂直方向上所能显示的像素数来表示分辨率.常见视频分辨率的有1080P即1920x1080,720P即1080x720,640x480等. 帧率:每秒的帧数(fps)或者说帧率表示图形处理器处理场时每秒钟能够更新的次数. 码率: 数据传输时单位时间传送的数据位数,一般我们用的

系统内存耗尽的案例分析

近日遇到一个RAC节点hang导致节点被重启的问题,最后经过分析,发现在系统运行一段时间后,系统内存就会耗尽,原本256G的内存,最后只剩几百M. 1. 问题时间段的TOP输出可以看到,内存只剩7G,而分析内存问题,TOP输出是不够的,一般情况下,Database的SGA和PGA是内存使用大户,所以,在TOP很难发现谁是使用内存最多的. 除非某些进程内存使用的格外明显 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Linux OSWbb v7.3.

关于android:getSlotFromBufferLocked的问题

今天写了个调用摄像头之后将照片贴在屏幕上面的demo,出现了这个问题.代码如下 package android.com.java.choosepictest; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.

每天进步一点----- 开启照相机,剪切相片

1 package com.example.choosepictest; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.graphics.Bitmap; 10 import android.

Android--多媒体操作

---恢复内容开始--- 1.拍照,这里直接上代码,看注释就好 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); takePhoto = (Button) findViewById(R.id.take_photo); photo = (ImageView) findViewById(R.id.photo); tak

Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等

仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 微信 下载地址 : 微信上传图片源码 很多网友不知道怎么获取图片路径,这里贴出来: String path = Bimp.tempSelectBitmap.get(position).getImagePath(); //部分代码如下 [java] view plain copy package co

Android调用系统相机拍照保存照片很小解决方案

保存图片小的一般操作步骤: 1. 调用系统相机 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); 2. 保存照片 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {  // TODO Auto-generated method s

Cocos2d-x 3.x 选择并编辑头像功能(Android、IOS双平台实现)

大连游戏行业不是太发达,最后选择在一家应用外包公司工作,在工作和业余学习过程中积累了一点微不住道的经验,希望分享给热爱游戏的小伙伴们. 在应用开发过程中会常常有用户上传头像的功能,在网上找了N多资料发现没有人具体介绍过该用cocos2d-x实现.这篇文章就来介绍一下如何在Android和IOS平台上实现该功能. 欢迎热爱游戏开发的小伙伴骚扰: 电邮:[email protected] 企鹅:601322806 先传一张完成后的图片一饱眼福: 直接上代码: 头文件 ImagePicker.h /*

[Android] 拍照、截图、保存并显示在ImageView控件中

最近在做Android的项目,其中部分涉及到图像处理的内容.这里先讲述如何调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买了本<Android第一行代码 著:郭霖>,参照里面的内容完成(推荐该书,前面的布局及应用非常不错).网上这类资料非常多,作者仅仅分享给初学者同时在线记录些内容,希望对大家有所帮助. 首先,设置activity_main.xml为LinearLayout布局且 android:orientation=&q