3

当我尝试将位图对象设置为图像视图时出现以下错误。“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>

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

4

5 回答 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);

}
于 2013-02-07T06:15:01.123 回答
4

findViewById(R.id.(imageView2))应该是findViewById(R.id.imageView2)

imageView2是类上的一个字段,id而不是类上的一个方法id

于 2013-02-07T06:08:31.700 回答
0

正如金加姆吉克所说

它应该是 findViewById(R.id.imageView2) 这意味着通过使用您声明并命名为“imageView2”的 Resources(R) 中的 id(id) 来查找视图

于 2013-02-07T06:13:39.440 回答
0

像这样尝试 ImageView mImg = (ImageView) findViewById(R.id.imageView2);这一行在创建方法上写入

 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();
    }
于 2013-02-07T06:14:25.327 回答
0
implementation 'com.chootdev:blurimg:1.0.1'

添加此依赖项并将此代码添加到您的 main

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);

    }
}

它会使图像有点模糊

于 2020-01-15T22:27:04.290 回答