标签: bitmapfactory

调整BitmapFactory.decodeByteArray()生成的图像大小

我正在创建音频播放器,我想向播放器显示歌曲封面,它使用小图像,但如果mp3文件有大图像,那么它就会超出布局视图.我正在使用以下代码将图像重新调整为300x300:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDensity = 300;
opt.inTargetDensity = 300;

songCoverView.setImageBitmap(BitmapFactory.decodeByteArray(songCover, 0, songCover.length, opt));
Run Code Online (Sandbox Code Playgroud)

但它仍然显示更大并且没有布局.

这段代码有什么问题?

android image-resizing bitmapfactory

4
推荐指数
2
解决办法
8413
查看次数

获取图像输入流的大小

我需要获得输入流中找到的图像的高度和宽度.这是我做的:

private Boolean testSize(InputStream inputStream){
        BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
        Bitmp_Options.inJustDecodeBounds = true;
        BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
        int currentImageHeight = Bitmp_Options.outHeight;
        int currentImageWidth = Bitmp_Options.outWidth;
        Bitmp_Options.inJustDecodeBounds = false;
    if(currentImageHeight < 200 || currentImageWidth < 200){
        Object obj = map.remove(pageCounter);
        Log.i("Page recycled", obj.toString());
        return true;
    }
    return false;}
Run Code Online (Sandbox Code Playgroud)

跳到问题点:

即使我在下面的第二个方法计算后强制它为false,它也会更改BitmapFactory.Options.

private Bitmap getBitmap(InputStream InpStream){
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);//Null.
    return originalBitmap;
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是有另一种方法从输入流获取图像的大小和宽度?我真的需要帮助,非常感谢任何帮助.

                ZipInputStream zip = null;
                zip = new ZipInputStream(new FileInputStream(getFileLocation()));
                for(ZipEntry zip_e = zip.getNextEntry(); zip_e != null …
Run Code Online (Sandbox Code Playgroud)

android inputstream bitmap bitmapfactory

4
推荐指数
1
解决办法
3999
查看次数

为什么使用inBitmap解码位图总是得到java.lang.IllegalArgumentException:将解码转换为现有位图

我正在使用inBitmap在android3 +上加载位图.但我总是得到java.lang.IllegalArgumentException: Problem decoding into existing bitmap.

在LruCache的entryMoved()功能中:我reusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap()));用来保持位图.

并且下次加载位图:我在reusableBitmaps上使用iterator找到我可以用于inBitmap的那个,我使用iterator找到哪一个,就像在官方开发者网站上管理内存一样.

但不幸的是,总是失败,logcat显示 java.lang.IllegalArgumentException: Problem decoding into existing bitmap.

任何人都可以给我一个示例或教程?

android bitmapfactory

4
推荐指数
1
解决办法
3150
查看次数

inDither =真正的Android

有人解释在Android中配置位图的情况下设置inDither = true时真正发生了什么吗?

在Developer.Android中,可以读到静态变量

Config.RGB_565
Run Code Online (Sandbox Code Playgroud)

此配置可能会产生轻微的视觉瑕疵,具体取决于源的配置.例如,没有抖动,结果可能会显示绿色.为了获得更好的结果,应该应用抖动

我有这个问题,直到我遵循这个建议,即:

options.inPreferredConfig = Config.RGB_565;
options.inDither = true;
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:如何在Android中理解这一点.知道何时使用语法是另一回事......另一个是完全理解它.

提前致谢!

android bitmap bitmapfactory

4
推荐指数
1
解决办法
2411
查看次数

为什么BitmapFactory.decodeResource会扩展我的图像?

我的目录中有一个120x120px的图像/res/drawable/.这是所有设备所需的大小.

要加载此Bitmap,我使用以下内容:

Bitmap tmpBmp = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
Run Code Online (Sandbox Code Playgroud)

问题是,当我测量时tmpBmp,大小是360x360px.我知道如何将其缩小到我需要的120x120,但这是浪费处理时间,尽管很少.

我假设这与屏幕密度有关,因为我的资源位于无密度目录中.

decodeResource缩放图像的原因是什么?我怎么阻止它这样做?

android bitmap drawable bitmapfactory android-resources

4
推荐指数
1
解决办法
3443
查看次数

Android:将图像对象转换为位图不起作用

我正在尝试将图像对象转换为位图,但它返回 null。

image = reader.acquireLatestImage();

                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
Run Code Online (Sandbox Code Playgroud)

图像本身是jpeg图像,我可以将其保存到磁盘中,我想转换为位图的原因是因为我想在将其保存到磁盘之前进行最终旋转。挖掘类 BitmapFactory 我看到这一行。

bm = nativeDecodeByteArray(data, offset, length, opts);
Run Code Online (Sandbox Code Playgroud)

此行返回空值。使用调试器进一步挖掘

private static native Bitmap nativeDecodeByteArray(byte[] data, int offset,
            int length, Options opts);
Run Code Online (Sandbox Code Playgroud)

这假设返回 Bitmap 对象,但它返回 null。

任何技巧..或想法?

谢谢

android bitmapfactory android-bitmap

4
推荐指数
1
解决办法
8303
查看次数

打开失败的 EISDIR(是一个目录)..如何解决这个问题?

每次它都会给我“无法解码流 java.io.FileNotFoundException: /: open Faild EISDIR (Is a Directory)”

我如何才能摆脱这个错误..这个类在我完成的许多任务中运行良好!

这是我用来获取位图的类

private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

    @Override
    protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

        InputStream iStream=null;
        String imgUrl = (String) hm[0].get("image");
        int position = (Integer) hm[0].get("position");

        URL url;
        try {
            url = new URL(imgUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            // Getting Caching directory
            File …
Run Code Online (Sandbox Code Playgroud)

android bitmapfactory

3
推荐指数
1
解决办法
2万
查看次数

位图应该是 2 字节还是 4 字节对齐?

MSDN 文档似乎自相矛盾:

这里说:

对于未压缩的 RGB 格式,最小步幅始终是以字节为单位的图像宽度,四舍五入到最接近的 DWORD。

这里它说:

每条扫描线中的字节数。该值必须能被 2 整除,因为系统假定位图的位值形成一个字对齐的数组。

所以有时 MSDN 需要 4 字节对齐的步幅,有时它需要 2 字节对齐的步幅。哪个是对的?

更具体地说,在保存位图文件时,我应该使用 4 字节步幅还是 2 字节步幅?

c++ msdn bitmap bitmapfactory

3
推荐指数
1
解决办法
1621
查看次数

资产中的 BitmapFactory.decodeFile 不起作用 (FileNotFindException)

decodeFile参数指向资产文件夹内的文件时,我不知道为什么不起作用。

     // Load images from the file path
    String[] dir = null;
    try {
        dir = GenericMainContext.sharedContext.getAssets().list("drawable");
       // dir Log => [ic.png, ic_info_dark.png, ic_launcher_default.png]
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (String uri : dir){ 
        // do your stuff here
        if (uri!=null) {
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeFile("file:///android_asset/drawable/"+uri);               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Run Code Online (Sandbox Code Playgroud)

有什么解释吗?

android android-assets bitmapfactory

3
推荐指数
1
解决办法
2301
查看次数

如何在ARCore中从session.update()获取Bitmap [A​​ndroid Studio]

我正在尝试使用 ARCore 从 ARSession 的当前帧获取位图。但它始终等于 null。我已经在网上搜索了一段时间,但无法弄清楚我做错了什么。

try {
    capturedImage = mFrame.acquireCameraImage();

    ByteBuffer buffer = capturedImage.getPlanes()[0].getBuffer();

    byte[] bytes = new byte[buffer.capacity()];

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,null);

    if (bitmap == null) 
        Log.e(TAG,"Bitmap was NOT initialized!");

} catch(Exception e){

}
Run Code Online (Sandbox Code Playgroud)

我正在mFrameonDrawFrameGLSurfaceView用来显示相机图像的地方获取信息。一切工作正常,除了我的位图等于空。

我使用的是按钮,因此只使用了一个框架,如下所示:

scanButton = (Button) findViewById(R.id.scanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        checkbox = false;
        if (capturedImage!=null) capturedImage.close();
            BitmapMethod();
    }
});
Run Code Online (Sandbox Code Playgroud)

capturedImagebuffer并且bytes全部不等于 null。

可能有什么问题吗mFrame.acquireCameraImage()

多谢

java android bitmap bitmapfactory arcore

3
推荐指数
1
解决办法
2439
查看次数