Change pixel in java

Change the color of each pixel in an image java

The third parameter is ARGB value in 32 bits. This is laid out in bit form as:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB 

See the javadoc for BufferedImage.setRGB (assuming your using BufferedImage, your question doesn’t actually say. )

Sets a pixel in this BufferedImage to the specified RGB value. The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. For images with an IndexColorModel, the index with the nearest color is chosen

  • If you’re using an image type that supports transparency it is important you set alpha 255 means fully opaque, 0 is fully transparent.

You can create such a value using bit shifting.

int alpha = 255; int red = 0; int green = 255; int blue = 0; int argb = alpha  

Luckily there is a getRGB() method on java.awt.Color instances, so you could use

image.setRGB(i, j, Color.green.getRGB()); 

Here's a full working example, perhaps you can compare to your code:

public class StackOverflow27071351 < private static class ImagePanel extends JPanel < private BufferedImage image; public ImagePanel(int width, int height, BufferedImage image) < this.image = image; image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); repaint(); >public void paintComponent(Graphics g) < super.paintComponent(g); for (int i = 0; i < image.getWidth(); i++) < for (int j = 0; j < image.getHeight(); j++) < image.setRGB(i, j, new Color(255, 0, 0, 127).getRGB()); >> g.drawImage(image, 0, 0, getWidth(), getHeight(), this); > > public static void main(String[] args) < JFrame frame = new JFrame(); int width = 640; int height = 480; frame.setSize(width, height); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(new ImagePanel(width, height, image)); frame.setVisible(true); >> 

Thanks for answering. I use BufferedImage, and i try in ur way. but it doesn't work either. i use a black image( an image with all black background).

@HuazheYin I've added a fully working example showing red, I've not seen the all black problem you describe.

Well, 3rd parameter is the color in RGB, so it will be black if you set it to 0.

private int colorToRGB(int alpha, int red, int green, int blue)
image.setRGB(i, j, colorToRGB(alpha, 0, 0, 0)) 

the alpha value is between 0(transparent) -> 255, don't forget as @Adam mentioned to set your BufferedImage to TYPE_INT_ARGB

i just tried it, it still does not work. my program is simple, i just put a buffered image into a frame and i can change the frame transparency, but i wanna change the partial transparency. do u have more resolution?

if you just want to change frame transparency I recommend to use AlphaComposite. Check this: java2s.com/Code/Java/2D-Graphics-GUI/MakeimageTransparency.htm

i know how to change the transparency of a frame, and i implement it. it can change the whole transparency of frame, but my requirement is change partial transparency, thats why i use buffered image. so i change the partial transparency of frame by changing the partial transparency of buffered image.

int[] pixel = new int[4]; // the following four ints must range 0..255 pixel[0] = redValue; pixel[1] = greenValue; pixel[2] = bluleValue; pixel[3] = alphaValue; raster.setPixel(x, y, pixel); 

To get a raster for a BufferedImage, I do this:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = image.getRaster(); 

I've done some performance testing, and have not found that stuffing all the bytes of color values into a single number to make much of a difference.

It is also good to know the technique where one can draw an opaque image (e.g., RGB rather than an ARGB) with an alpha value.

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (yourAlpha))); g2d.drawImage(. ); 

Источник

setpixel in bitmap android

This code is working !! Set the new bitmap in ImageView and its working !

public class Bleach extends AppCompatActivity implements Cloneable < Bitmap bm; ImageButton bDone; Bitmap _bitmapPrev; Bitmap _bitmapCur; File imageFile; Bitmap bm3; Bitmap bm4; ImageView bl; int x; int y; String[] projection = new String[]< MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images.ImageColumns.MIME_TYPE >; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_bleach); bDone = (ImageButton) findViewById(R.id.donebtnid); bDone.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < finish(); startActivity(new Intent(Bleach.this, MainMenuActivity.class)); >>); //bl = (ImageView)findViewById(R.id.pickbleachi d); final Cursor cursor = getContentResolver() .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"); if (cursor != null) < if (cursor.moveToFirst()) < final ImageView bl = (ImageView) findViewById(R.id.pickbleachid); String imageLocation = cursor.getString(1); imageFile = new File(imageLocation); if (imageFile.exists()) < // TODO: is there a better way to do this? try< bm = BitmapFactory.decodeFile(imageLocation); bm3 = ExifUtil.rotateBitmap(imageLocation,bm); >catch (Exception exception) < Log.d("PICtureConfirmation ","bm decodefile"+ exception.getMessage()); >try < // bl.setImageBitmap(bm3); //Drawable imgDrawable = teeth.getDrawable(); //Bitmap bm5=((BitmapDrawable)bm3.getDrawable()).getBitmap(); int W = bm3.getWidth(); int H = bm3.getHeight(); int [][][] clr = new int [3][H][W]; int pixel; for(int i=0;i/* . pixel changging process . */ //save a new image for(int i=0;i > bl.setImageBitmap(bm3); > catch (Exception e)<> > > > > public void SetGamma(double red,double green, double blue) < Bitmap temp = (Bitmap)_bitmapCur; //Bitmap bmap = (Bitmap)temp.clone(); >> 

Источник

Replacing image pixel in Java2D

I am trying to replace some pixels from my source image(PNG format). But i am end up with some confusing result. Basically i am replacing a particular RGB values with black and white colors. Here is my code,

import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; public class ChangePixel < public static void main(String args[]) throws IOException < File file = new File(System.getProperty("user.dir"), "D4635.png"); FileInputStream fis = new FileInputStream(file); BufferedImage image = ImageIO.read(fis); int[] replaceColors = new int[2]; replaceColors[0] = Color.BLACK.getRGB(); replaceColors[1] = Color.WHITE.getRGB(); Color src = new Color(133, 101, 51); int srcRGBvalue = src.getRGB(); changeAlg2(image, srcRGBvalue, replaceColors); >private static void changeAlg2(BufferedImage image, int srcRGBvalue, int[] replaceColors) throws IOException < for (int width = 0; width < image.getWidth(); width++) < for (int height = 0; height < image.getHeight(); height++) < if (image.getRGB(width, height) == srcRGBvalue) < image.setRGB(width, height, ((width + height) % 2 == 0 ? replaceColors[0] : replaceColors[1])); >> > File file = new File(System.getProperty("user.dir"), "107.png"); ImageIO.write(image, "png", file); > > 

It changes my source pixels to black and some other color, instead of white. Please advice me, what's going wrong here. Since this is my first post, I can't able to attach my images. Sorry for the inconvenience. Edit: I have uploaded the source and the output images in a site. Here is the URL, Source : http://s20.postimage.org/d7zdt7kwt/D4635.png Output : http://s20.postimage.org/kdr4vntzx/107.png Expected output : After the black pixel, white pixel has to come. Edit : Resolved code as per Jan Dvorak advice,

import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; public class ChangePixel < public static void main(String args[]) throws IOException < File file = new File(System.getProperty("user.dir"), "D12014.gif"); FileInputStream fis = new FileInputStream(file); BufferedImage image = ImageIO.read(fis); Color src = new Color(223, 170, 66); int srcRGBvalue = src.getRGB(); int[] replaceColors = new int[2]; replaceColors[0] = Color.MAGENTA.getRGB(); replaceColors[1] = Color.CYAN.getRGB(); changeAlg2(image, srcRGBvalue, replaceColors); >private static void changeAlg2(BufferedImage image, int srcRGBvalue, int[] replaceColors) throws IOException < BufferedImage image2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); for (int width = 0; width < image.getWidth(); width++) < for (int height = 0; height < image.getHeight(); height++) < if (image.getRGB(width, height) == srcRGBvalue) < image2.setRGB(width, height, ((width + height) % 2 == 0 ? replaceColors[0] : replaceColors[1])); >else < image2.setRGB(width, height, image.getRGB(width, height)); >> > File file = new File(System.getProperty("user.dir"), "110.gif"); ImageIO.write(image2, "gif", file); > > 

Источник

How to edit the pixels in a BufferedImage?

After scouring the internet for days, I found a Question that seemed to address my goal. (I'm trying to draw/edit an individual pixel in an image, and render it.) In said question, The ask-er requested code for a Black BufferedImage. The top Answer provided that code, and appears to work beautifully, until you try to change it to something other than black. Here's the Code:

package myProjectPackage; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import javax.swing.*; public class Copypasta < public static JFrame frame; BufferedImage img; public static int WIDTH = 500; public static int HEIGHT = 500; public Copypasta() < >public static void main(String[] a) < Copypasta t=new Copypasta(); frame = new JFrame("WINDOW"); frame.setVisible(true); t.start(); frame.add(new JLabel(new ImageIcon(t.getImage()))); frame.pack(); // Better to DISPOSE than EXIT frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); >public Image getImage() < return img; >public void start() < img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB); int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData(); boolean running=true; while(running)< BufferStrategy bs=frame.getBufferStrategy(); if(bs==null)< frame.createBufferStrategy(4); return; >for (int i = 0; i < WIDTH * HEIGHT; i++) pixels[i] = 0; //This is what i've been trying to change. Graphics g= bs.getDrawGraphics(); g.drawImage(img, 0, 0, WIDTH, HEIGHT, null); g.dispose(); bs.show(); >> > 

I Apologize for the indentation errors. I promise it looks right in the editor. When set to BufferedImage type ARGB, the black background disappears, causing me to believe that the start function isn't drawing to the Image at all, or the drawn image is not being drawn on the screen. Either way, There is something that I don't understand. If you have the time, I would appreciate some help Identifying What is going wrong, if not an explanation of why. Thank you all, -Navi. Link to Original Question: drawing your own buffered image on frame

Источник

Читайте также:  How to create files with java
Оцените статью