在图片上绘制线并保存

我们的做法是把一张图片当作位图,在这张图片上绘制线。
具体实现如下:

  • 布局文件(3个控件)

    
  • 代码文件

public class MainActivity extends Activity implements OnClickListener,
        OnTouchListener {

    private Button btn, btn_save;
    private ImageView iv;
    private float downx, downy, upx, upy;
    private Canvas cv;
    private Bitmap alter;
    private Paint paint;
    FileOutputStream fos = null;

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

    private void iviview() {
        btn = (Button) findViewById(R.id.button1);
        btn_save = (Button) findViewById(R.id.button2);
        iv = (ImageView) findViewById(R.id.imageView);
        btn.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        iv.setOnTouchListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btn) {
            // 利用意图调用内置的画廊
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 0);
        } else if (v == btn_save) {
            if (alter != null) {
                // 往SD卡里面保存图片
                // "/aa"是文件名
                File f = new File(Environment.getExternalStorageDirectory()
                        .getAbsoluteFile() + "/aa");
                try {
                    // 创建一个新文件
                    f.createNewFile();
                    fos = new FileOutputStream(f);
                    // format格式 quality像素 stream输出流
                    alter.compress(CompressFormat.JPEG, 100, fos);
                    Toast.makeText(this, "文件保存成功", 1).show();
                } catch (Exception e) {
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        // 将你在画廊中选中的图片信息返回,取出,并且以合适的尺寸绘制在IV
        if (resultCode == RESULT_OK) {
            Uri imageUri = data.getData();

            Display display = getWindowManager().getDefaultDisplay();
            int wd = display.getWidth();
            int hd = display.getHeight();

            // 我们要获得图片尺寸,,,如果你直接把这幅图加载到内存的话,如果图很大,可能导致内存溢出。
            // 加载图片的尺寸而并非是图片本身
            BitmapFactory.Options bo = new BitmapFactory.Options();
            bo.inJustDecodeBounds = true;
            try {
                Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(imageUri), null, bo);

                int widthRadio = (int) Math.ceil((float) bo.outWidth
                        / (float) wd);
                int heightRadio = (int) Math.ceil((float) bo.outHeight
                        / (float) hd);
                if (widthRadio > 1 || heightRadio > 1) {
                    // 表示图片尺寸大于显示尺寸要进行处理
                    if (widthRadio > heightRadio) {
                        bo.inSampleSize = widthRadio;
                        // inSampleSize 成员变量,可以根据图形的实际宽高 和期望宽高来计算一个合适的尺寸。
                    } else {
                        bo.inSampleSize = heightRadio;
                    }
                }

                // 处理完尺寸之后,我要加载图片
                bo.inJustDecodeBounds = false;
                bmp = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(imageUri), null, bo);

                // 把得到的图片的宽高当作位图
                alter = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
                        bmp.getConfig());
                cv = new Canvas(alter);
                paint = new Paint();
                paint.setColor(Color.BLUE);
                paint.setStrokeWidth(3);
                // 开始画
                cv.drawBitmap(bmp, 0, 0, paint);
                iv.setImageBitmap(alter);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent m) {
        int action = m.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:

            downx = m.getX();
            downy = m.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            upx = m.getX();
            upy = m.getY();
            cv.drawLine(downx, downy, upx, upy, paint);
            iv.invalidate();
            downx = upx;
            downy = upy;
            break;
        case MotionEvent.ACTION_UP:
            upx = m.getX();
            upy = m.getY();
            cv.drawLine(downx, downy, upx, upy, paint);
            iv.invalidate();
            break;
        default:
            break;
        }
        return true;
    }
}


  • 操作

photo.jpg

你可能感兴趣的:(在图片上绘制线并保存)