我有以下代码来创建一个大小为8303×5540的画布,但运行该代码会产生一个OutOfMemoryException.
scaledBitmap = Bitmap.createBitmap(8303, 5540, Bitmap.Config.ARGB_8888);
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我正在尝试压缩我保存在文件中的图像.我正在尝试将文件压缩为1MB.我尝试了一些方法,但它通常会产生一个OutofMemoryError.然后我尝试使用此解决方案,但它使位图空白.
这是我的代码:
System.gc();
getActivity().getContentResolver().notifyChange(mImageTempUri, null);
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(mImageDirectory + mImageName, options);
if(bitmap == null){
howRequestFailedErrorMessage("Gambar gagal di-upload");
return;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
File f = new File(mImageDirectory + mImageName);
if(f.exists()){
f.delete();
}
FileOutputStream fo;
try {
fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
Run Code Online (Sandbox Code Playgroud) 我正在从我的 Android 应用程序创建一个可打印标签(这是一个检查应用程序,允许您在蓝牙打印机上打印标签,以便留在现场以识别您发现的任何问题)。
我用来drawText在 上绘制文本Canvas并使用 来设置我的绘画(样式等)TextPaint。
如果您查看继承自的 上可用的标志Paint,TextPaint您将看到许多与潜在质量相关的标志,例如抖动和抗锯齿。
在查看其他教程后,他们似乎使用 和 之一或Paint.ANTI_ALIAS_FLAG两者Paint.LINEAR_TEXT_FLAG。
没有太多讨论这些选项实际上如何影响输出质量,特别是文本输出质量。一般来说,我知道抗锯齿和抖动的作用,但 Android 文档中没有关于用于文本等的官方建议。
有这方面的好资源吗?
我想drawTextToBitmap在位图图像上添加一个字符串。我有一个方法,这种方法在位图图像上工作成功将字符串放置在位图图像上。但我的位图图像非常小,像针标记图像。此函数根据位图高度和宽度设置字符串。我想放置字符串超过位图图像。所以请帮我解决问题。
我用来获取位图的以下方法:
public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
Resources resources = gContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// …Run Code Online (Sandbox Code Playgroud) 我已通过BitmapFactory.decodeStream以下代码将 JPEG 图像转换为 RGB 颜色值数组:
picw = selectedImage.getWidth();
pich = selectedImage.getHeight();
int[] pix = new int[picw * pich];
selectedImage.getPixels(pix, 0, picw, 0, 0, picw, pich);
int R, G, B;
for (int y = 0; y < pich; y++) {
for (int x = 0; x < picw; x++) {
int index = y * picw + x;
R = (pix[index] >> 16) & 0xff;
G = (pix[index] >> 8) & 0xff;
B = pix[index] & 0xff;
pix[index] = …Run Code Online (Sandbox Code Playgroud) 这里我附加将Bitmap转换为Byte数组的代码,现在我必须重新生成位图以在android Image-view中显示.
在下面的代码中,mRawBitmapData是byte Araay.这里的代码是为转换后的图像创建字节数组,所以现在我必须从这个字节数组中重新生成位图.
private void convertArgbToGrayscale(Bitmap bmpOriginal, int width, int height){
int pixel;
int k = 0;
int B=0,G=0,R=0;
try{
for(int x = 0; x < height; x++) {
for(int y = 0; y < width; y++, k++) {
pixel = bmpOriginal.getPixel(y, x);
if(pixel == -1){
mDataArray[k] = 1;
}
else {
mDataArray[k] = 0;
}
}
if(mDataWidth>width){
for(int p=width;p<mDataWidth;p++,k++){
mDataArray[k]=0;
}
}
}
}catch (Exception e) {
Log.e(TAG, e.toString());
}
}
private void createRawMonochromeData(){
int length = 0; …Run Code Online (Sandbox Code Playgroud) 我在这里设置了意图标志:
\n\npublic void createAlbum(View view) {\n Intent intent = new Intent();\n intent.setType("image/*");\n intent.setAction(Intent.ACTION_OPEN_DOCUMENT);\n intent.addCategory(Intent.CATEGORY_OPENABLE);\n intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);\n intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);\n String intentChooserDialog = getResources().getString(R.string.pick_image_dialog);\n startActivityForResult(Intent.createChooser(intent, intentChooserDialog), PICK_IMAGE);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在我的活动结果中,我设置了持久权限,如下所述:
\n\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n if (requestCode == PICK_IMAGE) {\n // Get image selection\n if (data != null) {\n ClipData clipData = data.getClipData();\n if (clipData != null) {\n int clipDataCount = clipData.getItemCount();\n if (clipDataCount > 1) {\n int clipIndex = 0;\n BitmapUri[] bitmapUris …Run Code Online (Sandbox Code Playgroud) 我有一个 imageView 和几个 textViews 我的应用程序允许用户在用户想要的 imageView (imageView 不是全屏)的每个坐标上拖动 textViews 。
换句话说,此应用程序允许用户向用户图像添加多个标题,并将该图像和标题转换为单个图像并将其存储在用户设备上。
据一个计算器的回答,我可以一个刚刚转换的TextView文本到bitamp
有什么方法可以截取用户在 kotlin 中创建的带有标题的最终图像?
这是我的代码:
@Throws(IOException::class)
fun foo(text: String) {
val textPaint = object : Paint() {
init {
setColor(Color.WHITE)
setTextAlign(Align.CENTER)
setTextSize(20f)
setAntiAlias(true)
}
}
val bounds = Rect()
textPaint.getTextBounds(text, 0, text.length, bounds)
val bmp = Bitmap.createBitmap(mImgBanner.getWidth(), mImgBanner.getHeight(), Bitmap.Config.RGB_565) //use ARGB_8888 for better quality
val canvas = Canvas(bmp)
canvas.drawText(text, 0, 20f, textPaint)
val path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/image.png"
val stream = FileOutputStream(path) …Run Code Online (Sandbox Code Playgroud) 基本上,我正在从用户存储中读取图像进行处理,在读取图像时,我正在计算 inSampleSize 以节省一些内存,这是我的代码:
fun calculateInSampleSize(
options: BitmapFactory.Options,
reqWidth: Int,
reqHeight: Int
): Int {
// Raw height and width of image
val (height: Int, width: Int) = options.run { outHeight to outWidth }
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val halfHeight: Int = height / 2
val halfWidth: Int = width / 2
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger …Run Code Online (Sandbox Code Playgroud) 我正在尝试显示通过存储的相机意图(触发内置相机应用程序)捕获的图片external SD-card.
文件路径和文件名存储在String变量中'mCurrentPhotoPath'.
我已经验证捕获的图像存储在'mCurrentPhotoPath'使用ES File Explorerapp 指定的位置,但BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)始终返回FileNotFoundException.这是我存储捕获图像的代码:
private void addPicToGallery() {
File f = new File(mCurrentPhotoPath);
//for debug purpose only
Log.i(MYTAG, "value of mCurrentPhotoPath in addPicToGallery: " +mCurrentPhotoPath);
Log.i(MYTAG, "value of f in addPicToGallery: " +f);
Uri contentUri = Uri.fromFile(f);
//for debug only
Log.i(MYTAG, "value of contentUri in addPicToGallery: " +contentUri);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
Run Code Online (Sandbox Code Playgroud)
解码缩放图像的代码,用于显示ImageView:
private void setFullImageFromFilePath() {
Log.i(MYTAG, …Run Code Online (Sandbox Code Playgroud) android ×10
android-bitmap ×10
bitmap ×2
kotlin ×2
canvas ×1
crashlytics ×1
java ×1
paint ×1
permissions ×1
rgb ×1
text ×1