如何将一个位图叠加到另一个位图并保存在SD卡上

时间:2013-04-02 06:35:28

标签: android bitmap android-canvas

我正在开发图像编辑器,我使用自定义类来绘制位图,这是我的代码..

private void settingBitmapToDraw() {
    // TODO Auto-generated method stub

    resultBitmap=Bitmap.createScaledBitmap(resultBitmap, WIDTH, HEIGHT, true);
    Matrix matrix=new Matrix();
    matrix.setRotate(TO_DEGREE);
    tempBitmap=Bitmap.createBitmap(resultBitmap, 0, 0, WIDTH, HEIGHT, 
    matrix,  true);

    bitmap=Bitmap.createBitmap(WIDTH, HEIGHT, tempBitmap.getConfig());
    canvas=new Canvas(bitmap);

    invalidate();

}

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    canvas.drawBitmap(tempBitmap, xAxis, yAxis, paint);

    if(overlayBitmap!=null)
    {
        canvas.drawBitmap(overlayBitmap, xAxis+50, yAxis+50, paint);
    }
}

public void overlayImage() {
    // TODO Auto-generated method stub

    ImageProcessing.SAVE_STATUS=false;
    overlayBitmap=BitmapFactory.decodeResource(getResources(),
            R.drawable.add_image);
    invalidate();

}

位图在画布上绘制,但保存时会保存为黑色图像。 这是我的保存位图代码...

    try {
FileOutputStream fos=new FileOutputStream(file);
updatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    SAVE_STATUS=true;
saveDialog.dismiss();
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
    e.printStackTrace();
}

更新的位图由'bitmap'

初始化
public Bitmap getUpdatedImage() {
    // TODO Auto-generated method stub
    //width=2048 height=1232

    Bitmap updatedBitmap=Bitmap.createScaledBitmap(bitmap, 
            orgWidth, orgHeight, true);
    return updatedBitmap;

}

////////////// 我的ImageProcessing.java类有代码......

if(!SAVE_STATUS)
{
updatedBitmap=ip_DrawingClass.getUpdatedImage();
if(updatedBitmap!=null)
{
    file=new File(file_root);
    saveDialog.show();
}
else
{
    Toast.makeText(ImageProcessing.this,
            "file can't saved...",  Toast.LENGTH_SHORT).show();
}
}   

和saveDialog有保存按钮,其中包含用于保存上面写入的图像的代码。

///////////////////////////


    @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    float xAxis,yAxis;

    switch(event.getAction()&MotionEvent.ACTION_MASK)
    {
    case MotionEvent.ACTION_DOWN:
        MODE="DRAG";
        startX=event.getX();
        startY=event.getY();
        if(ImageProcessing.drawLineStatus)
            path.moveTo(startX, startY);
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        MODE="ZOOM";
        oldDist=this.findDistanceXY(event);
        break;
    case MotionEvent.ACTION_MOVE:
        if(MODE=="ZOOM")
        {
            newDist=findDistanceXY(event);
            if(newDist>oldDist)
            {
                this.applyZooming("plus");
            }
            else
            {
                this.applyZooming("minus");
            }
            oldDist=newDist;
        }
        else if(MODE=="DRAG")
        {
            xAxis=event.getX();
            yAxis=event.getY();
            if(!lock_status)
            {
                this.translateImage(xAxis,yAxis,startX,startY);
            }
            else
            {
               this.translateTextOnBitmap
                                         (xAxis,yAxis,startX,startY);
            }

            if(ImageProcessing.drawLineStatus)
            {
                path.lineTo(xAxis, yAxis);
                this.drawLinesOnBitmap();
            }
            else
                draw_line=false;
        }
        break;
    case MotionEvent.ACTION_POINTER_UP:
        MODE="DRAG";
        this.setZoomBoxXY();
        break;
    case MotionEvent.ACTION_UP:
        this.set_XY_Axis();
        break;
    }

    return true;
}

图片翻译在这里..

 public void translateImage(float x, float y, float startX, float startY) {
// TODO Auto-generated method stub

    if((startX>=imageAtX&&startX<=(imageAtX+imageW))&&
                (startY>=imageAtY&&startY<=(imageAtY+imageH)))
    {
        if(imagePortionSelected||crop_status)
        {
            if((startX>=selectorAtX&&startX<=(selectorAtX+ZOOM))
                               &&(startY>=selectorAtY&&startY<=(selectorAtY+ZOOM)))
            {
                left=(int)(x-(startX-selectorAtX));
                top=(int)(y-(startY-selectorAtY));
                right=left+ZOOM;
                bottom=top+ZOOM;
                checkForValidityOfPara();
            }
            rect.set(left, top, right, bottom);
        }
        else
        {
            xAxis=x-(startX-imageAtX);
            yAxis=y-(startY-imageAtY); 
        }
    }

    invalidate();
}

4 个答案:

答案 0 :(得分:2)

对于组合2位图,请使用以下方法

public Bitmap combineImages(Bitmap frame, Bitmap image) {

    Bitmap cs = null;
    Bitmap rs = null;

    rs = Bitmap.createScaledBitmap(frame, image.getWidth(),
            image.getHeight(), true);

    cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
            Bitmap.Config.RGB_565);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(image, 0, 0, null);
    comboImage.drawBitmap(rs, 0, 0, null);

    if (rs != null) {
        rs.recycle();
        rs = null;
    }
    Runtime.getRuntime().gc();

    return cs;
}

并且可以在任何地方使用代码保存组合图像...

 Bitmap outBmp = combineImages(bmp1, bmp2);

    imageFileFolder = new File(Environment.getExternalStorageDirectory(),
            "FOLDER_PHOTOS");
    imageFileFolder.mkdir();
    FileOutputStream out1 = null;

    imageFileName = new File(imageFileFolder, "file_name.jpg");

        out1 = new FileOutputStream(imageFileName);
        outBmp.compress(Bitmap.CompressFormat.JPEG, 100, out1);
        out1.flush();
        out1.close();

答案 1 :(得分:1)

使用此代码。您必须将图纸布局保存为图像。希望下面的代码可以帮助你。

Bitmap bm;
    DrawingLayout.setDrawingCacheEnabled(true);
    bm = DrawingLayout.getDrawingCache();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    //String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/drawn_image";
    String path = getFilesDir() .getAbsolutePath();
    boolean exists = (new File(path)).exists();
    /*if (!exists) {
        new File(path).mkdirs();
    }*/
    OutputStream outStream = null;
    File file = new File(path, "drawn_image" + ".PNG");
    try {
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);            
        outStream.flush();
        outStream.close();               

    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

答案 2 :(得分:1)

您没有使用更新的位图。在保存之前,在更新的位图上绘制第一个位图和覆盖位图。将 getUpdatedImage()更改为此 -

protected void getUpdatedImage() {

int width = tempBitmap.getWidth();
int height  = tempBitmap.getHeight();
Bitmap updatedBitmap=Bitmap.createScaledBitmap(bitmap, 
        width, height, true);
Canvas canvas  = new Canvas(updatedBitmap);

canvas.drawBitmap(tempBitmap, xAxis, yAxis, paint);

if(overlayBitmap!=null)
{
    canvas.drawBitmap(overlayBitmap, xAxis+50, yAxis+50, paint);
}

//now save the updated bitmap
return updatedBitmap;
}

答案 3 :(得分:0)

保存位图试试这个......-

 public void savebitmap(final Bitmap bitmap)
{
    AlertDialog.Builder alert = new AlertDialog.Builder(Work.this);
alert.setMessage("File name :"); //get file name from user
    input = new EditText(Work.this);
    input.setLayoutParams(new LayoutParams(100,50));
    alert.setView(input);
    alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String _mNameValue = input.getText().toString();
            try
            {
                File fn=new File("/sdcard/"+_mNameValue+".png");
                FileOutputStream out=new FileOutputStream(fn);
                Toast.makeText(getApplicationContext(), "In Save",Toast.LENGTH_SHORT).show();
                bitmap.compress(Bitmap.CompressFormat.PNG, 90,out);
                out.flush();
                out.close();    
                Toast.makeText(getApplicationContext(), "File is Saved in   "+fn, Toast.LENGTH_SHORT).show();
            }
            catch(Exception e){
                e.printStackTrace();        
            }
        }
    });
    alert.show();
}

这在我的位置完美运作。

ontouch事件 -

    paint = new Paint();
                    paint.setAntiAlias(true);
                    paint.setColor(getResources().getColor(R.color.Yellow)) ;
                    paint.setAlpha(opacity);

                        _ImageView.setAdjustViewBounds(true);
                          _ImageView.setImageBitmap(_MutableImage1);
                          canvas1 = new Canvas(_MutableImage1);
                          canvas1.drawRect(event.getX()-10,event.getY()-10,event.getX()+10,event.getY()+10, paint);
                          bitmap1=_MutableImage1.copy(Bitmap.Config.ARGB_4444,false);