在安卓中将位图转换为图像视图

3
当我尝试将位图对象设置到ImageView中时,出现以下错误提示:"imageView2无法解析为变量",出错代码如下:mImg = (ImageView) findViewById(R.id.(imageView2));。该错误与找不到对应的控件有关。
package com.example.ocr01;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

    //SETTING UP BUTTON AND LISTENER
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener((OnClickListener) this);
}

public void onClick(View v) {
      // do something when the button is clicked
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




//CONVERTING IMAGE TO BITMAP

/*public static Bitmap getBitmapFromURL(String xxx) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}*/

    void create_bitmap(){
        //creating bitmap
        Bitmap source = BitmapFactory.decodeResource(getResources(),
        R.drawable.image1);
        //calling doGreyScale
        doGreyscale(source);
    }

    public static void doGreyscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;

        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;

        // get image size
        int width = src.getWidth();
        int height = src.getHeight();

        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        //converting bitmap object to show in imageview2
        ImageView mImg;
        mImg = (ImageView) findViewById(R.id.(imageView2));
        mImg.setImageBitmap(bmOut);

    }

}

对应的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:src="@drawable/image1" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/imageView1"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

注意: 我是一位第一次尝试学期项目的开发者。欢迎任何形式的帮助。

2
Why R.id.(imageView2) ? - smk
5个回答

8

试试这个:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.imageView2);
    mImg.setImageBitmap(bmOut);

}

改为这样:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.(imageView2));
    mImg.setImageBitmap(bmOut);

}

4
什么是区别? - chia yongkang

4

findViewById(R.id.imageView2)应该替代findViewById(R.id.(imageView2))

imageView2id类中的一个字段,而不是id类上的一个方法。


去掉括号,但仍然出现错误:“R的原始类型int没有id字段” @Kingamajick - Zubin Sheth
如果您在Eclipse中进行开发,在输入R.id.后按Ctrl+Space,它将列出所有可能的ID。 - Vins
你自添加imageView2后是否重新构建了你的R文件? - Kingamajick
@vins - 当我按下Ctrl空格键时,没有“默认建议”。 - Zubin Sheth
@kindamajick - 如何重新构建我的R文件?请指导。 - Zubin Sheth

0
尝试像这样写:ImageView mImg = (ImageView) findViewById(R.id.imageView2); 这行代码写在 onCreate 方法中。
 ImageView mImg = (ImageView) findViewById(R.id.imageView2);
 Bitmap bitmapOrg = MainActivity.mImg ;     
 statusimage.setImageBitmap(bitmapOrg);

编辑:

 Drawable drawable = mImg.getDrawable();
   if (drawable instanceof BitmapDrawable) {
       BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
       bitmap = bitmapDrawable.getBitmap();
    }

在onCreate中编写以下代码行: ImageView mImg = (ImageView) findViewById(R.id.imageView2); 有助于消除错误,但为什么要使用MySipDetails.mImg呢?mImg.setImageBitmap(bmOut);不是可以完成工作吗? - Zubin Sheth
在编辑的代码中,使用您的Activity名称“MainActivity”代替“MySipDetails”。 - NagarjunaReddy
无法将图像转换为Bitmap以供bitmapOrg使用。 - Zubin Sheth

0

正如Kingamjick所说

应该使用findViewById(R.id.imageView2),它的意思是通过在资源(R)中声明并命名为"imageView2"的id(id),来找到视图.


去掉了括号,但仍然出现错误:“R的原始类型int没有id字段”。 - Zubin Sheth
可能是无法获取资源文件,请确保在构建后的Eclipse项目中有gen文件夹和其中的R.java文件.. 如果仍然存在问题,则尝试清理该项目。 - Vins
已经清理了项目,是的,有一个带有R.java文件的gen文件夹。但错误仍然存在。 - Zubin Sheth

0
implementation 'com.chootdev:blurimg:1.0.1'

添加这个依赖项并将此代码添加到您的主要文件中

package com.example.blureffect;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.chootdev.blurimg.BlurImage;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.image);
        BlurImage blurImage = new BlurImage();
        blurImage.withContext(this)
                .blurFromResource(R.drawable.dnd_background)
                .into(imageView);

    }
}

它会稍微模糊图像


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接