合并两个位图并保存为一个图像
combine two bitmap and save as one image
我正在尝试合并两个位图并将其设置到 imageview 中,并在我单击按钮时保存该位图,但是当我合并位图将其设置为图像时,它显示的第二个位图比实际的大 size.here 是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBitmap(image1);
Bitmap drawingBitmap = Bitmap.createBitmap(parts.getWidth(),
parts.getHeight(),Bitmap.Config.ARGB_8888);
canvas = new Canvas(drawingBitmap);
canvas.drawBitmap(parts,0,0,null);
canvas.drawBitmap(touch,0,0,null);
bit=drawingBitmap;
public void getBitmap(View v) {
Bitmap bitmap= Bitmap.createBitmap(v.getWidth(),v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);
touch=bitmap;
}
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SaveImage save = new SaveImage();
save.Save(getApplicationContext(),bit);
}
});
}
这里parts是hold camera picture的位图,image1是动态创建的imageview。
SaveImage.java
package com.example.tattoodesign;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
public class SaveImage {
private Context TheThis;
private String NameOfFolder = "/PaintImage";
public String NameOfFile = null;
//private ArrayList<Bitmap> images = null;
public void Save(Context context,Bitmap ImageToSave){
TheThis = context;
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
File dir = new File(file_path);
NameOfFile="PaintImage"+CurrentDateAndTime+ ".jpg";
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir,NameOfFile);
if (file.exists ()) file.delete ();
try {
FileOutputStream fOut = new FileOutputStream(file);
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MakeSureFileWasCreatedThenMakeAvabile(file);
}
catch (FileNotFoundException e) {
UnableToSave();}
catch (IOException e){
UnableToSave();
}
}
private void MakeSureFileWasCreatedThenMakeAvabile(File file) {
MediaScannerConnection.scanFile(TheThis,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
private String getCurrentDateAndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
private void UnableToSave() {
Toast.makeText(TheThis, "Picture cannot to gallery", Toast.LENGTH_SHORT)
.show();
}
}
我该如何解决这个问题?
如有任何帮助,我们将不胜感激。
谢谢。
不太可能显示图片 "bigger than it's real size"。
它更有可能 "real size" 与您想象的不一样。
请记住,外观尺寸与像素 width/height 和 dpi 有关。
我认为您应该考虑缩放其中一张图像以匹配另一张图像。
例如,参见 Bitmap.createScaledBitmap
我正在尝试合并两个位图并将其设置到 imageview 中,并在我单击按钮时保存该位图,但是当我合并位图将其设置为图像时,它显示的第二个位图比实际的大 size.here 是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBitmap(image1);
Bitmap drawingBitmap = Bitmap.createBitmap(parts.getWidth(),
parts.getHeight(),Bitmap.Config.ARGB_8888);
canvas = new Canvas(drawingBitmap);
canvas.drawBitmap(parts,0,0,null);
canvas.drawBitmap(touch,0,0,null);
bit=drawingBitmap;
public void getBitmap(View v) {
Bitmap bitmap= Bitmap.createBitmap(v.getWidth(),v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);
touch=bitmap;
}
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SaveImage save = new SaveImage();
save.Save(getApplicationContext(),bit);
}
});
}
这里parts是hold camera picture的位图,image1是动态创建的imageview。
SaveImage.java
package com.example.tattoodesign;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
public class SaveImage {
private Context TheThis;
private String NameOfFolder = "/PaintImage";
public String NameOfFile = null;
//private ArrayList<Bitmap> images = null;
public void Save(Context context,Bitmap ImageToSave){
TheThis = context;
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
File dir = new File(file_path);
NameOfFile="PaintImage"+CurrentDateAndTime+ ".jpg";
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir,NameOfFile);
if (file.exists ()) file.delete ();
try {
FileOutputStream fOut = new FileOutputStream(file);
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MakeSureFileWasCreatedThenMakeAvabile(file);
}
catch (FileNotFoundException e) {
UnableToSave();}
catch (IOException e){
UnableToSave();
}
}
private void MakeSureFileWasCreatedThenMakeAvabile(File file) {
MediaScannerConnection.scanFile(TheThis,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
private String getCurrentDateAndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
private void UnableToSave() {
Toast.makeText(TheThis, "Picture cannot to gallery", Toast.LENGTH_SHORT)
.show();
}
}
我该如何解决这个问题?
如有任何帮助,我们将不胜感激。
谢谢。
不太可能显示图片 "bigger than it's real size"。 它更有可能 "real size" 与您想象的不一样。
请记住,外观尺寸与像素 width/height 和 dpi 有关。
我认为您应该考虑缩放其中一张图像以匹配另一张图像。 例如,参见 Bitmap.createScaledBitmap