如何将画布绘图保存为图像?

时间:2012-04-11 06:36:53

标签: android image-processing canvas surfaceview

在我的Android应用程序中我将绘图保存为图像。我能够保存文件,但它不能完全保存100%,即一些内容缺少空格即将到来。函数在下面是我通过onclick调用的。请帮我看看如何保存完美的绘图 屏幕截图请访问以下链接: http://www.flickr.com/photos/77093196@N03/7066979339http://www.flickr.com/photos/77093196@N03/7066979341

DigitalSignatureActivity.java

private DrawingSurface drawingSurface;

    // To save file as Image
        public void saveDrawing(View v) throws IOException {

            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MySignatures");
             Bitmap nBitmap = drawingSurface.getBitmap();
            try {
                if (!mediaStorageDir.exists()) {
                    mediaStorageDir.mkdirs();
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                        .format(new Date());
                File mediaFile = new File(mediaStorageDir.getPath()
                        + File.separator + "SIGN_" + timeStamp + ".png");
             FileOutputStream out = new FileOutputStream(mediaFile);

            nBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
                Toast.makeText(this, "Signature saved  to " + mediaFile,
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Not saved",
                        Toast.LENGTH_SHORT).show();
            }

        }

DrawingSurface.java

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
//some code
}

1 个答案:

答案 0 :(得分:1)

我通过设置canvas.drawColor(Color.WHITE,PorterDuff.Mode.DARKEN)来解决;在DrawingSurface.java类