努力制作带有位图的画布并在其上绘制

时间:2015-04-20 14:56:57

标签: android canvas

到目前为止,这是我的代码。 编辑:添加完整代码。

public void enterPreviewMode(byte[] pictureData) {

    //Stop preview and enter review mode
    previewPic = true;
    cam.stopPreview();

    //Find buttons and make them visible/invisible
    Button takePicB = (Button)findViewById(R.id.takePicButton);
    Button exitPreviewB = (Button)findViewById(R.id.xButton);
    exitPreviewB.setVisibility(View.VISIBLE);
    exitPreviewB.setEnabled(true);
    takePicB.setVisibility(View.INVISIBLE);
    takePicB.setEnabled(false);

    //Decode byte array into bmp image, then copy bmp to make drawable for canvas
    Bitmap bmp = BitmapFactory.decodeByteArray(pictureData, 0, pictureData.length);
    Bitmap drawableBmp = bmp.copy(Bitmap.Config.ARGB_8888, true);

    //Find ImageView, make it fit the screen, set the image (and rotate the image), set visible
    ImageView image = (ImageView)findViewById(R.id.picPreview);
    image.setScaleType(ImageView.ScaleType.FIT_XY);
    image.setImageBitmap(rb.RotateBitmap(drawableBmp,90));
    image.setVisibility(View.VISIBLE);

    //Create canvas using drawableBmp, currently fills in black for testing purposes
    Canvas editableZone = new Canvas(drawableBmp);
    editableZone.drawColor(Color.BLACK);
}

这是我尝试拍照并在上面画画(好吧,现在只需在它上面放一个矩形,宝贝步骤)。可悲的是,这段代码没有显示在图像上。尝试阅读一些针对我的具体情况的教程,似乎无法识别出错的地方。

3 个答案:

答案 0 :(得分:0)

我认为此代码存在问题:editableZone.drawRect(100f,100f,100f,100f,p), 对于drawRect(浮动左,浮顶,浮右,浮底,油漆); 你可以尝试这个editableZone.drawRect(100f,100f,200f,200f,p)

祝你好运

答案 1 :(得分:0)

    I using the follow code and it can draw the rect on the bitmap 

public Bitmap getRectBitmap(){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.new_home_default_editor_portrait);
            Bitmap copyBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            int w = copyBitmap.getWidth();
            int h = copyBitmap.getHeight();

            Bitmap tem = Bitmap.createBitmap(copyBitmap, 0, 0, w, h);
            Canvas canvas = new Canvas(tem);
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStrokeWidth(10f);
            canvas.drawRect(10f, 10f, 200f, 200f, paint);

            return tem;
        }

答案 2 :(得分:0)

想出来。问题是我的rotateBMP函数创建了一个新的bmp,​​oops。