How to remove imageview from layout and add textview before converting the layout into image - android-linearlayout

Im developing one app, In that app, Im sharing image in social networks(Converting the layout into image) so my question is in my layout am having share icon, so i want remove that share icon and add textview to that layout before converting into image. can anyone give solution for my question. Thanks in advance
final View itemView = inflater.inflate(R.layout.fragment_layout, container, false);
itemView.setDrawingCacheEnabled(true);
itemView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
itemView.layout(0, 0, 300, 400);
itemView.buildDrawingCache(true);
holder.share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(itemView.getWidth(),itemView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
itemView.draw(canvas);
Uri bmpUri = null;
try {
// output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
sendIntent.putExtra(Intent.EXTRA_TEXT, SplashScreen.newstitleArray.get(SplashScreen.myCustomPosition));
sendIntent.setType("image/*");
context1.startActivity(sendIntent);
}
});

Related

why zxing can not decode this qrcode image?

JavaSE: Exception is "com.google.zxing.NotFoundException"
Can any one help me?
The version is 3.4.1
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.1</version>
</dependency>
and the java code is there.
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Map<DecodeHintType,Object> hints = new LinkedHashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
The Exception info is :
com.google.zxing.NotFoundException
There is not anything other displayed...
But the following code can not be decoded out,It's really weird
Almost identical QR codes, one can be decoded but the other cannot be coded`
Use GenericMultipleBarcodeReader to read the QR code.
public void decodefile(String filename) {
// Read an image to BufferedImage
BufferedImage image = null;
try {
image = ImageIO.read(new File(filename));
} catch (IOException e) {
System.out.println(e);
return;
}
// ZXing
BinaryBitmap bitmap = null;
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
try {
Result[] zxingResults = multiReader.decodeMultiple(bitmap);
System.out.println("ZXing result count: " + zxingResults.length);
if (zxingResults != null) {
for (Result zxingResult : zxingResults) {
System.out.println("Format: " + zxingResult.getBarcodeFormat());
System.out.println("Text: " + zxingResult.getText());
System.out.println();
}
}
} catch (NotFoundException e) {
e.printStackTrace();
}
pixels = null;
bitmap = null;
if (image != null) {
image.flush();
image = null;
}
}
Here is my results:
ZXing result count: 1
Format: QR_CODE
Text: https://mp-daily.bookln.cn/q?c=1201M9XUEDE

CLICK JAVAFXML BUTTON SCENEBUILDER

#FXML
void handleButtonAction(ActionEvent event) {
buttonOpenFile.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
openFiletxtField.setText(selectedFile.getAbsolutePath());
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
});
buttonSaveFile.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File file = new File("Details.txt");
try {
FileOutputStream fOut = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("whatever you need to write");
JOptionPane.showMessageDialog(null, "Saved Successfully..");
osw.flush();
osw.close();
} catch (IOException iOException) {
System.out.println("" + iOException.getMessage());
}
}
});
}
I Have a problem using javafx fxml button click event. Whenever I click the button once, nothing happens. I have to click it the second time for it to work? What could I be doing wrong? I have shared my code above. I am using scene builder I have everything in place apart from this error. everything is working fine apart from this issue. The code load text file from the computer directory and save the data to the loaded file.
#FXML
void handleOpenFileButtonAction(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
openFiletxtField.setText(selectedFile.getAbsolutePath());
}
}
#FXML
void handleSaveFileButtonAction(ActionEvent event) {
File file = new File("Details.txt");
try {
FileOutputStream fOut = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(firstNametxtField.getText() + "," + lastNametxtField1.getText() + "," + enrolledCheckBox.isSelected() + "," + addresstxtField.getText() + "," + statesCombobox.getSelectionModel().getSelectedItem().toString() + "\n");
JOptionPane.showMessageDialog(null, "Saved Successfully..");
osw.flush();
osw.close();
} catch (IOException iOException) {
System.out.println("" + iOException.getMessage());
}
}

binding progress bar with a method, javafx

I have a method which performs some task(reading, writing files and other tasks also) for almost 3 minutes.
I want to bind progress bar in javafx which can run with progress of the method.
This is my method
System.out.println("Going to load contract/security:"+new Date());
Map<Integer,FeedRefData> map = loadContractAndSecurityFromFile(loadFO);
addIndicesRefData(map);
BufferedWriter writer = createFile();
for (FeedRefData feedRefData : map.values()) {
try {
updateInstrumentAlias(map, feedRefData);
String refDataString = feedRefData.toString();
writer.write(refDataString, 0, refDataString.length());
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
log.info("Unable to write Quote Object to : " );
}
}
System.out.println("Ref Data File Generated:"+new Date());
For bind your method with progressbar you should do these steps :
Create a task which contains your method.
Create a thread which run this task.
Bind your progress property with your task property.
I made this simple example ,just change my code with your method code :
public class Bind extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
ProgressBar pb = new ProgressBar();
pb.setProgress(1.0);
Button button = new Button("start");
button.setOnAction((ActionEvent event) -> {
/*Create a task which contain method code*/
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
File file = new File("C:\\Users\\Electron\\Desktop\\387303_254196324635587_907962025_n.jpg");
ByteArrayOutputStream bos = null;
try {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
bos = new ByteArrayOutputStream();
for (int len; (len = fis.read(buffer)) != -1;) {
bos.write(buffer, 0, len);
updateProgress(len, file.length());
/* I sleeped operation because reading operation is quiqly*/
Thread.sleep(1000);
}
System.out.println("Reading is finished");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} catch (IOException e2) {
System.err.println(e2.getMessage());
}
return null;
}
};
Thread thread = new Thread(task);
thread.start();
/*bind the progress with task*/
pb.progressProperty()
.bind(task.progressProperty());
});
HBox box = new HBox(pb, button);
Stage stage = new Stage();
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
Operation started :
Operation finished :
PS: I used Thread.sleep(1000) because my file is so small.You can remove it if your progress time is long.

Hide custom context menu

I create this simple example of a context menu shown when I click with a mouse:
Image image = new Image("http://docs.oracle.com/javafx/"
+ "javafx/images/javafx-documentation.png");
ImageView pic = new ImageView();
pic.setImage(image);
final ContextMenu cm = new ContextMenu();
MenuItem cmItem1 = new MenuItem("Copy Image");
cmItem1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent e)
{
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putImage(pic.getImage());
clipboard.setContent(content);
}
});
cm.getItems().add(cmItem1);
pic.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent e)
{
if (e.getButton() == MouseButton.SECONDARY)
cm.show(pic, e.getScreenX(), e.getScreenY());
}
});
I noticed that when I click near the picture the context menu is still visible. How I make it hidden when I click outside if the context menu body?
set contextMenu.setAutoHide as true.
contextMenu.setAutoHide(true);

JavaFX how use Image and ImageView to open and image from my system

I am trying to display an image using Image and ImageView classes of JavaFX and it does not work.
try {
String workingDir = System.getProperty("user.dir");
final File f = new File(workingDir, "src/chk.hrn.nye.movieplayer/img.png");
// System.out.println(workingDir);
// File file = new File(loadImage.class.getResource("img1.jpg").toString());
Image img = new Image(f.toURI().toString());
ImageView imv = new ImageView(img);
Group root = new Group();
root.getChildren().add(imv);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
System.out.println(e);
}

Resources