gpt4 book ai didi

java - 使用 MEME 图像等文本创建图像并保存为图像文件。使用 Canvas 在位图上绘制时无法保存图像

转载 作者:行者123 更新时间:2023-11-30 10:51:26 24 4
gpt4 key购买 nike

我正在创建一个 android 应用程序,它基本上通过 xml 布局中的 EditText 将背景图像(默认图像)与用户输入合并。我已经开发了一些工作代码,但它似乎无法正常工作。欢迎任何形式的帮助。附言开发新手。

这是我的 MainActivity.Java

public class MainActivity extends AppCompatActivity  {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

EditText editText; //defined edit text
Bitmap bmp; // defined Bitmap
Button button;

public void myBit(){

bmp = Bitmap.createBitmap(editText.getDrawingCache());
}

public void saveImg() {

File myDir = new File("/sdcard/saved_images");
myDir.mkdir();
Random ran = new Random();
int n = 10000;
n = ran.nextInt(n);
String fname = "Image-"+n+".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);

// canvas for merging text over image (Bitmap)
Canvas canvas = new Canvas(bmp);

Paint paint = new Paint();
paint.setColor(Color.WHITE); // Text Color
paint.setStrokeWidth(12); // Text Size
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
// some more settings...

canvas.drawBitmap(bmp, 400, 400, paint); // ◄ Edited here
canvas.drawText("Testing...", 10, 10, paint);

bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

}
@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

//getMenuInflater().inflate(R.menu.main, menu);

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

saveImg();
Toast.makeText(MainActivity.this, "Image Saved!!", Toast.LENGTH_SHORT).show();

}

});
return true;
}
}

最佳答案

我试过以下两种方法从图像上的位图创建文件

  • 通过将 edittext 作为图像或传递 View 并从下面发布的代码获取位图 getBitmapFromView 方法将 View 转换为位图
  • 第二个就像给 edittext 文本并在默认值之上绘制image.writeTextOnImage 此方法将从图像上方的给定文本创建位图,它将像 MEME 类型的图像一样工作。用你的图片代替 R.drawable.t_img

Activity

    public class CreateSaveImage extends AppCompatActivity {
private EditText editText;
private File file;
Bitmap bmp; // defined Bitmap
ImageView imgShowOuput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_voice);
file = new File(Environment.getExternalStorageDirectory() + File.separator + "saveimage");
file.mkdirs();
editText = (EditText) findViewById(R.id.edtTextView);
imgShowOuput = (ImageView) findViewById(R.id.imgShowOuput);
findViewById(R.id.btnSaveImage).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bmp = getBitmapFromView(editText);
String path = file.getAbsolutePath() + File.separator + System.currentTimeMillis() + "_image" + ".jpg";
saveBitmapToFile(bmp, path, 100);
imgShowOuput.setImageBitmap(bmp);
Toast.makeText(CreateSaveImage.this, "Image Saved!!", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnSaveImageFromText).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bmp = writeTextOnImage(CreateSaveImage.this, editText.getText().toString());
String path = file.getAbsolutePath() + File.separator + System.currentTimeMillis() + "_drawImage" + ".jpg";
saveBitmapToFile(bmp, path, 100);
imgShowOuput.setImageBitmap(bmp);
}
});
}
/**
* @param view
* @return
*/
public static Bitmap getBitmapFromView(View view) {
view.clearFocus();
view.setPressed(false);
view.setFocusable(false);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

public static String saveBitmapToFile(
Bitmap bitmap, String path, int quality) {
File imageFile = new File(path);
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e("BitmapToTempFile", "Error writing bitmap", e);
}
return imageFile.getAbsolutePath();
}
public Bitmap writeTextOnImage(Context mContext, String mText) {
try {
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
/// Here you need to give your default image
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.t_img);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// Change Text Size & Color based on your requirement
paint.setColor(Color.BLACK);
paint.setTextSize((int) (21 * scale));
paint.setShadowLayer(20f, 0, 0, Color.LTGRAY);
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
// Change position based on your requirement
int x = 20;
int y = 20;
canvas.drawText(mText, x * scale, y * scale, paint);
return bitmap;
} catch (Exception e) {
return null;
}
}
}

Xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7e95e6"
android:orientation="vertical">

<EditText
android:id="@+id/edtTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000" />

<Button
android:id="@+id/btnSaveImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save EditText Image" />

<Button
android:id="@+id/btnSaveImageFromText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Text from EditText as a Image" />

<ImageView
android:id="@+id/imgShowOuput"
android:layout_width="fill_parent"
android:layout_height="400dp" />
</LinearLayout>

Check output of above which i got

如果有什么事请告诉我

关于java - 使用 MEME 图像等文本创建图像并保存为图像文件。使用 Canvas 在位图上绘制时无法保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34729693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com