标签: android-bitmap

如何从imageView获取位图

我需要从 imageView 中获取图像并将其作为位图。

String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
          .load(s)
          .into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
Run Code Online (Sandbox Code Playgroud)

您可以在 mImageViewFilter 中看到,我正在使用其 URI 和 Glide 库加载照片。我的下一步是拍摄 mImageViewFilter 中的这张照片并将其制作为位图。在最后一行代码之后有一个异常。我究竟做错了什么?

android bitmap android-bitmap

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

如何将 YUV_420_888 图像转换为位图

我正在从事 AR 项目,我需要捕获当前帧并将其保存到图库。我可以使用 AR 核心中的 Frame 类获取图像,但图像格式为 YUV_420_888。我已经尝试了很多解决方案来将其转换为位图,但无法解决。

android android-image android-bitmap arcore sceneform

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

CameraX PreviewView 截图

我想将 CameraX 预览从 previewView 转换为位图。类似于 textureView.bitmap 的东西

我已经用纹理视图尝试过它并且效果很好,我可以截取相机视图的屏幕截图,但相机被拉伸了。

screenshot bitmap android-bitmap takesscreenshot android-camerax

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

Glide centerCrop() 不适用于 CustomTarget

我得到的是“中心内部”而不是“中心作物” 在此输入图像描述

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

主要活动

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

要求

  • imageViews 在推送通知的情况下不可用,因此.into(CustomTarget<>)使用而不是.into(imageView)
  • 成功和失败的回调是必要的

android android-bitmap android-glide

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

BitmapFactory.decodeFile在Android 4.4 KitKat上返回异常

我想使用以下代码显示图像:

protected void onActivityResult(int requestCode, int resultCode, 
       Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            Log.wtf("M3K", "Above decode");
            Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
            Log.wtf("M3K", "Below decode");

            //Display image on layout
            Log.wtf("M3K", "Above display");
            logo.setImageBitmap(logoBMP);
            Log.wtf("M3K", "Below display");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题出Bitmap logoBMP = BitmapFactory.decodeFile(filePath);在Android 4.4(在我的Nexus 7上测试)上,它将返回一个未找到文件的异常,原因是EACCES(Permission …

android android-bitmap

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

Android,内存不足转换为黑白

当用户触摸它时,我将一些资源转换为黑白.我有这个函数在我的Util java文件中执行此操作:

public static Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);

    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    Bitmap blackAndWhiteBitmap = orginalBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint();
    paint.setColorFilter(colorMatrixFilter);

    Canvas canvas = new Canvas(blackAndWhiteBitmap);
    canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

    return blackAndWhiteBitmap;
}
Run Code Online (Sandbox Code Playgroud)

但有时,在某些设备中我收到此错误:

java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCopy(Native Method)
at android.graphics.Bitmap.copy(Bitmap.java:479)
at com.mygame.util.Util.convertColorIntoBlackAndWhiteImage(Util.java:145)
Run Code Online (Sandbox Code Playgroud)

该功能的第145行是

Bitmap blackAndWhiteBitmap = orginalBitmap.copy(Bitmap.Config.ARGB_8888, true);
Run Code Online (Sandbox Code Playgroud)

确切地说,我从其他stackoverflow答案中获取了该函数.

怎么了 ?

谢谢

performance android android-bitmap

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

如何在android上计算位图的java hashmap的大小

我在我的android服务中创建了一个位图对象的hahsmap,使用int作为键.为此,我根据需要添加动态创建的不同位图.我可以通过这个散列图(包括位图的大小),使用某个工具或以编程方式找到添加到我的服务的RAM /堆内存占用.

如果以前有人在这个地区工作过,请帮忙!

java android bitmap hashmap android-bitmap

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

位图到uri转换android?

我想将位图转换为URI转换我正在从该API使用google image search API,我从该URL获取了图像URL,将其转换为位图,然后我希望使用此示例将其从位图转换为URI,但我得到一个空值。

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {

        imagQest.setImageBitmap(result);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        result.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(ctx.getContentResolver(),
                result, "Title", null);
        LivLog.info(this.getClass(), "bit map conversion is" + result
                + path);

    }

    // Creates Bitmap from InputStream and …
Run Code Online (Sandbox Code Playgroud)

android uri android-bitmap

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

纹理查看视频和位图显示

我有一个显示位图(占位符图像)的TextureView,当用户加载视频时,textureView应该显示视频.

我设法在纹理视图中播放视频并在纹理视图中显示位图,但是当按顺序完成时,我只得到一个黑屏.

在我的onSurfaceTextureAvailable方法中,我在表面视图上绘制一个位图,如下所示:

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_add_clip);
    Canvas canvas = lockCanvas();
    canvas.drawBitmap(bm, (width/2)-(bm.getWidth()/2), (height/2)-(bm.getHeight()/2), null);
    unlockCanvasAndPost(canvas);
}
Run Code Online (Sandbox Code Playgroud)

当用户从其库中选择视频时,我将Uri加载到媒体播放器中并将曲面设置为媒体播放器.但是仍然显示位图.我试图通过清除画布删除位图,它确实删除了位图,但纹理视图只是黑色,也不会显示视频.

public void setMediaPlayer(MediaPlayer mediaPlayer) {
    // Canvas canvas = lockCanvas();
    // canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    // unlockCanvasAndPost(canvas);

    Surface s = new Surface(getSurfaceTexture());
    mMediaPlayer = mediaPlayer;
    mMediaPlayer.setSurface(s);
    mMediaPlayer.setOnPreparedListener(this);
}
Run Code Online (Sandbox Code Playgroud)

有些东西告诉我,用于显示视频的表面视图位于我在TextureAvailble上绘制的位图的后面.当我尝试清除画布时,我也清除了媒体播放器想要使用的表面.有解决方案吗

android android-canvas textureview android-bitmap

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

从Google Drive URI获取文件名

在我的应用程序中,用户可以从相机和图库中选择图像以加载到ImageView中。

我正在使用以下代码打开相关的应用程序以选择图像:

 Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Run Code Online (Sandbox Code Playgroud)

问题出在Google硬盘上。如果用户从Google云端硬盘中选择图片,则我将获得这样的URI:content://com.google.android.apps.docs.storage.legacy/enc%3DHxtpLaMr66OMOzDOKTRZ-5ed7q7Szj7F7nC0IlxBnV8hx2P1%0A

我能够生成位图,但无法获取文件名。

 InputStream input = context.getContentResolver().openInputStream(uri);
               Bitmap bitmap = BitmapFactory
                        .decodeStream(input);
Run Code Online (Sandbox Code Playgroud)

我需要显示用户选择的文件名。

请帮我从Google驱动器URI中提取图片名称。

android android-imageview google-drive-api android-bitmap google-drive-android-api

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