I'm working on a image processing application and I use this code
Camera {
id: cam
viewfinder.resolution: "640x480"
viewfinder.minimumFrameRate: 5
captureMode: Camera.CaptureStillImage
imageCapture {
resolution: "640x480"
onReadyChanged: {
imageDecoder.reset() // my decoder class
cam.imageCapture.capture()
}
onImageCaptured: {
console.log(requestId, preview)
imageDecoder.decodeQmlPreview(preview)
}
}
imageProcessing {
contrast: 0.60
}
}
VideoOutput {
id: output
fillMode: VideoOutput.PreserveAspectCrop
anchors.fill: parent
source: cam
}
I see that all these captured images (preview) are stored on the hard drive and are never being deleted. How to delete these images? I have very limited hard disk space available.
# ls -lh | grep IMG
-rw-r--r-- 1 root root 10.5K Jan 1 00:40 IMG_00000001.jpg
-rw-r--r-- 1 root root 12.2K Jan 1 00:45 IMG_00000002.jpg
-rw-r--r-- 1 root root 15.6K Jan 1 00:48 IMG_00000003.jpg
-rw-r--r-- 1 root root 15.6K Jan 1 00:49 IMG_00000004.jpg
-rw-r--r-- 1 root root 11.4K Jan 1 01:05 IMG_00000005.jpg
QML has a rather implicit relation to the FS - its internal functionality will read and sometimes write, but it doesn't seem to have a defined API for the user to interact directly with the FS.
Which means you will have to implement that functionality in C++, and expose it to QML.
My practice is to create a class System : public QObject and put all the necessary stuff inside, and then in main.cpp:
System s(&engine);
engine.rootContext()->setContextProperty("Sys", &s);
Then from QML you can simply Sys.doYourStuff(). In your case you need the static function bool QFile::remove(const QString &fileName).
Related
I was able to refine the program further, but still need a little help in terms of storing the now separate words into an array. My current code only takes in the first word/command only. Here's my new code:
#include <unistd.h>
#include <string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int main(){
int pid,status,rs;
char comm[100];
cout<<"Enter command:\n ";
cin.getline(comm,100);
char *token=strtok(comm," ");
pid=fork();
if(pid==-1)
{
perror("fork");
exit(pid);
}
if(pid==0)
{
while(token!=NULL){
char *argv[]={token,0};
rs=execvp(argv[0],argv);
token = strtok(NULL, " ");
}
if(rs==-1)
{
perror("execvp");
exit(rs);
}
}
else
{
cout<<"Done waiting for: "<<wait(&status)<<endl;
}
}
The outcome of the code should be something like this:
% ./z123456
Enter command: mkdir one
Enter command: touch one/file one/other one/more
Enter command: ls -l one
total 0
-rw-r--r-- 1 student student 0 Apr 3 13:03 file
-rw-r--r-- 1 student student 0 Apr 3 13:03 more
-rw-r--r-- 1 student student 0 Apr 3 13:03 other
Enter command: gobly gook
gobly: No such file or directory
Enter command: cd one
cd: No such file or directory
Enter command: exit
%
I have a custom label control in which the font alone is being changed to Bold.
Got the following crash log from Appstore which I am unable to reproduce.
The crash log points to navigating to .xaml with my custom control.
public class CustomLabelBold : Label
{
public CustomLabelBold()
{
FontFamily = "Avenir Next";
}
}
Following is the crash log:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x00000001814492ec __pthread_kill + 8
1 libsystem_pthread.dylib 0x00000001815ea288 pthread_kill$VARIANT$mp + 376 (pthread.c:1484)
2 libsystem_c.dylib 0x00000001813b7db0 __abort + 152 (abort.c:128)
3 libsystem_c.dylib 0x00000001813b7d18 abort + 152 (abort.c:99)
4 MyProject 0x00000001031046e0 print_callback(char const*, int) + 39061216 (runtime.m:1216)
5 MyProject 0x00000001030ff1a8 monoeg_g_log + 39039400 (goutput.c:125)
6 MyProject 0x00000001030ba098 major_alloc_object + 38756504 (sgen-marksweep.c:697)
7 MyProject 0x00000001030dbec4 copy_object_no_checks + 38895300 (sgen-copy-object.h:71)
8 MyProject 0x00000001030db470 simple_nursery_serial_scan_object + 38892656 (sgen-minor-copy-object.h:250)
9 MyProject 0x00000001030dbdf0 simple_nursery_serial_drain_gray_stack + 38895088 (sgen-gray.h:191)
10 MyProject 0x00000001030b390c finish_gray_stack + 38729996 (sgen-gc.c:1099)
11 MyProject 0x00000001030b29d0 collect_nursery + 38726096 (sgen-gc.c:1807)
12 MyProject 0x00000001030af388 sgen_perform_collection + 38712200 (sgen-gc.c:2534)
13 MyProject 0x00000001030a5f00 sgen_alloc_obj_nolock + 38674176 (sgen-alloc.c:257)
14 MyProject 0x00000001030a639c sgen_alloc_obj + 38675356 (sgen-alloc.c:423)
15 MyProject 0x000000010307f100 mono_gc_alloc_obj + 38514944 (sgen-mono.c:948)
16 MyProject 0x0000000100ce46a8 wrapper_managed_to_native_object___icall_wrapper_mono_gc_alloc_obj_intptr_intptr + 104
17 MyProject 0x0000000100cde564 wrapper_alloc_object_AllocSmall_intptr_intptr + 228
18 MyProject 0x0000000100fd71c4 Xamarin_Forms_Core_Xamarin_Forms_BindableObject__ctor + 4272580 (.D:\agent\_work\1\s\Xamarin.Forms.Core\BindableObject.cs:1)
19 MyProject 0x000000010103326c Xamarin_Forms_Core_Xamarin_Forms_VisualElement__ctor + 4649580 (.D:\agent\_work\1\s\Xamarin.Forms.Core\VisualElement.cs:122)
20 MyProject 0x0000000100fe3bc0 Xamarin_Forms_Core_Xamarin_Forms_View__ctor + 4324288 (.D:\agent\_work\1\s\Xamarin.Forms.Core\View.cs:24)
21 MyProject 0x000000010100fa9c Xamarin_Forms_Core_Xamarin_Forms_Label__ctor + 4504220 (.D:\agent\_work\1\s\Xamarin.Forms.Core\Label.cs:57)
22 MyProject 0x00000001018bd5e4 Core_MyProject_Core_CustomControls_CustomLabelBold__ctor + 20
Android requires you to specify the actual file name of the font you want to use.
If you just hand over the font name, this will cause a crash.
So make sure you set your FontFamily property to the correct file name (in your case the name of the font defining the bold version).
Also I would suggest adapting the code in your custom renderer as follows, in order to prevent your app from crashing when having specified the wrong font:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
TextView label = (TextView)Control;
if (e.NewElement?.FontFamily != null)
{
Typeface font = null;
try
{
font = Typeface.CreateFromAsset(AndroidApp.Application.Context.Assets, e.NewElement.FontFamily);
}
catch (Exception)
{
font = Typeface.Default;
}
label.Typeface = font;
}
}
This will result in your label falling back to the default font, instead of your whole app going down. As a bonus effect, it will also enable you testing your app within the xamarin live player or inspecting your screen in the forms previewer, since native resources are not available when you use one of those execution environments.
For iOS, make sure that you have an entry in your info.plist which lists all the fonts you have added to your app.
<key>UIAppFonts</key>
<array>
<string>fontawesome.ttf</string>
<string>OpenSans-Light.ttf</string>
<string>OpenSans-Bold.ttf</string>
<string>OpenSans-LightItalic.ttf</string>
<string>OpenSans-Italic.ttf</string>
<string>OpenSans-Regular.ttf</string>
<string>Lato-Bold.ttf</string>
<string>Lato-Regular.ttf</string>
</array>
Also make sure that the font file is in your Resources Folder and its build action is set to "BundleResource". Strangely enough, iOS needs to have the FontFamily property set to the filename without the .ttf extension.
I tried recreating your crash, but to no success. The worst thing that happened was that the default font was being rendered.
Even with all fonts in the right place and the property set to the right name, without a custom renderer, the font didn't appear at all.
This is from the custom renderer i am using for iOS labels:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && e.NewElement.FontFamily != null)
{
e.NewElement.FontFamily = e.NewElement.FontFamily.Replace(".ttf", "");
}
}
I have the following directory on my laptop:
/tmp/
myapp/
assets/
config.yml
models/
troll.ply
tree.ply
textures/
troll-skin.png
tree-skin.png
I would like to zip /tmp/myapp/assets (and all its recursive contents) up into a ZIP named assets.zip, such that, when I unzip it (via unzip assets.zip), it preserves the directory structure under the assets folder. Hence, when unzipped, it would show config.yml in the "root" of the ZIP, and 2 directories inside the ZIP (models and textures). The rest of the files would be inside these respective subdirectories, etc.
When I run this code:
File sourceDir = new File("/tmp/myapp/assets");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("/Users/myuser/archives/assets.zip"));
File[] contents = sourceDir.listFiles();
for(File file : contents) {
zip.putNextEntry(new ZipEntry(file.name));
InputStream isteam = new FileInputStream(file);
Files.copy(isteam, zip);
zip.closeEntry();
isteam.close();
}
zip.close();
The code correctly creates a ZIP at /Users/myuser/archives/assets.zip.
However, when I unzip it (unzip /Users/myuser/archives/assets.zip) and then run ls -al /Users/myuser/archives, my output is:
-rw-r--r-- 1 myuser 1754083733 492 Dec 30 14:14 assets.zip
-rw-r--r-- 1 myuser 1754083733 10 Dec 30 14:14 config.yml
-rw-r--r-- 1 myuser 1754083733 7 Dec 30 14:14 models
-rw-r--r-- 1 myuser 1754083733 9 Dec 30 14:14 textures
So both models and textures are being treated like files (not as directories). Furthermore, when I take a peek at the contents of the "models file", it appears that the contents of troll.ply and tree.ply have been concatenated inside of it, and ditto for the "tree file" with the 2 PNGs.
How can I tweak this so that directory structure (no matter how deep/nested) is always preserved in the resultant ZIP?
you can probably use the recursive method call to preserve the sub directories structure:
private static void addDir(File sourceDir, ZipOutputStream zip) throws IOException {
File[] contents = sourceDir.listFiles();
for(File file : contents) {
if(file.isDirectory()){
addDir(file, zip);
} else {
zip.putNextEntry(new ZipEntry(file.getAbsolutePath().replace("/tmp/myapp/","")));
System.out.println("file name " + file.getAbsolutePath().replace("/tmp/myapp/",""));
Path rn_demo = Paths.get(String.valueOf(file));
Files.copy(rn_demo, zip);
}
}
zip.closeEntry();
}
and you call in main method as below:
public static void main(String[] args) throws IOException {
File sourceDir = new File("/tmp/myapp/assets");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("/Users/myuser/archives/assets.zip"));
addDir(sourceDir, zip);
zip.close();
}
Zipping through Java Zip seems to work differently on different OS's. I had the issue that it was working fine on Windows 7. But on Linux (RHEL6) the files were before the folders. This caused tests to fail.
A way to solve it is to sort the files and folders. folders first and then files. So the..
File[] contents = sourceDir.listFiles();
...File array should be sorted via path. Create a List<> from the Files and sort.
Collections.sort(newFiles, (a, b) ->
b.getAbsolutePath().compareTo(a.getAbsolutePath())
);
Note, I created an InputFile object to store the absolute path of the file.
My application contains a class which inherits QWebView. The problem is that I get a crash every time I try to type something in this view! Here's my code :
void QViewSupport::setupSupport(QWidget * widget)
{
QUrl startURL = QUrl("http://www.google.fr");
load(startURL);
}
So basically whenever I try to type something in the google search bar, it crashes at the first character...
Here's the output :
ASSERTION FAILED: ICU could not open a break iterator:
U_MISSING_RESOURCE_ERROR (2)
U_SUCCESS(openStatus)
c:\work\build\qt5_workdir\w\s\qtwebkit\source\webcore\platform\text\TextBreakIteratorICU.cpp(45) : WebCore::setUpIterator
1 02426EF7
2 018F0F40
3 018F0CA0
4 018F0E1A
5 0207D7D3
6 0207CE91
7 014F1470
8 01CDBBF5
9 013EEEAC
10 013A45BE
11 01392CA7
12 013A4076
13 0134C062
14 0136166A
15 0144F19A
16 014562E3
17 014205E5
18 0141D9A6
19 017DCE6C
20 0134C82C
21 014EA80F
22 014F1525
23 013EEEAC
24 013A45BE
25 01392CA7
26 013A4076
27 0134C062
28 0136166A
29 017DCE25
30 014202C3
31 0100D627
First chance exception at 0x02426ef7 (Qt5WebKitd.dll) in myApp.exe : 0xC0000005: Access violation writing location 0xbbadbeef.
Unhandled exception à 0x02426ef7 (Qt5WebKitd.dll) in myApp.exe : 0xC0000005: Access violation writing location 0xbbadbe
Am I missing something? I searched the Internet and couldn't find something close to this problem. Thanks in advance if you have the solution!
Edit : As asked, here's the class :
class QViewSupport : public QWebView
{
Q_OBJECT
public:
QViewSupport(QWidget *parent);
~QViewSupport();
private:
void setupSupport(QWidget *Form);
};
And in the .cpp file :
#include "qviewsupport.h"
QViewSupport::QViewSupport(QWidget *parent)
: QWebView(parent)
{
setupSupport(this);
}
void QViewSupport::setupSupport(QWidget * widget)
{
QUrl startURL = QUrl("http://www.google.fr");
load(startURL);
}
QViewSupport::~QViewSupport()
{
}
Edit : The call to this function is done in Setup.cpp (see below), and pViewSupport is a private member define in the Setup class : QViewSupport* pViewSupport;.
Setup::Setup(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
}
void Setup::setupUi(QWidget * widget)
{
// plenty of other things
pViewSupport = new QViewSupport(this);
// same
}
Ok, the problem came from the ICU dlls from Qt, I just replaced them and it worked.
I have to read a bunch of .CSV files with dynamic file names from a SFTP server. These files get generated every 15 minutes.
I am using JSch's ChannelSftp, but there is no method which would give the exact filenames. I only see an .ls() method. This gives a Vector e.g.
[drwxr-xr-x 2 2019 2019 144 Aug 9 22:29 .,
drwx------ 6 2019 2019 176 Aug 27 2009 ..,
-rw-r--r-- 1 2019 2019 121 Aug 9 21:03 data_task1_2011_TEST.csv,
-rw-r--r-- 1 2019 2019 121 Aug 9 20:57 data_task1_20110809210007.csv]
Is there a simple way to read all the files in a directory and copy them to another location?
This code works for copying a single file:
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
channelSftp.get("data_task1_20110809210007.csv","data_task1_20110809210007.csv");
The ls method is the one you need. It returns a vector of LsEntry objects, each of which you can ask about its name.
So, after your channelSftp.cd(SFTPWORKINGDIR);, you could do the following:
Vector<ChannelSftp.LsEntry> list = channelSftp.ls("*.cvs");
for(ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), destinationPath + entry.getFilename());
}
(This assumes destinationPath is a local directory name ending with / (or \ in Windows).)
Of course, if you don't want to download the same files again after 15 minutes, you might want to have a list of the local files, to compare them (use a HashSet or similar), or delete them from the server.
Note that ls is case sensitive. This method retrieves all csv files, regardless of the extension case
ArrayList<String> list = new ArrayList<String>();
Vector<LsEntry> entries = sftpChannel.ls("*.*");
for (LsEntry entry : entries) {
if(entry.getFilename().toLowerCase().endsWith(".csv")) {
list.add(entry.getFilename());
}
}