我想写一个Android应用程序,基本上在另一个图像上的图像上叠加,然后我想保存图片与叠加作为jpg或png。基本上这将是我想要保存的整个视图。
示例代码非常有用。
编辑:
我尝试了你的建议,并在星号线上得到一个空指针。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;
public class EditPhoto extends Activity {
/** Called when the activity is first created. */
LinearLayout ll = null;
TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.text);
ll = (LinearLayout) findViewById(R.id.layout);
ll.setDrawingCacheEnabled(true);
Bitmap b = ll.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
*** b.compress(CompressFormat.JPEG, 95,fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:81)
您可以利用View的绘图缓存。
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));
视图是您的视图。 95是JPG压缩的质量。文件输出流就是那个。
答案 1 :(得分:6)
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
在Moncader的答案中使用fos引用作为b.compress()方法的第3个参数。图像将作为image.jpg存储在SD卡的根目录中。
答案 2 :(得分:1)
将RelativeLayout或任何视图保存到图像(.jpg)
String getSaveImageFilePath() {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), YOUR_FOLDER_NAME);
// Create a storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(YOUR_FOLDER_NAME, "Failed to create directory");
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "IMG_" + timeStamp + ".jpg";
String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
Log.d(YOUR_FOLDER_NAME, "selected camera path " + selectedOutputPath);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
int maxSize = 1080;
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
if (bWidth > bHeight) {
int imageHeight = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
bitmap = Bitmap.createScaledBitmap(bitmap, maxSize, imageHeight, true);
} else {
int imageWidth = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
bitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, maxSize, true);
}
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
OutputStream fOut = null;
try {
File file = new File(selectedOutputPath);
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return selectedOutputPath;
}