Java Android图形保存到数据库
我的应用程序有签名板。我使用DrawView和FrameLayout来显示它。如何在DB中保存签名。下面是我的代码。非常感谢您的任何建议或帮助 Summary.javaJava Android图形保存到数据库,java,android,android-canvas,paint,android-framelayout,Java,Android,Android Canvas,Paint,Android Framelayout,我的应用程序有签名板。我使用DrawView和FrameLayout来显示它。如何在DB中保存签名。下面是我的代码。非常感谢您的任何建议或帮助 Summary.java FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); DrawView drawView = new DrawView(this); drawView.requestFocus(); preview.addView(drawView); p
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
DrawView drawView = new DrawView(this);
drawView.requestFocus();
preview.addView(drawView);
public class DrawView extends View {
private static final float STROKE_WIDTH = 5f;
/** Need to track this so the dirty region can accommodate the stroke. **/
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private Paint paint = new Paint();
private Path path = new Path();
/** Optimizes painting by invalidating the smallest possible area. */
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public DrawView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
/** Erases the signature. */
public void clear() {
path.reset();
// Repaints the entire view.
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
// There is no end point yet, so don't waste cycles invalidating.
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
// Start tracking the dirty region.
resetDirtyRect(eventX, eventY);
// When the hardware tracks events faster than they are delivered,
// the
// event will contain a history of those skipped points.
int historySize = event.getHistorySize();
// Logger.debug("historySize : " + historySize);
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
// After replaying history, connect the line to the touch point.
// Logger.debug("eventX " + eventX);
// Logger.debug("eventY " + eventX);
//
// Logger.debug("lastTouchX " + lastTouchX);
// Logger.debug("lastTouchY " + lastTouchY);
//
// if (eventX == lastTouchX && eventY == lastTouchY) {
//
// path.addCircle(eventX, eventY, 20, Path.Direction.CW);
//
// }
path.lineTo(eventX, eventY);
break;
default:
// Logger.debug("Ignored touch event: " + event.toString());
return false;
}
// Include half the stroke width to avoid clipping.
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
/**
* Called when replaying history to ensure the dirty region includes all
* points.
*/
private void expandDirtyRect(float historicalX, float historicalY) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX;
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX;
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY;
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY;
}
}
/** Resets the dirty region when the motion event occurs. */
private void resetDirtyRect(float eventX, float eventY) {
// The lastTouchX and lastTouchY were set when the ACTION_DOWN
// motion event occurred.
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
DrawView.java
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
DrawView drawView = new DrawView(this);
drawView.requestFocus();
preview.addView(drawView);
public class DrawView extends View {
private static final float STROKE_WIDTH = 5f;
/** Need to track this so the dirty region can accommodate the stroke. **/
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private Paint paint = new Paint();
private Path path = new Path();
/** Optimizes painting by invalidating the smallest possible area. */
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public DrawView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
/** Erases the signature. */
public void clear() {
path.reset();
// Repaints the entire view.
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
// There is no end point yet, so don't waste cycles invalidating.
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
// Start tracking the dirty region.
resetDirtyRect(eventX, eventY);
// When the hardware tracks events faster than they are delivered,
// the
// event will contain a history of those skipped points.
int historySize = event.getHistorySize();
// Logger.debug("historySize : " + historySize);
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
// After replaying history, connect the line to the touch point.
// Logger.debug("eventX " + eventX);
// Logger.debug("eventY " + eventX);
//
// Logger.debug("lastTouchX " + lastTouchX);
// Logger.debug("lastTouchY " + lastTouchY);
//
// if (eventX == lastTouchX && eventY == lastTouchY) {
//
// path.addCircle(eventX, eventY, 20, Path.Direction.CW);
//
// }
path.lineTo(eventX, eventY);
break;
default:
// Logger.debug("Ignored touch event: " + event.toString());
return false;
}
// Include half the stroke width to avoid clipping.
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
/**
* Called when replaying history to ensure the dirty region includes all
* points.
*/
private void expandDirtyRect(float historicalX, float historicalY) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX;
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX;
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY;
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY;
}
}
/** Resets the dirty region when the motion event occurs. */
private void resetDirtyRect(float eventX, float eventY) {
// The lastTouchX and lastTouchY were set when the ACTION_DOWN
// motion event occurred.
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
公共类DrawView扩展视图{
专用静态最终浮动行程_宽度=5f;
/**需要对此进行跟踪,以便脏区域可以容纳笔划**/
专用静态最终浮动半冲程宽度=冲程宽度/2;
私人油漆=新油漆();
私有路径路径=新路径();
/**通过使尽可能小的区域无效来优化绘制*/
私人浮动lastTouchX;
私密接触;
private final RectF dirtyRect=new RectF();
公共绘图视图(上下文){
超级(上下文);
paint.setAntiAlias(真);
油漆。设置颜色(颜色。白色);
绘制.设置样式(绘制.样式.笔划);
绘制.设置行程连接(绘制.连接.圆形);
油漆。设置行程宽度(行程宽度);
}
/**删除签名*/
公共空间清除(){
path.reset();
//重新绘制整个视图。
使无效();
}
@凌驾
受保护的void onDraw(画布){
画布.绘制路径(路径,绘制);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float eventX=event.getX();
float eventY=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
path.moveTo(eventX,eventY);
lastTouchX=eventX;
lastTouchY=eventY;
//现在还没有终点,所以不要浪费周期使其失效。
返回true;
case MotionEvent.ACTION\u移动:
case MotionEvent.ACTION\u UP:
//开始跟踪脏区域。
resetDirtyRect(eventX,eventY);
//当硬件跟踪事件的速度快于事件的交付速度时,
//
//事件将包含这些跳过点的历史记录。
int historySize=event.getHistorySize();
//调试(“historySize:+historySize”);
for(int i=0;idirtyRect.right){
dirtyRect.right=historicalX;
}
if(历史目录底部){
dirtyRect.bottom=历史记录;
}
}
/**在运动事件发生时重置脏区域*/
私有void resetDirtyRect(float-eventX,float-eventY){
//lastTouchX和lastTouchY是在动作结束时设置的
//发生运动事件。
dirtyRect.left=Math.min(lastTouchX,eventX);
dirtyRect.right=Math.max(lastTouchX,eventX);
dirtyRect.top=Math.min(lastTouchY,eventY);
dirtyRect.bottom=Math.max(lastTouchY,eventY);
}
}
您可以像这样获得签名图像数据:
// in Activity code, maybe on a button click or onSaveInstanceState
// get a reference to the view
View drawView = getMyDrawViewFromSomewhere();
// get signature as a bitmap
drawView.buildDrawingCache();
Bitmap signature = drawView.getDrawingCache();
// convert to byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
signature.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// get your database
SQLiteDatabase db = getMySQLiteOpenHelperFromSomewhere().getWritableDatabase();
ContentValues values = new ContentValues();
// The column names here will depend on your schema, of course.
// This would work with:
// CREATE TABLE `signature_table` (`username` VARCHAR, `signature_image` BLOB);
values.put("username", getTheUserName());
values.put("signature_image", byteArray);
db.insert("signature_table", null, values);
然后将其保存到数据库,如下所示:
// in Activity code, maybe on a button click or onSaveInstanceState
// get a reference to the view
View drawView = getMyDrawViewFromSomewhere();
// get signature as a bitmap
drawView.buildDrawingCache();
Bitmap signature = drawView.getDrawingCache();
// convert to byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
signature.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// get your database
SQLiteDatabase db = getMySQLiteOpenHelperFromSomewhere().getWritableDatabase();
ContentValues values = new ContentValues();
// The column names here will depend on your schema, of course.
// This would work with:
// CREATE TABLE `signature_table` (`username` VARCHAR, `signature_image` BLOB);
values.put("username", getTheUserName());
values.put("signature_image", byteArray);
db.insert("signature_table", null, values);
别忘了清理:
signature.recycle();
drawView.destroyDrawingCache();
有关数据库的更多基础知识,如果尚未设置: