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