1.自定义一个imageview用来设定surfaceview上的特定区域。
??
public class DrawImageView extends ImageView { private Paint paint; private int mFristPointX = 100, mFristPointY = 200; private int mSecondPointX = 400, mSecondPointY = 500; private boolean isFirstDown = true; private int mOldX = 0, mOldY = 0; public DrawImageView(Context context) { super(context); // TODO Auto-generated constructor stub init(); } public DrawImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2.5f); paint.setAlpha(100); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawRect(new Rect(getmFristPointX(), getmFristPointY(), getmSecondPointX(), getmSecondPointY()), paint); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() != MotionEvent.ACTION_UP) { int x = (int) event.getX(); int y = (int) event.getY(); Rect mRect = new Rect(getmFristPointX(), getmFristPointY(), getmSecondPointX(), getmSecondPointY()); if (mRect.contains(x, y)) { if (isFirstDown) { mOldX = x; mOldY = y; isFirstDown = false; } else { int mXDis = x - mOldX; int mYDis = y - mOldY; mOldX = x; mOldY = y; ReSetVaue(mXDis, mYDis); } } } else { isFirstDown = true; } return true; } public void setValue(int x, int y) { setmFristPointX(x - 50); setmFristPointY(y - 50); setmSecondPointX(x + 50); setmSecondPointY(y + 50); invalidate(); } public void ReSetVaue(int xDis, int yDis) { setmFristPointX(getmFristPointX() + xDis); setmFristPointY(getmFristPointY() + yDis); setmSecondPointX(getmFristPointX() + 100); setmSecondPointY(getmFristPointY() + 100); invalidate(); } public int getmFristPointX() { return mFristPointX; } public void setmFristPointX(int mFristPointX) { this.mFristPointX = mFristPointX; } public int getmFristPointY() { return mFristPointY; } public void setmFristPointY(int mFristPointY) { this.mFristPointY = mFristPointY; } public int getmSecondPointX() { return mSecondPointX; } public void setmSecondPointX(int mSecondPointX) { this.mSecondPointX = mSecondPointX; } public int getmSecondPointY() { return mSecondPointY; } public void setmSecondPointY(int mSeconPointY) { this.mSecondPointY = mSeconPointY; }}
?2.定义xml布局。
?
??
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <SurfaceView android:id="@+id/open_camera" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="15dp" /> <test.camera.cut.DrawImageView android:id="@+id/draw_rect" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="15dp" /> </FrameLayout>
?3.在surfaceChanged中设置参数开始画区域:
?
?
DrawImageView.onDraw(new Canvas()); DrawImageView.setValue(mFirstPointX, mFirstPointY);//左上角点的值
?4.在camera拍照部分进行压缩裁剪。
private Bitmap mBitmap; private Camera.PictureCallback takePictureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // TODO Auto-generated method stub if (data != null) { mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); camera.stopPreview(); } Matrix matrix = new Matrix(); matrix.setRotate(90); Bitmap rotaBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false); getSurfaceSize(); Bitmap sizeBitmap = Bitmap.createScaledBitmap(rotaBitmap, SurView_Width, SurView_Height, true); SaveBitmap(sizeBitmap); mFirstPointX = mDrawImageView.getmFristPointX() + 50; mFirstPointY = mDrawImageView.getmFristPointY() + 50; Bitmap newBitmap = Bitmap.createBitmap(sizeBitmap, mFirstPointX - 50, mFirstPointY - 50, 100, 100); if (newBitmap != null) { mTestImageView.setImageBitmap(newBitmap); } camera.startPreview(); } }; private void getSurfaceSize(){ SurView_Width = mSurfaceView.getWidth(); SurView_Height = mSurfaceView.getHeight(); } private void SaveBitmap(Bitmap bitmap) { File mpicture = new File(mPath + File.separator + "pic.jpg"); try { mpicture.createNewFile(); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(mpicture)); bitmap.compress(CompressFormat.JPEG, 75, outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }
?
?
参考文献: ?http://blog.csdn.net/yanzi1225627/article/details/8580034
?