如何覆盖一个位图到另一个节省SD卡 [英] how to overlay one bitmap to another and save on sdcard
问题描述
我正在开发的图像编辑器,并使用自定义类绘制位图IM,这里是我的code ..
私人无效settingBitmapToDraw(){
// TODO自动生成方法存根 resultBitmap = Bitmap.createScaledBitmap(resultBitmap,宽度,高度,真);
字模=新的Matrix();
matrix.setRotate(TO_DEGREE);
tempBitmap = Bitmap.createBitmap(resultBitmap,0,0,宽度,高度,
矩阵,真); 位图= Bitmap.createBitmap(宽度,高度,tempBitmap.getConfig());
帆布=新的Canvas(位图); 无效();}保护无效的onDraw(帆布油画){
// TODO自动生成方法存根
super.onDraw(画布); canvas.drawBitmap(tempBitmap,X轴,Y轴,油漆); 如果(overlayBitmap!= NULL)
{
canvas.drawBitmap(overlayBitmap,X轴+ 50,Y轴+ 50,油漆);
}
}公共无效窗格在overlayImage(){
// TODO自动生成方法存根 ImageProcessing.SAVE_STATUS = FALSE;
overlayBitmap = BitmapFactory.de codeResource(getResources()
R.drawable.add_image);
无效();}
位图绘制在画布上,但同时节省其保存为黑色图像。
这是我保存位图code ...
{尝试
FOS的FileOutputStream =新的FileOutputStream(文件);
updatedBitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
SAVE_STATUS =真;
saveDialog.dismiss();
}赶上(FileNotFoundException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
更新位图是由位图
初始化 公共位图getUpdatedImage(){
// TODO自动生成方法存根
//宽= 2048 HEIGHT = 1232 位图updatedBitmap = Bitmap.createScaledBitmap(位图,
orgWidth,orgHeight,真正的);
返回updatedBitmap;}
//////////////
我ImageProcessing.java类有code ...
如果(!SAVE_STATUS)
{
updatedBitmap = ip_DrawingClass.getUpdatedImage();
如果(updatedBitmap!= NULL)
{
文件=新的文件(file_root);
saveDialog.show();
}
其他
{
Toast.makeText(ImageProcessing.this,
文件无法保存的...,Toast.LENGTH_SHORT).show();
}
}
和saveDialog具有保存按钮,保存code保存上面写的形象。
///////////////////////////
@覆盖
公共布尔onTouchEvent(MotionEvent事件){
// TODO自动生成方法存根 浮XAXIS,Y轴; 开关(event.getAction()及MotionEvent.ACTION_MASK)
{
案例MotionEvent.ACTION_DOWN:
MODE =拖;
运行startx = event.getX();
startY = event.getY();
如果(ImageProcessing.drawLineStatus)
path.moveTo(运行startx,startY);
打破;
案例MotionEvent.ACTION_POINTER_DOWN:
MODE =ZOOM;
oldDist = this.findDistanceXY(事件);
打破;
案例MotionEvent.ACTION_MOVE:
如果(MODE ==ZOOM)
{
newDist = findDistanceXY(事件);
如果(newDist> oldDist)
{
this.applyZooming(加);
}
其他
{
this.applyZooming(减);
}
oldDist = newDist;
}
否则,如果(MODE ==拖动)
{
XAXIS = event.getX();
Y轴= event.getY();
如果(!LOCK_STATUS)
{
this.translateImage(X轴,Y轴,运行startx,startY);
}
其他
{
this.translateTextOnBitmap
(X轴,Y轴,运行startx,startY);
} 如果(ImageProcessing.drawLineStatus)
{
path.lineTo(X轴,Y轴);
this.drawLinesOnBitmap();
}
其他
draw_line = FALSE;
}
打破;
案例MotionEvent.ACTION_POINTER_UP:
MODE =拖;
this.setZoomBoxXY();
打破;
案例MotionEvent.ACTION_UP:
this.set_XY_Axis();
打破;
} 返回true;
}
图像转换这里..
公共无效translateImage(浮法X,浮法Y,startx的浮动,浮动startY){
// TODO自动生成方法存根 如果((startx的> = imageAtX和放大器;&放大器;运行startx< =(imageAtX + imageW))及和放大器;
(startY&GT = imageAtY&放大器;&放大器; startY&下; =(imageAtY + imageH)))
{
如果(imagePortionSelected || crop_status)
{
如果((startx的> = selectorAtX和放大器;&放大器;运行startx< =(selectorAtX + ZOOM))
&放大器;及(startY&GT = selectorAtY&放大器;&放大器; startY&下; =(selectorAtY +变焦)))
{
左=(INT)(X-(startx的-selectorAtX));
顶=(int)的(Y-(startY-selectorAtY));
右=左+ ZOOM;
底部=顶部+ ZOOM;
checkForValidityOfPara();
}
rect.set(左,上,右,下);
}
其他
{
X轴= X-(startx的-imageAtX);
Y轴= Y-(startY-imageAtY);
}
} 无效();
}
有关合并2位图使用下面的方法
公共位图combineImages(位图框,位图图像){ 位图CS = NULL;
位图RS = NULL; RS = Bitmap.createScaledBitmap(帧,image.getWidth()
image.getHeight(),TRUE); CS = Bitmap.createBitmap(rs.getWidth(),rs.getHeight()
Bitmap.Config.RGB_565); 帆布comboImage =新的Canvas(CS); comboImage.drawBitmap(图像,0,0,NULL);
comboImage.drawBitmap(RS,0,0,NULL); 如果(RS!= NULL){
rs.recycle();
RS = NULL;
}
调用Runtime.getRuntime()GC()。 返回CS;
}
和保存低于code组合图像的使用,无论你想...
位图outBmp = combineImages(BMP1,BMP2); imageFileFolder =新的文件(Environment.getExternalStorageDirectory()
FOLDER_PHOTOS);
imageFileFolder.mkdir();
FileOutputStream中OUT1 = NULL; 映像文件名称=新的文件(imageFileFolderfile_name.jpg); OUT1 =新的FileOutputStream(映像文件名称);
outBmp.com preSS(Bitmap.Com pressFormat.JPEG,100,OUT1);
out1.flush();
out1.close();
i am developing image editor and im using custom class to draw bitmap, here is my code..
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();
}
The bitmap is drawn on canvas but while saving it saves as a black image. this is my save bitmap code...
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();
}
updated bitmap is initialized by 'bitmap'
public Bitmap getUpdatedImage() {
// TODO Auto-generated method stub
//width=2048 height=1232
Bitmap updatedBitmap=Bitmap.createScaledBitmap(bitmap,
orgWidth, orgHeight, true);
return updatedBitmap;
}
////////////// my ImageProcessing.java class has code...
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();
}
}
and saveDialog has save button which holds code for saving image written above.
///////////////////////////
@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;
}
image translate here..
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();
}
For combining 2 bitmaps use below method
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;
}
And for saving combined Image use below code wherever you want...
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();
这篇关于如何覆盖一个位图到另一个节省SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!