Reputation: 41
I am getting the following error when i try to set a bitmap object into an imageview. "imageView2 cannot be resolved to a variable" for the line : mImg = (ImageView) findViewById(R.id.(imageView2));
CODE :
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 for the same :
<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>
NB : I am a first time developer trying my semester project. Any kind of help is welcomed.
Upvotes: 3
Views: 22544
Reputation: 121
implementation 'com.chootdev:blurimg:1.0.1'
add this dependency and add this code to your 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);
}
}
it will blur the image a little
Upvotes: 0
Reputation: 8645
try like this ImageView mImg = (ImageView) findViewById(R.id.imageView2);
this line write in on create method
ImageView mImg = (ImageView) findViewById(R.id.imageView2);
Bitmap bitmapOrg = MainActivity.mImg ;
statusimage.setImageBitmap(bitmapOrg);
EDIT:
Drawable drawable = mImg.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmap = bitmapDrawable.getBitmap();
}
Upvotes: 0
Reputation: 4119
As said by Kingamjick
It should be findViewById(R.id.imageView2) it means find the view by using the id(id) in Resources(R) which you declared and named as "imageView2"
Upvotes: 0
Reputation: 194
Try this:
}
//converting bitmap object to show in imageview2
ImageView mImg;
mImg = (ImageView) findViewById(R.id.imageView2);
mImg.setImageBitmap(bmOut);
}
instead of this:
}
//converting bitmap object to show in imageview2
ImageView mImg;
mImg = (ImageView) findViewById(R.id.(imageView2));
mImg.setImageBitmap(bmOut);
}
Upvotes: 8
Reputation: 2281
findViewById(R.id.(imageView2))
should be findViewById(R.id.imageView2)
.
imageView2
is a field on the class id
rather than a method on the class id
.
Upvotes: 4