0
回答
我想要拍照并且在照片上涂鸦~
华为云数据库免费试用   

可是遇到问题 我该如何保存 onDraw 方法中的canvas 到bitmap 呢  下面贴代码 拜托大神帮忙

都是在网上找的代码 改了改 就是不行啊  代码写的比较乱 菜鸟么。。

package com.viziner.cleaninghouse;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.viziner.cleaninghouse.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraActivity extends Activity {

	private String flagstr = "";
	private Bitmap bitmap;
	private MyView handWrite = null;
	private byte[] bitmapByte;
	private Bitmap obitmap;
	static int code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera);

		ActivityStackControlUtil.add(this);
		flagstr = getIntent().getStringExtra("flag");
		handWrite = (MyView) findViewById(R.id.camera_img);
		setListener();
		code = 0;
	}

	private void setListener() {
		int[] ids = { R.id.camera_cam, R.id.camera_draw, R.id.camera_del,
				R.id.camera_btn4 };
		for (int i = 0; i < ids.length; i++) {
			findViewById(ids[i]).setOnClickListener(onclick);
		}
	}

	private OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.camera_cam:
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 0);
				break;
			case R.id.camera_draw:

				code = 1;

				break;
			case R.id.camera_del:
				if (!(obitmap.equals(bitmap))) {
				}

				break;
			case R.id.camera_btn4:
				
				if (bitmap != null) {
					handWrite.setImge();
					code = 0;
					ByteArrayOutputStream baos = new ByteArrayOutputStream();

					obitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
					bitmapByte = baos.toByteArray();

					Intent intent2 = new Intent();
					intent2.setClass(getApplicationContext(),
							ProblemActivity.class);
					intent2.putExtra("image", bitmapByte);
					intent2.putExtra("flag", flagstr);
					startActivity(intent2);
				} else {
					Toast.makeText(getApplicationContext(),
							"Sorry!No image can be selected!",
							Toast.LENGTH_LONG).show();
				}

				break;

			default:
				break;
			}
		}
	};

	/**
	 * 璋冨嵈鎽勫儚澶磋嚜甯︽媿鐓у姛鑳�
	 * */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == Activity.RESULT_OK) {
			obitmap = null;
			String sdStatus = Environment.getExternalStorageState();

			// 妫�祴sd鏄惁鍙敤
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
				return;
			}
			Bundle bundle = data.getExtras();
			// 鑾峰彇鐩告満杩斿洖鐨勬暟鎹紝骞惰浆鎹负Bitmap鍥剧墖鏍煎紡
			bitmap = (Bitmap) bundle.get("data");

			FileOutputStream fos = null;
			File file = new File("/sdcard/DCIM/myImage/");
			// 鍒涘缓鏂囦欢澶癸紝鍚嶇О涓簃yimage
			file.mkdirs();

			// 鐓х墖鐨勫懡鍚嶏紝鐩爣鏂囦欢澶逛笅锛屼互褰撳墠鏃堕棿鏁板瓧涓蹭负鍚嶇О锛屽嵆鍙‘淇濇瘡寮犵収鐗囧悕绉颁笉鐩稿悓銆�
			String str = null;
			Date date = null;
			// 鑾峰彇褰撳墠鏃堕棿锛岃繘涓�杞寲涓哄瓧绗︿覆
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			date = new Date();
			str = format.format(date);
			String fileName = "/sdcard//DCIM/myImage/" + str + ".jpg";
			System.out.println("fileName = " + fileName);

			try {
				fos = new FileOutputStream(fileName);
				// 鎶婃暟鎹啓鍏ユ枃浠�
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

				File f = new File(fileName);
				FileInputStream inputStream = new FileInputStream(f);
				obitmap = BitmapFactory.decodeStream(inputStream).copy(
						Bitmap.Config.ARGB_8888, true);

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				if (data != null) {
					if (requestCode == 0) {
						handWrite.setOriginalBitmap(obitmap);
						System.out.println("成功======" + obitmap.getWidth()
								+ "--------" + obitmap.getHeight());
					}
				}
			}
		}
	}

//	static Handler hh;
//
//	public void handerl() {
//		hh = new Handler() {
//
//			@Override
//			public void handleMessage(Message msg) {
//				int what = msg.what;
//				if (what == 3) {
//					handWrite.setImge();
//				}
//				super.handleMessage(msg);
//			}
//
//		};
//	}

}
package com.viziner.cleaninghouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
	private Paint paint = null;
	private Bitmap originalBitmap = null;
	private Bitmap new1Bitmap = null;
	private Bitmap new2Bitmap = null;
	private float startX, startY;
	private float stopX, stopY;
	private boolean isMove = false;
	private boolean isClear = false;
	private int color = Color.RED;
	private float strokeWidth = 5.0f;
	private List<PointF> points = new ArrayList<PointF>();
	private Map<Object, Object>[] maps;
	private Rect src = new Rect();
	private Rect dst = new Rect();

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

		
	}

	public void setOriginalBitmap(Bitmap originalBitmap) {

		this.originalBitmap = originalBitmap;
		new1Bitmap = Bitmap.createBitmap(originalBitmap);
		if (CameraActivity.code == 1) {
			setDrawingCacheEnabled(true);
		}
		invalidate();
		Log.i("RG", "new1Bitmap--->>>" + new1Bitmap);
	}

	public void clear() {
		isClear = true;
		new2Bitmap = Bitmap.createBitmap(originalBitmap);
		invalidate();
	}

	Bitmap saveImage = null;

	public Bitmap saveImage() {
		if (saveImage == null) {
			return null;
		}
		return saveImage;
	}

	public void setImge() {
		saveImage = null;
	}

	public void setstyle(float strokeWidth) {
		this.strokeWidth = strokeWidth;
	}

	/**
	 * OnDraw鏂规硶锛�
	 * 
	 * @return
	 * */
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (new1Bitmap != null && originalBitmap != null) {
			src.left = 0;
			src.top = 0;
			src.right = 160;
			src.bottom = 120;

			dst.left = 0;
			dst.top = 0;
			dst.right = 760;
			dst.bottom = 520;

			canvas.drawBitmap(new1Bitmap, src, dst, null);
			
			paint = new Paint();
			paint.setColor(color);
			paint.setStrokeWidth(strokeWidth);
			paint.setStyle(Style.STROKE);
			paint.setAntiAlias(true);
			for (int i = 0; i < points.size() - 1; i++) {

				PointF startPoint = points.get(i);
				PointF endPoint = points.get(i + 1);
				canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y,
						paint);
				
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (CameraActivity.code == 1) {
			
			if (event.getAction() == MotionEvent.ACTION_DOWN) {

				points.clear();
				points.add(new PointF(event.getX(), event.getY()));
				isMove = false;
				invalidate();
				return true;

			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {

				points.add(new PointF(event.getX(), event.getY()));
				isMove = true;
				invalidate();
				return true;

			} else if (event.getAction() == MotionEvent.ACTION_UP) {

				points.add(new PointF(event.getX(), event.getY()));
				isMove = false;
				invalidate();
				return true;

			}
		}

		return super.onTouchEvent(event);
	}

	// public void SaveBitmap(Bitmap bmp)
	// {
	// Bitmap bitmap = Bitmap.createBitmap(800, 600, Config.ARGB_8888);
	// Canvas canvas = new Canvas(bitmap);
	// //淇濆瓨鍏ㄩ儴鍥惧眰
	// canvas.save(Canvas.ALL_SAVE_FLAG);
	// canvas.restore();
	// //瀛樺偍璺緞
	// File file = new File("/sdcard/song/");
	// if(!file.exists())
	// file.mkdirs();
	// try {
	// FileOutputStream fileOutputStream = new FileOutputStream(file.getPath() +
	// "/xuanzhuan.jpg");
	// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
	// fileOutputStream.close();
	// System.out.println("saveBmp is here");
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

}


举报
泽丨Zeus
发帖于5年前 0回/470阅
顶部