使用此代码:
Drawable blankDrawable = context.getResources().getDrawable(image);
Bitmap blankBitmap=((BitmapDrawable)blankDrawable).getBitmap();
Run Code Online (Sandbox Code Playgroud)
我得到一个缩放到上下文密度的位图,保留位图的物理大小(基于其dpi值).例如,我有一个405x500位图(dpi = 96)作为资源.但是当我将它加载到设备上时,我得到一张密度= 240的608x750图像.我想加载位图而不进行缩放.我怎么做?
这个问题非常类似于:
但是,在我的情况下不能使用该解决方案,因为我没有输入流.我所拥有的只是一个资源ID,而getDrawable()方法没有密度参数.加载位图后,为时已晚 - 它已经调整大小.
谢谢.
我的SD卡上有一个2448x3264图像,消耗1,667,072字节,但当我将其作为位图加载并使用getRowBytes()*getHeight()我最终计算其大小为15,980,544字节时.
为什么会发生这种情况,如何计算文件的实际大小?
我所拥有的是R:255 G:181 B:178,我在C#工作(对于WP8,更具体)
我想将其转换为十六进制数字以用作颜色(设置WriteableBitmap的像素颜色).我正在做的是以下内容:
int hex = (255 << 24) | ((byte)R << 16) | ((byte)G << 8) | ((Byte)B<<0);
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我才会变蓝.
我有什么想法我做错了吗?
另外,要撤消此操作,要检查RGB值,我将:
int r = ((byte)(hex >> 16)); // = 0
int g = ((byte)(hex >> 8)); // = 0
int b = ((byte)(hex >> 0)); // = 255
Run Code Online (Sandbox Code Playgroud) 该Bitmap班有一个方法copy()与下面的签名:
public Bitmap copy(Bitmap.Config config, boolean isMutable)
Run Code Online (Sandbox Code Playgroud)
可变和不可变之间是否存在性能差异Bitmap?
我的应用程序中存在巨大的内存问题.我正在使用谷歌地图api v2 ClusterManager和自定义标记.我markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));根据其类别通过调用为每个标记提供图像.问题是:在几次屏幕旋转后,我的应用程序因OOM错误而崩溃:
05-14 11:04:12.692 14020-30201/rokask.rideabike E/art? Throwing OutOfMemoryError "Failed to allocate a 4194316 byte allocation with 1627608 free bytes and 1589KB until OOM"
05-14 11:04:12.722 14020-30201/rokask.rideabike E/AndroidRuntime? FATAL EXCEPTION: GLThread 19179
Process: rokask.rideabike, PID: 14020
java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 1627608 free bytes and 1589KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:939)
at android.graphics.Bitmap.createBitmap(Bitmap.java:912)
at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
at com.google.maps.api.android.lib6.gmm6.n.c.i.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.c.l.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.c.l.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.c.l.b(Unknown Source)
at …Run Code Online (Sandbox Code Playgroud) android bitmap out-of-memory markerclusterer google-maps-android-api-2
在Flash中,BitmapData对象可用于在RAM中存储位图,稍后您可以使用beginBitmapFill()方法将它们绘制到MovieClip .
如何将外部位图文件(.jpg)加载到BitmapData对象中?
即使是AS3代码也会有所帮助.
例如,我希望在位图的所有4侧围绕10像素的白色边框.我没有将它用于imageview我目前正在使用此代码来裁剪图像.我可以知道如何在其中添加白色边框吗?
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth …Run Code Online (Sandbox Code Playgroud) 是否可以将EditText框内的字符串文本转换为位图?换句话说,有没有办法将字符串文本转换为位图,这意味着文本将显示为图像?
以下是我的代码:
class TextToImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
//create String object to be converted to image
String sampleText = "SAMPLE TEXT";
String fileName = "Image";
//create a File Object
File newFile = new File("./" + fileName + ".jpeg");
//create the font you wish to use
Font font = new Font("Tahoma", Font.PLAIN, 11);
//create the FontRenderContext object which helps us to measure the text
FontRenderContext frc = new FontRenderContext(null, true, true);
}
}
Run Code Online (Sandbox Code Playgroud) 我需要将文件从文件发送到服务器.服务器以2400x2400的分辨率请求图像.
我想要做的是:
1)使用正确的inSampleSize使用BitmapFactory.decodeFile获取位图.
2)以JPEG格式压缩图像,质量为40%
3)在base64中对图像进行编码
4)发送到服务器
我无法实现第一步,它抛出一个内存不足的异常.我确信inSampleSize是正确的,但我想即使使用inSampleSize,Bitmap也很大(在DDMS中大约30 MB).
任何想法怎么办呢?我可以在不创建位图对象的情况下执行这些步骤吗?我的意思是在文件系统而不是RAM内存上进行.
这是当前的代码:
// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > …Run Code Online (Sandbox Code Playgroud) 我正在使用PdfRenderer上面的api 21在我的应用程序中显示pdf,我注意到页面的质量非常差.我也跟着谷歌样本使用PdfRenderer,这就是我Bitmap为页面创建的方式:
//mCurrentPage is a PdfRenderer.Page and mImageView is an ImageView
Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(),
mCurrentPage.getHeight(),
Bitmap.Config.ARGB_8888);
mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
mImageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)
我用过,ARGB_8888因为据我所知,它是显示位图的最佳质量.难道我做错了什么?
编辑
这是PdfRenderer类和经典Pdf阅读器之间的巨大差异:
bitmap ×10
android ×8
actionscript ×1
bit-shift ×1
bitmapdata ×1
c# ×1
drawing ×1
flash ×1
heap-memory ×1
image ×1
memory ×1
mutable ×1
pdf ×1
pdfrenderer ×1
size ×1