I'm implementing a frame processor plugin (react-native-vision-camera). I would like to be able to load an image from a file and return it instead of the ImageProxy type frame so I can controll frames content.
For that, I have to load an image (no worries so far) and create an Image or an ImageProxy from the loaded image.
(I also tried to replace the bytes of the ImageProxy type image with those of the loaded image but I didn't succeed either. So I am also interested in this topic.)
#Override
public Object callback(#NotNull ImageProxy image, #NotNull Object[] params) {
try {
// Get path of image to load
String param = params[0].toString();
Bitmap bmImg = BitmapFactory.decodeFile(param);
ImageView img = new ImageView(this);
img.setImageBitmap(bmImg);
return img;
//return image;
} catch (Exception e) {
System.out.println("CRASHED");
e.printStackTrace();
}
return null;
}
To do this I tried the code above but I am creating an ImageView and not an Image or an ImageProxy.
Any idea or sample code so I can control frame content?
Thanks in advance
Related
Hello Guys i trying to resize a bitmap using the comand Bitmap.CreateScaleBitmap but that comand create an new image at the my dispositivo. i need in one solution for resize not create one new image.
my code:
if(requestCode ==0){
Bitmap bitmap = null;
try {
uri_image_boi=data.getData();
bitmap=MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),uri_image_boi);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap,600,400,false);
image_boi.setImageDrawable(new BitmapDrawable(resizedBitmap));
botao_adicionar_foto.setAlpha(0);
resize= getImageUri(this,resizedBitmap);
} catch (Exception e) {
}
}
}
i have be trying to upload the signature captured from the codename one signature to my php server.the problem is that the image uploaded is a black image.Below is my code.how can i fix this
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
and the php code
[![image uploaded][1]][1]
<?php
#SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
the black img is the file which is uploaded to the server.the other shows the screenshot of the running app.i would like to upload the signature as shown in the screenshot
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage will be opaque with the white color background.
Apparently, this is my code I have been using to get image from my disks to my program.
public void getVehicleImage(){
FileDialog fd = new FileDialog(this);
fd.setFile("*.jpg; *.jpg; *.png; *.gif");
fd.show();
vehicle_path = fd.getDirectory()+fd.getFile();
vehicleFileName.setText(vehicle_path = fd.getFile());
vehicleImagePath.setText(vehicle_path = fd.getDirectory()+fd.getFile());
image.setIcon(new ImageIcon(vehicle_path));
}
How do I fit the image to the size of my jLabel? I have tried using the
getScaledInstance()
but still no good. And also I want to ask if I am using the right code on how to get Image from my disk? I kinda feel it is wrong.
I faced similar problem and did below workaround:
Step1: Read the picture as a BufferedImage from your file system.
BufferedImage image = null;
try {
image = ImageIO.read(new File("fileName.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
Step2: Create a new BufferedImage that is the size of the JLabel
BufferedImage img= image.getScaledInstance(label.width, label.height,
Image.SCALE_SMOOTH);
Step3: Create new ImageIcon from the resized BufferedImage (Step 2)
ImageIcon imageIcon = new ImageIcon(img);
In your case, create a helper method and call it while creating ImageIcon as below:
public void getVehicleImage(){
...................
image.setIcon(new ImageIcon(getScaledImage(vehicle_path)));//call helper here
}
This can be helper function:
public BufferedImage getScaledImage(String imagePath){
BufferedImage image = null;
try {
image = ImageIO.read(new File("fileName.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage img= image.getScaledInstance(label.width, label.height,
Image.SCALE_SMOOTH);
return img;
}
I am trying to scale an image to fit the screen width.
Therefore I am trying to load the imported multi image in the beforeTerminalGUI method here:
#Override
protected void beforeTerminalGUI(Form f) {
ImageViewer iv = findImageViewer(f);
Image i = fetchResourceFile().getImage("stillstand.PNG");
i.scaledWidth(iv.getWidth());
iv.setImage(i);
}
which was created when I clicked the beforeShow event in the gui builder. (I also saved the GUI builder after doing so)
But I am getting this error:
java.lang.ArithmeticException: / by zero
at com.codename1.impl.javase.JavaSEPort.scaleArray(JavaSEPort.java:3505)
at com.codename1.impl.javase.JavaSEPort.scale(JavaSEPort.java:3497)
at com.codename1.ui.Image.scale(Image.java:961)
at com.codename1.ui.Image.scaledImpl(Image.java:931)
at com.codename1.ui.Image.scaled(Image.java:896)
at com.codename1.ui.Image.scaledWidth(Image.java:835)
at com.codename1.ui.EncodedImage.scaledWidth(EncodedImage.java:536)
at userclasses.StateMachine.beforeTerminalGUI(StateMachine.java:37)
at generated.StateMachineBase.beforeShow(StateMachineBase.java:537)
at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2512)
at com.codename1.ui.util.UIBuilder.showContainerImpl(UIBuilder.java:2334)
at com.codename1.ui.util.UIBuilder.showContainer(UIBuilder.java:2214)
at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2900)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:338)
at com.codename1.ui.Form.actionCommandImpl(Form.java:1415)
at com.codename1.ui.Form.dispatchCommand(Form.java:1381)
at com.codename1.ui.SideMenuBar$CommandWrapper$ShowWaiter.run(SideMenuBar.java:1611)
at com.codename1.ui.Display.processSerialCalls(Display.java:1147)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1091)
at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
same happens for the methods i.scale (..),i.scaledHeight(..) whenever I run the app and want to switch to the TerminalGUI.
I am new to CodenameOne and any help is welcome.
The problem is iv.getWidth() You should use iv.getImage().getWidth(), this will return the width of the ImageViewer's image and not the width of the component itself. Also check for existence of the image stillstand.PNG before scaling it.
#Override
protected void beforeTerminalGUI(Form f) {
ImageViewer iv = findImageViewer(f);
Image i = Resources.openLayered("/theme").getImage("stillstand.PNG");
if (i != null) {
i.scaledWidth(iv.getImage().getWidth());
iv.setImage(i);
} else {
System.out.println("Image not found");
}
}
Another way to scale the image to fit the screen width is doing this:
#Override
protected void beforeTerminalGUI(Form f) {
ImageViewer iv = findImageViewer(f);
Image i = Resources.openLayered("/theme").getImage("stillstand.PNG");
if (i != null) {
iv.setImage(i.scaledWidth(Display.getInstance().getDisplayWidth()));
} else {
System.out.println("Image not found");
}
}
I am having trouble setting images in my newest game. When I call the method getImage(String), and I get the Image like so:
Image head = getImage("Torso_model_01.png");
I get the following error message:
Err: java.lang.NullPointerException
At PB2Main.Body(Body.java : 27)
...
and so on...
On this tutorial, it explains how to get an image using ImageIcon like so:
String imgFile = "Images/" + img;
URL imgURL = getClass().getClassLoader().getResource(imgFile);
ImageIcon imageIcon;
Image image;
if(imgURL != null){
imageIcon = new imageIcon(imgURL);
image = imageIcon.getImage();
}
final Image anImage = image;
I made a method for this:
public URL getURL(String img){
String imgFile = "Images/" + img;
URL imgURL = getClass().getClassLoader().getResource(imgFile);
return imgURL;
}
Then I made a method called getImage(String)
public Image getImage(String img) {
ImageIcon imageIcon;
Image image;
URL imgURL = getClass().getClassLoader().getResource(getURL(img));
if(imgURL != null){
imageIcon = new ImageIcon(imgURL);
image = imageIcon.getImage();
return image;
}
System.err.println("Unable to Locate Image: " + imgURL);
}
Now, I have a class called Body.
In that class, I have a constructor:
public Body(float x, float y, String head, String torso){//atm this is just so i can get the image to actually draw on the screen
Image Head = debugger.getImage(head);// debugger doubles as a library and debugger
//i also can't have this class extend debugger otherwise it will create a window :/
// is that a glitch or something in Java? :L perhaps i just don't understand
// inheritance very well and what happens exactly when you inherit a class :(
Image Torso = debugger.getImage(torso);
g2.drawImage(Head, canvas.geWidth()/ 2,canvas.getHeight()/2, null)// canvas: the window to
//draw to
// can someone also quickly explain in their answer what an image observer is please?
g2.drawImage(Torso, Head.getX() - 5, Head.getY() - 5, null);
}
The compiler gives me the following error message:
java.lang.NullPointerException
At PlazmaBurst2.Body(Body.java: 37)
//the code it brings me to is line 1 in the constructor:
/* null: */ Image Head = debugger.getImage(img);
I don't understand where this NullPointerException is coming from. I did it exactly how they do it in the Custom Graphics Programming section of the same site.
The code works fine if I just copy and paste the code, but not if I use the method getImage(String).
You're problem is on line 3 of getImage(String):
URL imgURL = getClass().getClassLoader().getResource(getURL(img));
This should be changed to:
URL imgURL = getURL(img);